CPN
Computational Process Networks
Logger.h
Go to the documentation of this file.
1 //=============================================================================
2 // Computational Process Networks class library
3 // Copyright (C) 1997-2006 Gregory E. Allen and The University of Texas
4 //
5 // This library is free software; you can redistribute it and/or modify it
6 // under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
14 //
15 // The GNU Public License is available in the file LICENSE, or you
16 // can write to the Free Software Foundation, Inc., 59 Temple Place -
17 // Suite 330, Boston, MA 02111-1307, USA, or you can find it on the
18 // World Wide Web at http://www.fsf.org.
19 //=============================================================================
25 #ifndef LOGGER_H
26 #define LOGGER_H
27 #pragma once
28 #include <cpn/common.h>
30 #include <string>
31 #include <cstdarg>
37 class LoggerOutput {
38 public:
39  virtual ~LoggerOutput();
42  virtual int LogLevel() const = 0;
46  virtual int LogLevel(int level) = 0;
51  virtual void Log(int level, const std::string &msg) = 0;
52 };
53 
57 class Logger : public LoggerOutput {
58 public:
59  enum {
60  ERROR = 100,
61  WARNING = 75,
62  INFO = 50,
63  DEBUG = 25,
64  TRACE = 0
65  };
66  Logger();
67  Logger(int dfltlvl);
68  Logger(LoggerOutput *lo, int dfltlvl);
69  Logger(LoggerOutput *lo, int dfltlevel, const std::string &nm);
70  virtual ~Logger();
71 
72  int LogLevel() const;
73  int LogLevel(int level);
74 
75  int DefaultLevel() const;
76  int DefaultLevel(int level);
77 
78  int Adjust() const;
79  int Adjust(int a);
80 
81  const std::string &Name() const;
82  const std::string &Name(const std::string &nm);
83 
86 
87  virtual void Log(int level, const std::string &msg);
88 
89  void Log(const std::string &msg) { Log(defaultlevel, msg); }
90 
91  void Logf(int level, const char *fmt, ...);
92 
93  void vLogf(int level, const char *fmt, va_list ap);
94 
95  void Logf(const char *fmt, ...);
96 
97  void Error(const char *fmt, ...);
98  void Warn(const char *fmt, ...);
99  void Info(const char *fmt, ...);
100  void Debug(const char *fmt, ...);
101  void Trace(const char *fmt, ...);
102 protected:
103  Logger(const Logger&);
104  Logger &operator=(const Logger&);
105 private:
106 
109  int loglevel;
111  int adjust;
112  std::string name;
113 };
114 
119 public:
120  LoggerStdOutput(int level) : loglevel(level) {}
121  int LogLevel(int level);
122  int LogLevel() const;
123  void Log(int level, const std::string &msg);
124 private:
126  int loglevel;
127 };
128 
129 struct ScopeTrace {
130  ScopeTrace(Logger &l, const char *fn, unsigned ln)
131  : logger(l), fname(fn), line(ln)
132  {
133  logger.Trace("Enter %s:%u", fname, line);
134  }
136  logger.Trace("Exit %s:%u", fname, line);
137  }
139  const char *fname;
140  unsigned line;
141 };
142 // Will create a tracer on the stack which outputs a trace message of
143 // Enterying function and exiting function and it will use the symbol
144 // scoperace[linenumber]
145 #define SCOPE_TRACE(logger) ScopeTrace scopetracer__LINE__ (logger, __PRETTY_FUNCTION__, __LINE__)
146 
147 #endif
Logger object that is used for forwarding log messages.
Definition: Logger.h:57
void vLogf(int level, const char *fmt, va_list ap)
Definition: Logger.cc:132
virtual ~LoggerOutput()
Definition: Logger.cc:31
std::string name
Definition: Logger.h:112
int Adjust() const
Definition: Logger.cc:78
virtual void Log(int level, const std::string &msg)=0
Log a message to this outputer.
virtual ~Logger()
Definition: Logger.cc:42
LoggerOutput * Output()
Definition: Logger.cc:98
virtual int LogLevel() const =0
A LoggerOutput implementation that prints to stdout.
Definition: Logger.h:118
void Trace(const char *fmt,...)
Definition: Logger.cc:195
Logger()
Definition: Logger.cc:34
int defaultlevel
Definition: Logger.h:110
void Error(const char *fmt,...)
Definition: Logger.cc:159
LoggerStdOutput(int level)
Definition: Logger.h:120
unsigned line
Definition: Logger.h:140
LoggerOutput * logout
Definition: Logger.h:108
void Log(int level, const std::string &msg)
Log a message to this outputer.
Definition: Logger.cc:214
void Info(const char *fmt,...)
Definition: Logger.cc:177
virtual void Log(int level, const std::string &msg)
Log a message to this outputer.
Definition: Logger.cc:108
Abstract base class for logger outputers. Any object who wishes to be a place for logging messages to...
Definition: Logger.h:37
const std::string & Name() const
Definition: Logger.cc:88
void Log(const std::string &msg)
Definition: Logger.h:89
int LogLevel() const
Definition: Logger.cc:209
Sync::ReentrantLock lock
Definition: Logger.h:107
int LogLevel() const
Definition: Logger.cc:58
Sync::ReentrantLock lock
Definition: Logger.h:125
const char * fname
Definition: Logger.h:139
int loglevel
Definition: Logger.h:109
int adjust
Definition: Logger.h:111
void Warn(const char *fmt,...)
Definition: Logger.cc:168
Logger & logger
Definition: Logger.h:138
~ScopeTrace()
Definition: Logger.h:135
ScopeTrace(Logger &l, const char *fn, unsigned ln)
Definition: Logger.h:130
Logger & operator=(const Logger &)
void Logf(int level, const char *fmt,...)
Definition: Logger.cc:114
A reentrant lock implementation.
int DefaultLevel() const
Definition: Logger.cc:68
void Debug(const char *fmt,...)
Definition: Logger.cc:186