DABC (Data Acquisition Backbone Core)  2.9.9
logging.h
Go to the documentation of this file.
1 // $Id: logging.h 4474 2020-04-15 13:47:01Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_logging
17 #define DABC_logging
18 
19 #include <cstdio>
20 
21 #include <cstdint>
22 
23 #ifndef DABC_string
24 #include "dabc/string.h"
25 #endif
26 
27 #ifndef DABC_defines
28 #include "dabc/defines.h"
29 #endif
30 
31 #ifndef DEBUGLEVEL
32 #define DEBUGLEVEL 1
33 #endif
34 
35 namespace dabc {
36 
37  class LoggerEntry;
38  class LoggerLineEntry;
39  class Mutex;
40 
49  class Logger {
50  public:
51  enum EShowInfo {
52  lPrefix = 0x0001, // show configured debug prefix
53  lDate = 0x0002, // show current date
54  lTime = 0x0004, // show current time
55  lFile = 0x0008, // show source file
56  lFunc = 0x0010, // show source func
57  lLine = 0x0020, // show line number
58  lLevel = 0x0040, // show message level
59  lMessage = 0x0080, // show debug message itself
60  lNoDrop = 0x0100, // disable drop of frequent messages
61  lNoPrefix= 0x0200, // disable prefix output (superior to lPrefix)
62  lTStamp = 0x0400, // show TimeStamp (ms precision)
63  lSyslgLvl= 0x0800 // show messege level in syslog format
64  };
65 
66  Logger(bool withmutex = true);
67  virtual ~Logger();
68 
69  // Debug mask sets fields, which are displayed for normal debug messages
70  void SetDebugMask(unsigned mask) { fDebugMask = mask; }
71  unsigned GetDebugMask() const { return fDebugMask; }
72 
73  // Error mask sets fields, which are displayed for error messages
74  void SetErrorMask(unsigned mask) { fErrorMask = mask; }
75  unsigned GetErrorMask() const { return fErrorMask; }
76 
77  // File mask can only add extra fields to the output
78  void SetFileMask(unsigned mask) { fFileMask = mask; }
79  unsigned GetFileMask() const { return fFileMask; }
80 
81  void SetDebugLevel(int level = 0);
82  void SetFileLevel(int level = 0);
83  void SetSyslogLevel(int level = 0);
84 
85  void SetLogLimit(unsigned limit = 100) { fLogLimit = limit; }
86  inline int GetDebugLevel() const { return fDebugLevel; }
87  inline int GetFileLevel() const { return fFileLevel; }
88 
89  void SetPrefix(const char* prefix = 0) { fPrefix = prefix ? prefix : ""; }
90  const char* GetPrefix() { return fPrefix.c_str(); }
91 
92  virtual void LogFile(const char* fname);
93  virtual void Syslog(const char* prefix);
94 
95  void ShowStat(bool tofile = true);
96 
98  void CloseFile();
99 
100  static void CheckTimeout();
101 
102  static void DisableLogReopen();
103 
104  static inline Logger* Instance() { return gDebug; }
105 
106  static inline void Debug(int level, const char* filename, unsigned linenumber, const char* funcname, const char* message)
107  {
108  if (Instance() && (level <= Instance()->fLevel))
109  Instance()->DoOutput(level, filename, linenumber, funcname, message);
110  }
111 
112  protected:
113 
114  static Logger* gDebug;
115 
116  virtual void DoOutput(int level, const char* filename, unsigned linenumber, const char* funcname, const char* message);
117 
118  void _ExtendLines(unsigned max);
119 
120  void _FillString(std::string& str, unsigned mask, LoggerEntry* entry);
121 
122  virtual void _DoCheckTimeout();
123 
124  private:
127  unsigned fMaxLine;
129  FILE *fFile; // output file for log messages
130  std::string fSyslogPrefix; // if specified, send message to syslog
131  unsigned fDebugMask; // mask for debug output
132  unsigned fErrorMask; // mask for error output
133  unsigned fFileMask; // mask for file output
134  int fDebugLevel; // level of debug output on terminal
135  int fFileLevel; // level of debug output to file
136  int fSyslogLevel; // level of syslog message
137  int fLevel; // used to define max
138  std::string fPrefix; // prefix of all messages
139  std::string fLogFileName; // name of logfile
140  double fLogReopenTime; // last time when logfile was reopened
141  bool fLogFileModified; // true if any string was written into file
142  unsigned fLogLimit; // maximum number of log messages before drop
143  bool fLogReopenDisabled; // disable file reopen when doing shutdown
144  };
145 
146  #define DOUT(level, args ... ) \
147  dabc::Logger::Debug(level, __FILE__, __LINE__, __func__, dabc::format( args ).c_str())
148 
149  #if DEBUGLEVEL > -2
150  #define EOUT( args ... ) DOUT(-1, args )
151  #else
152  #define EOUT( args ... )
153  #endif
154 
155  #if DEBUGLEVEL > -1
156  #define DOUT0( args ... ) DOUT(0, args )
157  #else
158  #define DOUT0( args ... )
159  #endif
160 
161  #if DEBUGLEVEL > 0
162  #define DOUT1( args ... ) DOUT(1, args )
163  #else
164  #define DOUT1( args )
165  #endif
166 
167  #if DEBUGLEVEL > 1
168  #define DOUT2( args ... ) DOUT( 2, args )
169  #else
170  #define DOUT2( args ... )
171  #endif
172 
173  #if DEBUGLEVEL > 2
174  #define DOUT3( args ... ) DOUT(3, args )
175  #else
176  #define DOUT3( args ... )
177  #endif
178 
179  #if DEBUGLEVEL > 3
180  #define DOUT4( args ... ) DOUT(4, args )
181  #else
182  #define DOUT4( args ... )
183  #endif
184 
185  #if DEBUGLEVEL > 4
186  #define DOUT5( args ... ) DOUT(5, args )
187  #else
188  #define DOUT5( args ... )
189  #endif
190 
191  #define DBOOL(arg) (arg ? "true" : "false")
192 
193  #define DNAME(arg) (arg ? arg->GetName() : "---")
194 
195  extern void SetDebugLevel(int level = 0);
196  extern void SetFileLevel(int level = 0);
197  extern void SetDebugPrefix(const char* prefix = 0);
198 
199  extern Logger* lgr();
200 };
201 
202 #endif
203 
Logging class.
Definition: logging.h:49
void SetFileLevel(int level=0)
Definition: logging.cxx:176
void SetErrorMask(unsigned mask)
Definition: logging.h:74
LoggerLineEntry ** fLines
Definition: logging.h:126
double fLogReopenTime
Definition: logging.h:140
int GetFileLevel() const
Definition: logging.h:87
unsigned GetErrorMask() const
Definition: logging.h:75
Mutex * fMutex
Definition: logging.h:128
unsigned fErrorMask
Definition: logging.h:132
std::string fLogFileName
Definition: logging.h:139
virtual void Syslog(const char *prefix)
Definition: logging.cxx:226
bool fLogReopenDisabled
Definition: logging.h:143
void SetLogLimit(unsigned limit=100)
Definition: logging.h:85
virtual ~Logger()
Definition: logging.cxx:140
void CloseFile()
Close any file open by logger.
Definition: logging.cxx:161
virtual void LogFile(const char *fname)
Definition: logging.cxx:210
void SetPrefix(const char *prefix=0)
Definition: logging.h:89
Logger(bool withmutex=true)
Definition: logging.cxx:108
unsigned GetDebugMask() const
Definition: logging.h:71
void SetSyslogLevel(int level=0)
Definition: logging.cxx:182
unsigned fDebugMask
Definition: logging.h:131
std::string fSyslogPrefix
Definition: logging.h:130
int fDebugLevel
Definition: logging.h:134
const char * GetPrefix()
Definition: logging.h:90
void SetDebugMask(unsigned mask)
Definition: logging.h:70
int fSyslogLevel
Definition: logging.h:136
unsigned fFileMask
Definition: logging.h:133
static Logger * Instance()
Definition: logging.h:104
std::string fPrefix
Definition: logging.h:138
static void DisableLogReopen()
Definition: logging.cxx:405
void ShowStat(bool tofile=true)
Definition: logging.cxx:410
virtual void _DoCheckTimeout()
Definition: logging.cxx:384
unsigned fLogLimit
Definition: logging.h:142
FILE * fFile
Definition: logging.h:129
int fFileLevel
Definition: logging.h:135
void _ExtendLines(unsigned max)
Definition: logging.cxx:188
void SetDebugLevel(int level=0)
Definition: logging.cxx:170
int fLevel
Definition: logging.h:137
virtual void DoOutput(int level, const char *filename, unsigned linenumber, const char *funcname, const char *message)
Definition: logging.cxx:308
bool fLogFileModified
Definition: logging.h:141
int GetDebugLevel() const
Definition: logging.h:86
void SetFileMask(unsigned mask)
Definition: logging.h:78
static void Debug(int level, const char *filename, unsigned linenumber, const char *funcname, const char *message)
Definition: logging.h:106
unsigned fMaxLine
Definition: logging.h:127
Logger * fPrev
Definition: logging.h:125
static Logger * gDebug
Definition: logging.h:114
static void CheckTimeout()
Definition: logging.cxx:398
void _FillString(std::string &str, unsigned mask, LoggerEntry *entry)
Definition: logging.cxx:234
unsigned GetFileMask() const
Definition: logging.h:79
posix pthread mutex
Definition: threads.h:61
Event manipulation API.
Definition: api.h:23
void SetDebugLevel(int level=0)
Definition: logging.cxx:468
void SetDebugPrefix(const char *prefix=0)
Definition: logging.cxx:478
void SetFileLevel(int level=0)
Definition: logging.cxx:473
Logger * lgr()
Definition: logging.cxx:483