PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
syslog.h
Go to the documentation of this file.
1 /*
2  * syslog.h
3  *
4  * System Logging class.
5  *
6  * Portable Tools Library
7  *
8  * Copyright (c) 2009 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef _PSYSTEMLOG
28 #define _PSYSTEMLOG
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include "ptlib_config.h"
35 
36 #if P_SYSTEMLOG
37 
38 #include "ptlib/udpsock.h"
39 
40 class PSystemLogTarget;
41 
42 
47 class PSystemLog : public PObject, public std::ostream
48 {
49  PCLASSINFO(PSystemLog, PObject);
50  public:
53  P_DECLARE_ENUM_EX(Level,NumLogLevels,
55  StdError,-1,
56  Fatal,
57  Error,
58  Warning,
59  Info,
60  Debug,
61  Debug2,
62  Debug3,
63  Debug4,
64  Debug5,
65  Debug6
66  );
67 
69  PSystemLog(
70  Level level = NumLogLevels
71  );
72 
74  ~PSystemLog() { flush(); }
76 
81  static PSystemLogTarget & GetTarget();
82 
85  static void SetTarget(
86  PSystemLogTarget * target,
87  bool autoDelete = true
88  );
89 
91  static void OutputToTarget(
92  PSystemLog::Level level,
93  const char * msg
94  );
96 
97  private:
98  PSystemLog(const PSystemLog & other);
99  PSystemLog & operator=(const PSystemLog &);
100 
101  class Buffer : public streambuf {
102  public:
103  Buffer();
104  virtual int_type overflow(int_type=EOF);
105  virtual int_type underflow();
106  virtual int sync();
107  PSystemLog * m_log;
108  PCharArray m_string;
109  } m_buffer;
110  friend class Buffer;
111 
112  Level m_logLevel;
113 
114  friend class PSystemLogTarget;
115 };
116 
117 
118 class PSystemLogTarget : public PObject
119 {
120  PCLASSINFO(PSystemLogTarget, PObject);
121  public:
124  PSystemLogTarget();
126 
132  void SetThresholdLevel(
133  PSystemLog::Level level
134  ) { m_thresholdLevel = level; }
135 
141  PSystemLog::Level GetThresholdLevel() const { return m_thresholdLevel; }
142 
145  void SetOutputLevelName(
146  bool flag
147  ) { m_outputLevelName = flag; }
148 
151  bool GetOutputLevelName() const { return m_outputLevelName; }
153 
154  protected:
159  virtual void Output(
160  PSystemLog::Level level,
161  const char * msg
162  ) = 0;
163 
166  void OutputToStream(
167  ostream & strm,
168  PSystemLog::Level level,
169  const char * msg,
170  int timeZone = PTime::Local
171  );
173 
174  protected:
175  PAtomicEnum<PSystemLog::Level> m_thresholdLevel;
176  bool m_outputLevelName;
177 
178  private:
179  PSystemLogTarget(const PSystemLogTarget & other);
180  PSystemLogTarget & operator=(const PSystemLogTarget &);
181 
182  friend void PSystemLog::OutputToTarget(PSystemLog::Level level, const char * msg);
183 };
184 
185 
188 class PSystemLogToNowhere : public PSystemLogTarget
189 {
190  PCLASSINFO(PSystemLogToNowhere, PSystemLogTarget);
191  public:
192  virtual void Output(PSystemLog::Level, const char *)
193  {
194  }
195 };
196 
197 
200 class PSystemLogToStderr : public PSystemLogTarget
201 {
202  PCLASSINFO(PSystemLogToStderr, PSystemLogTarget);
203  public:
208  virtual void Output(
209  PSystemLog::Level level,
210  const char * msg
211  );
213 };
214 
215 
216 #if PTRACING
217 
219 class PSystemLogToTrace : public PSystemLogTarget
220 {
221  PCLASSINFO(PSystemLogToTrace, PSystemLogTarget);
222  public:
225  PSystemLogToTrace();
227 
232  virtual void Output(
233  PSystemLog::Level level,
234  const char * msg
235  );
237 };
238 #endif
239 
240 
243 class PSystemLogToFile : public PSystemLogTarget
244 {
245  PCLASSINFO(PSystemLogToFile, PSystemLogTarget);
246  public:
249  PSystemLogToFile(
250  const PFilePath & filename
251  );
253 
258  virtual void Output(
259  PSystemLog::Level level,
260  const char * msg
261  );
263 
268  const PFilePath & GetFilePath() const { return m_file.GetFilePath(); }
269 
272  bool Clear();
273 
274  struct RotateInfo : PFile::RotateInfo
275  {
276  RotateInfo(
277  const PDirectory & dir = PDirectory(),
278  const PString & prefix = PString::Empty(), // If empty, uses PProcess::Current().GetName()
279  const PString & suffix = PString::Empty(),
280  const PString & timestamp = DefaultTimestamp()
281  ) : PFile::RotateInfo(dir, prefix, suffix, timestamp, 0) { }
282  virtual void OnCloseFile(PFile & file, const PFilePath & rotatedTo);
283  virtual bool OnOpenFile(PFile & file);
284  virtual void OnMessage(bool error, const PString & msg);
285  };
286 
289  virtual void SetRotateInfo(
290  const RotateInfo & info,
291  bool force = false
292  );
293 
296  const RotateInfo & GetRotateInfo() const { return m_rotateInfo; }
297 
305  virtual bool Rotate(
306  bool force
307  );
309 
310  protected:
311  PTextFile m_file;
312  RotateInfo m_rotateInfo;
313  PMutex m_mutex;
314  bool m_outputting;
315 };
316 
317 
320 class PSystemLogToNetwork : public PSystemLogTarget
321 {
322  PCLASSINFO(PSystemLogToNetwork, PSystemLogTarget);
323  public:
324  enum { RFC3164_Port = 514 };
325 
328  PSystemLogToNetwork(
329  const PIPSocket::Address & address,
330  WORD port = RFC3164_Port,
331  unsigned facility = 16
332  );
333  PSystemLogToNetwork(
334  const PString & server,
335  WORD port = RFC3164_Port,
336  unsigned facility = 16
337  );
339 
344  virtual void Output(
345  PSystemLog::Level level,
346  const char * msg
347  );
349 
350  const PIPSocket::AddressAndPort & GetServer() const { return m_server; }
351 
352  protected:
353  PIPSocket::AddressAndPort m_server;
354  unsigned m_facility;
355  PUDPSocket m_socket;
356 };
357 
358 
359 #ifdef WIN32
360 
362 class PSystemLogToDebug : public PSystemLogTarget
363 {
364  PCLASSINFO(PSystemLogToDebug, PSystemLogTarget);
365  public:
370  virtual void Output(
371  PSystemLog::Level level,
372  const char * msg
373  );
375 };
376 #elif !defined(P_VXWORKS)
377 #define P_SYSTEMLOG_TO_SYSLOG 1
378 
380 class PSystemLogToSyslog : public PSystemLogTarget
381 {
382  PCLASSINFO(PSystemLogToSyslog, PSystemLogTarget);
383  public:
386  PSystemLogToSyslog(
387  const char * ident = NULL,
388  int priority = -1,
390  int options = -1,
391  int facility = -1
392  );
393 
394  ~PSystemLogToSyslog();
396 
401  virtual void Output(
402  PSystemLog::Level level,
403  const char * msg
404  );
406 
407  protected:
408  PString m_ident;
409  int m_priority;
410 };
411 #endif
412 
413 
418 #define PSYSTEMLOG(level, variables) \
419  if (PSystemLog::GetTarget().GetThresholdLevel() >= PSystemLog::level) { \
420  PSystemLog P_systemlog(PSystemLog::level); \
421  P_systemlog << variables; \
422  P_systemlog.width(-12345678); \
423  } else (void)0
424 
425 
426 #endif
427 
428 
429 #endif //P_SYSTEMLOG
430 
431 // End Of File ///////////////////////////////////////////////////////////////
This class represents a disk file.
Definition: file.h:57
Array of characters.
Definition: array.h:552
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
Local Time.
Definition: ptime.h:65
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:42
Information on how to rotate files.
Definition: file.h:587
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
A class describing an IP address and port number combination.
Definition: ipsock.h:278
Class to represent a directory in the operating system file system.
Definition: pdirect.h:173
The character string class.
Definition: pstring.h:108
This class defines a thread mutual exclusion object.
Definition: mutex.h:101
static const PString & Empty()
Return an empty string.
A class describing an IP address.
Definition: ipsock.h:59
#define P_DECLARE_ENUM_EX(name, countName, firstName, firstValue,...)
This declares a standard enumeration (enum) of symbols with ++ and – operators.
Definition: object.h:229
A class representing a a structured file that is portable accross CPU architectures.
Definition: textfile.h:45
Definition: atomic.h:238
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
Definition: xmpp.h:484