liballofw
logger.h
Go to the documentation of this file.
1 #ifndef ALLOFW_LOGGER_H
2 #define ALLOFW_LOGGER_H
3 
4 #include "common.h"
5 
6 namespace allofw {
7 
8  class Logger;
9 
10  class LoggerFactory {
11  public:
12  // The factory function should be thread-safe, but the returned logger might not.
13  virtual Logger* getThreadLogger() = 0;
14  // Set the logger for the current thread, so the next time getThreadLogger will return this logger.
15  // The caller is responsible for releasing the logger.
16  virtual void setThreadLogger(Logger* logger) = 0;
17 
18  virtual ~LoggerFactory() { }
19 
20  // Default logger writes to stderr.
21  static LoggerFactory* Default();
22  static void SetDefault(LoggerFactory* factory);
23  };
24 
25  class Logger {
26  public:
27  // Log levels.
28  static const int kVerbose = 0;
29  static const int kInfo = 100;
30  static const int kWarning = 200;
31  static const int kError = 300;
32  static const int kFatal = 400;
33 
34  // Level filter.
35  virtual void setLevelFilter(int minimum_level) = 0;
36 
37  // Scope functionality.
38  virtual void pushScope(const char* prefix = " ") = 0;
39  virtual void popScope() = 0;
40  // Output.
41  virtual void print(int level, const char* string) = 0;
42  virtual void printf(int level, const char* fmt, ...) = 0;
43  virtual void vprintf(int level, const char* fmt, va_list args) = 0;
44 
45  virtual ~Logger() { }
46 
47  // Get logger from the default logger factory.
48  // The function is thread-safe, but the returned logger is not.
49  inline static Logger* Default() { return LoggerFactory::Default()->getThreadLogger(); }
50  };
51 
52  // This class implements basic scope functions, and also keeps thread ID upon creation.
53  class ScopedLogger : public Logger, non_copyable {
54  public:
55  ScopedLogger();
56  virtual void setLevelFilter(int minimum_level) override;
57  virtual void pushScope(const char* prefix = " ") override;
58  virtual void popScope() override;
59  virtual void print(int level, const char* string) override;
60  virtual void printf(int level, const char* fmt, ...) override;
61  virtual void vprintf(int level, const char* fmt, va_list args) override;
62  ~ScopedLogger();
63  protected:
64  // Implement this to suit your needs.
65  virtual void loggerOutput(int level, const char* string) = 0;
66  private:
67  struct Details;
68  Details* details_;
69  };
70 
71  // Handy class to be used with C++ scopes.
72  class LoggerScope {
73  public:
74  LoggerScope(int level, const char* header = nullptr, const char* prefix = " ", Logger* logger = Logger::Default());
75  inline void operator()(const char* str) { print(str); }
76  void print(const char* string);
77  void printf(const char* fmt, ...);
78  ~LoggerScope();
79  private:
80  LoggerScope(const LoggerScope&);
81  LoggerScope& operator = (const LoggerScope&);
82  Logger* logger_;
83  int level_;
84  };
85 
86 }
87 
88 #endif
void operator()(const char *str)
Definition: logger.h:75
static void SetDefault(LoggerFactory *factory)
Definition: logger.h:10
virtual Logger * getThreadLogger()=0
static LoggerFactory * Default()
Definition: allofw.h:12
virtual void setThreadLogger(Logger *logger)=0
Definition: logger.h:72
Inherit this to make a class non-copyable.
Definition: common.h:44
virtual ~Logger()
Definition: logger.h:45
Definition: logger.h:25
static Logger * Default()
Definition: logger.h:49
virtual ~LoggerFactory()
Definition: logger.h:18
Definition: logger.h:53