libDwm-0.6.0
DwmFileLogger.hh
Go to the documentation of this file.
1 //===========================================================================
2 // @(#) $DwmPath: dwm/libDwm/tags/libDwm-0.6.0/include/DwmFileLogger.hh 8401 $
3 // @(#) $Id: DwmFileLogger.hh 8401 2016-04-17 06:44:31Z dwm $
4 //===========================================================================
5 // Copyright (c) Daniel W. McRobb 2007, 2016
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 // 3. The names of the authors and copyright holders may not be used to
18 // endorse or promote products derived from this software without
19 // specific prior written permission.
20 //
21 // IN NO EVENT SHALL DANIEL W. MCROBB BE LIABLE TO ANY PARTY FOR
22 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
23 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE,
24 // EVEN IF DANIEL W. MCROBB HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
25 // DAMAGE.
26 //
27 // THE SOFTWARE PROVIDED HEREIN IS ON AN "AS IS" BASIS, AND
28 // DANIEL W. MCROBB HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
29 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. DANIEL W. MCROBB MAKES NO
30 // REPRESENTATIONS AND EXTENDS NO WARRANTIES OF ANY KIND, EITHER
31 // IMPLIED OR EXPRESS, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE,
33 // OR THAT THE USE OF THIS SOFTWARE WILL NOT INFRINGE ANY PATENT,
34 // TRADEMARK OR OTHER RIGHTS.
35 //===========================================================================
36 
37 //---------------------------------------------------------------------------
40 //---------------------------------------------------------------------------
41 
42 #ifndef _DWMFILELOGGER_HH_
43 #define _DWMFILELOGGER_HH_
44 
45 extern "C" {
46 #include <inttypes.h>
47 #include <stdio.h>
48 #include <syslog.h>
49 }
50 
51 #include <map>
52 #include <mutex>
53 #include <string>
54 
55 namespace Dwm {
56 
57  //----------------------------------------------------------------------
73  //----------------------------------------------------------------------
74  class FileLogger
75  {
76  public:
77  typedef enum {
78  e_notBuffered = _IONBF,
79  e_lineBuffered = _IOLBF,
80  e_blockBuffered = _IOFBF
81  } BufferingType;
82 
83  static const uint32_t k_defaultRolloverSize = 100 * 1024;
84  static const uint32_t k_defaultNumRolloverFiles = 1;
85 
86  //--------------------------------------------------------------------
88  //--------------------------------------------------------------------
89  FileLogger();
90 
91  //--------------------------------------------------------------------
93  //--------------------------------------------------------------------
94  ~FileLogger();
95 
96  //--------------------------------------------------------------------
107  //--------------------------------------------------------------------
108  bool Open(const std::string & ident, int logopt,
109  const std::string & filename,
110  BufferingType bufferingType = e_blockBuffered);
111 
112  //--------------------------------------------------------------------
114  //--------------------------------------------------------------------
115  bool IsOpen() const;
116 
117  //--------------------------------------------------------------------
119  //--------------------------------------------------------------------
120  bool Close();
121 
122  //--------------------------------------------------------------------
126  //--------------------------------------------------------------------
127  bool Log(int priority, const char * message, ...);
128 
129  //--------------------------------------------------------------------
133  //--------------------------------------------------------------------
134  bool Log(const std::string & filename, int lineno,
135  const std::string & function, int priority,
136  const char * message, ...);
137 
138  //--------------------------------------------------------------------
141  //--------------------------------------------------------------------
142  bool VaLog(int priority, const std::string & message,
143  va_list & vaList);
144 
145  //--------------------------------------------------------------------
150  //--------------------------------------------------------------------
151  bool VaLog(const std::string & filename, int lineno,
152  const std::string & function, int priority,
153  const std::string & message, va_list & vaList);
154 
155  //--------------------------------------------------------------------
158  //--------------------------------------------------------------------
159  bool Flush();
160 
161  //--------------------------------------------------------------------
163  //--------------------------------------------------------------------
164  int MinimumPriority() const;
165 
166  //--------------------------------------------------------------------
170  //--------------------------------------------------------------------
171  int MinimumPriority(int minimumPriority);
172 
173  //--------------------------------------------------------------------
176  //--------------------------------------------------------------------
177  bool ShowPriorities() const;
178 
179  //--------------------------------------------------------------------
187  //--------------------------------------------------------------------
188  bool ShowPriorities(bool showPriorities);
189 
190  //--------------------------------------------------------------------
192  //--------------------------------------------------------------------
193  uint32_t RolloverSize() const;
194 
195  //--------------------------------------------------------------------
199  //--------------------------------------------------------------------
200  bool ShowFunction() const;
201 
202  //--------------------------------------------------------------------
208  //--------------------------------------------------------------------
209  bool ShowFunction(bool showFunction);
210 
211  //--------------------------------------------------------------------
214  //--------------------------------------------------------------------
215  bool ShowFileLocation() const;
216 
217  //--------------------------------------------------------------------
223  //--------------------------------------------------------------------
224  bool ShowFileLocation(bool showFileLocation);
225 
226  //--------------------------------------------------------------------
230  //--------------------------------------------------------------------
231  uint32_t RolloverSize(uint32_t rolloverSize);
232 
233  //--------------------------------------------------------------------
235  //--------------------------------------------------------------------
236  uint32_t NumRolloverFiles() const;
237 
238  //--------------------------------------------------------------------
240  //--------------------------------------------------------------------
241  uint32_t NumRolloverFiles(uint32_t numRolloverFiles);
242 
243  //--------------------------------------------------------------------
247  //--------------------------------------------------------------------
248  bool SetPriorities(const std::map<int, std::pair<std::string,std::string> > & priorities);
249 
250  //------------------------------------------------------------------------
253  //------------------------------------------------------------------------
254  std::string PriorityTag(int priority);
255 
256  private:
257  bool _isOpen;
258  int _logOptions;
259  int _minimumPriority;
260  std::string _ident;
261  bool _showPriorities;
262  bool _showFunction;
263  bool _showFileLocation;
264  std::string _hostName;
265  std::string _filePrefix;
266  FILE *_file;
267  off_t _rolloverSize;
268  uint32_t _numRolloverFiles;
269  off_t _currentFilePosition;
270  BufferingType _bufferingType;
271  std::map<int,std::string> _priorityTags;
272  std::map<std::string,int> _priorityNames;
273  mutable std::mutex _mutex;
274 
275  bool Lock() const;
276  bool Unlock() const;
277  bool CloseNoLock();
278  bool OpenNoLock(const std::string & ident, int logopt,
279  const std::string & filename,
280  BufferingType bufferingType = e_blockBuffered);
281  bool OpenFile(const std::string & filename);
282  bool RollFilesOver();
283  void InitializePriorities();
284  };
285 
286 } // namespace Dwm
287 
288 #endif // _DWMFILELOGGER_HH_
289 
290 //---------------------------- emacs settings -----------------------------
291 // Local Variables:
292 // mode: C++/la
293 // tab-width: 2
294 // indent-tabs-mode: nil
295 // c-basic-offset: 2
296 // End:
297 //-------------------------------------------------------------------------
int MinimumPriority() const
Returns the lowest message priority that the logger will log.
bool SetPriorities(const std::map< int, std::pair< std::string, std::string > > &priorities)
Overrides the standard priority tags and names.
bool ShowFunction() const
If the logger is set to show function information in Log(const string &, int, const string &...
bool Log(int priority, const char *message,...)
Just like syslog(), takes a priority and a format string and variable list of arguments.
bool IsOpen() const
Returns true if the logger is open, else returns false.
uint32_t NumRolloverFiles() const
Returns the number of rollover files used by the logger.
bool VaLog(int priority, const std::string &message, va_list &vaList)
Just like vsyslog(), takes a priority and a format string and a va_list.
Definition: DwmBZ2IO.hh:67
bool ShowFileLocation() const
If the logger is set to show file location information, returns true.
This class encapsulates syslog-like logging, but uses files instead of the syslog facility...
Definition: DwmFileLogger.hh:74
uint32_t RolloverSize() const
Returns the current rollover size used by the logger.
FileLogger()
Constructor.
bool Flush()
Writes buffered data to the output log (i.e.
~FileLogger()
Destructor. Closes the log if it&#39;s open.
bool Open(const std::string &ident, int logopt, const std::string &filename, BufferingType bufferingType=e_blockBuffered)
Opens the logger.
bool Close()
Closes the logger.
bool ShowPriorities() const
If the logger is set to show priorities in each log message, returns true.
std::string PriorityTag(int priority)
Returns the priority tag ("[F]", "[W]", et.