PTLib  Version 2.10.11
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  * $Revision: 24994 $
27  * $Author: rjongbloed $
28  * $Date: 2011-01-04 17:13:51 -0600 (Tue, 04 Jan 2011) $
29  */
30 
31 #ifndef _PSYSTEMLOG
32 #define _PSYSTEMLOG
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #include "ptlib/udpsock.h"
39 
40 class PSystemLogTarget;
41 
42 
47 class PSystemLog : public PObject, public iostream
48 {
49  PCLASSINFO(PSystemLog, PObject);
50  public:
53  enum Level {
56  StdError = -1,
64  Info,
77 
79  };
80 
82  PSystemLog(
83  Level level
84  );
85 
87  ~PSystemLog() { flush(); }
89 
94  static PSystemLogTarget & GetTarget();
95 
98  static void SetTarget(
99  PSystemLogTarget * target,
100  bool autoDelete = true
101  );
103 
104  private:
105  PSystemLog(const PSystemLog & other);
106  PSystemLog & operator=(const PSystemLog &);
107 
108  class Buffer : public streambuf {
109  public:
110  Buffer();
111  virtual int_type overflow(int_type=EOF);
112  virtual int_type underflow();
113  virtual int sync();
114  PSystemLog * m_log;
115  PString m_string;
116  } m_buffer;
117  friend class Buffer;
118 
119  Level m_logLevel;
120 
121  friend class PSystemLogTarget;
122 };
123 
124 
125 class PSystemLogTarget : public PObject
126 {
128  public:
133 
140  PSystemLog::Level level
141  ) { m_thresholdLevel = level; }
142 
148  PSystemLog::Level GetThresholdLevel() const { return m_thresholdLevel; }
150 
151  protected:
156  virtual void Output(
157  PSystemLog::Level level,
158  const char * msg
159  ) = 0;
160 
163  void OutputToStream(
164  ostream & strm,
165  PSystemLog::Level level,
166  const char * msg
167  );
169 
170  protected:
172 
173  private:
174  PSystemLogTarget(const PSystemLogTarget & other);
175  PSystemLogTarget & operator=(const PSystemLogTarget &);
176 
177  friend class PSystemLog::Buffer;
178 };
179 
180 
184 {
186  public:
187  virtual void Output(PSystemLog::Level, const char *)
188  {
189  }
190 };
191 
192 
196 {
198  public:
203  virtual void Output(
204  PSystemLog::Level level,
205  const char * msg
206  );
208 };
209 
210 
214 {
216  public:
220  const PString & filename
221  );
223 
228  virtual void Output(
229  PSystemLog::Level level,
230  const char * msg
231  );
233 
238  const PFilePath & GetFilePath() const { return m_file.GetFilePath(); }
240 
241  protected:
243 };
244 
245 
249 {
251  public:
252  enum { RFC3164_Port = 514 };
253 
257  const PIPSocket::Address & address,
258  WORD port = RFC3164_Port,
259  unsigned facility = 16
260  );
262  const PString & hostname,
263  WORD port = RFC3164_Port,
264  unsigned facility = 16
265  );
267 
272  virtual void Output(
273  PSystemLog::Level level,
274  const char * msg
275  );
277 
278  protected:
280  WORD m_port;
281  unsigned m_facility;
283 };
284 
285 
286 #ifdef WIN32
287 
289 class PSystemLogToDebug : public PSystemLogTarget
290 {
291  PCLASSINFO(PSystemLogToDebug, PSystemLogTarget);
292  public:
297  virtual void Output(
298  PSystemLog::Level level,
299  const char * msg
300  );
302 };
303 #elif !defined(P_VXWORKS)
304 
307 {
309  public:
315 
320  virtual void Output(
321  PSystemLog::Level level,
322  const char * msg
323  );
325 };
326 #endif
327 
328 
333 #define PSYSTEMLOG(level, variables) \
334  if (PSystemLog::GetTarget().GetThresholdLevel() >= PSystemLog::level) { \
335  PSystemLog P_systemlog(PSystemLog::level); \
336  P_systemlog << variables; \
337  } else (void)0
338 
339 
340 #endif
341 
342 
343 // End Of File ///////////////////////////////////////////////////////////////
Log a non-fatal error.
Definition: syslog.h:60
friend class PSystemLogTarget
Definition: syslog.h:121
Log a warning.
Definition: syslog.h:62
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:1049
This class abstracts the operating system dependent error logging facility.
Definition: syslog.h:47
const PFilePath & GetFilePath() const
Get the path to the file being logged to.
Definition: syslog.h:238
A socket channel that uses the UDP transport on the Internet Protocol.
Definition: udpsock.h:47
This class describes a full description for a file on the particular platform.
Definition: filepath.h:65
PSystemLog::Level m_thresholdLevel
Definition: syslog.h:171
Level
define the different error log levels
Definition: syslog.h:54
unsigned m_facility
Definition: syslog.h:281
friend class Buffer
Definition: syslog.h:117
Log from standard error stream.
Definition: syslog.h:56
PSystemLog::Level GetThresholdLevel() const
Get the current level for logging.
Definition: syslog.h:148
PTextFile m_file
Definition: syslog.h:242
Log even more debugging information.
Definition: syslog.h:70
Log a bucket load of debugging information.
Definition: syslog.h:76
Log system output to nowhere.
Definition: syslog.h:183
Log more debugging information.
Definition: syslog.h:68
Log system output to stderr.
Definition: syslog.h:195
Log system output to the network using RFC 3164 BSD syslog protocol.
Definition: syslog.h:248
Log a real lot of debugging information.
Definition: syslog.h:74
The character string class.
Definition: pstring.h:108
Log debugging information.
Definition: syslog.h:66
static PSystemLogTarget & GetTarget()
Get the current target/destination for system logging.
PSystemLog(Level level)
Create a system log stream.
~PSystemLog()
Destroy the string stream, deleting the stream buffer.
Definition: syslog.h:87
Log general information.
Definition: syslog.h:64
Definition: syslog.h:78
A class describing an IP address.
Definition: ipsock.h:75
PUDPSocket m_socket
Definition: syslog.h:282
Log a lot of debugging information.
Definition: syslog.h:72
Log system output to the Posix syslog() function.
Definition: syslog.h:306
Log system output to a file.
Definition: syslog.h:213
void SetThresholdLevel(PSystemLog::Level level)
Set the level at which errors are logged.
Definition: syslog.h:139
Definition: syslog.h:125
A class representing a a structured file that is portable accross CPU architectures.
Definition: textfile.h:49
static void SetTarget(PSystemLogTarget *target, bool autoDelete=true)
Set the current target/destination for system logging.
WORD m_port
Definition: syslog.h:280
Log a fatal error.
Definition: syslog.h:58
Ultimate parent class for all objects in the class library.
Definition: object.h:1118
virtual void Output(PSystemLog::Level, const char *)
Log an error into the system log.
Definition: syslog.h:187
PIPSocket::Address m_host
Definition: syslog.h:279