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