MI - Fimex
Logger.h
Go to the documentation of this file.
1 /*
2  * Fimex
3  *
4  * (C) Copyright 2008, met.no
5  *
6  * Project Info: https://wiki.met.no/fimex/start
7  *
8  * This library is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  * License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21  * USA.
22  */
23 
24 #ifndef LOGGER_H_
25 #define LOGGER_H_
26 
27 #include <boost/shared_ptr.hpp>
28 #include <list>
29 #include <string>
30 #include <sstream>
31 
32 namespace MetNoFimex {
33 
34 class LoggerClass;
35 class LoggerImpl;
36 
49 class Logger
50 {
51 private:
52  std::string className_;
53  std::auto_ptr<LoggerImpl> pimpl_; // lazy initialized member
54  LoggerImpl* impl();
55 
56 public:
60  enum LogLevel {
61  OFF = 1000,
62  FATAL = 900,
63  ERROR = 800,
64  WARN = 700,
65  INFO = 600,
66  DEBUG = 500
67  };
68 
69  Logger(const std::string& className);
70  ~Logger();
71 
75  bool isEnabledFor(LogLevel level);
76 
78  void reset();
79 
87  void forcedLog(LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber);
88 
92  enum LogClass {
94  LOG4CPP = 1
95  };
102  static bool setClass(LogClass logClass);
103 
117  static bool setClass(LoggerClass* lc);
118 };
119 
120 
121 class LoggerImpl {
122 public:
123  virtual ~LoggerImpl();
124  virtual bool isEnabledFor(Logger::LogLevel level) = 0;
125  virtual void log(Logger::LogLevel level, const std::string& message, const char* filename, unsigned int lineNumber) = 0;
126 };
127 
128 
129 class LoggerClass {
130 public:
131  virtual ~LoggerClass();
132  virtual LoggerImpl* loggerFor(Logger* logger, const std::string& className) = 0;
133 
134 protected:
135  void remember(Logger* logger);
136  void reset();
137 
138 protected:
140  loggers_t loggers_;
141 };
142 
143 
150 extern void defaultLogLevel(Logger::LogLevel);
151 
158 #define LOG4FIMEX(logger, level, message) { \
159  if (logger->isEnabledFor(level)) {\
160  std::ostringstream buffer; \
161  buffer << message; \
162  logger->forcedLog(level, buffer.str(), __FILE__, __LINE__);}}
163 
164 typedef boost::shared_ptr<Logger> LoggerPtr;
165 
172 extern LoggerPtr getLogger(const std::string& className);
173 
174 } // namespace MetNoFimex
175 
176 
177 #endif /* LOGGER_H_ */
basic_string< char > string
Logger::LogLevel defaultLogLevel()
std::list< Logger * > loggers_t
Definition: Logger.h:139
Definition: Logger.h:63
loggers_t loggers_
Definition: Logger.h:140
Definition: Logger.h:94
Definition: Logger.h:61
LogLevel
Definition: Logger.h:60
LoggerPtr getLogger(const std::string &className)
Definition: Logger.h:121
Definition: C_CDMReader.h:35
Definition: Logger.h:66
static bool setClass(LogClass logClass)
Definition: Logger.h:49
Definition: Logger.h:65
bool isEnabledFor(LogLevel level)
Logger(const std::string &className)
STL class.
Definition: Logger.h:129
Definition: Logger.h:62
Definition: Logger.h:64
Definition: Logger.h:93
void forcedLog(LogLevel level, const std::string &message, const char *filename, unsigned int lineNumber)
LogClass
Definition: Logger.h:92
boost::shared_ptr< Logger > LoggerPtr
Definition: Logger.h:164