PTLib  Version 2.12.9
 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  * $Revision: 28920 $
27  * $Author: rjongbloed $
28  * $Date: 2013-01-18 11:01:31 +1100 (Fri, 18 Jan 2013) $
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 P_IOSTREAM
48 {
49  PCLASSINFO(PSystemLog, PObject);
50  public:
53 
54  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  );
90 
91  private:
92  PSystemLog(const PSystemLog & other);
93  PSystemLog & operator=(const PSystemLog &);
94 
95  class Buffer : public streambuf {
96  public:
97  Buffer();
98  virtual int_type overflow(int_type=EOF);
99  virtual int_type underflow();
100  virtual int sync();
101  PSystemLog * m_log;
102  PCharArray m_string;
103  } m_buffer;
104  friend class Buffer;
105 
106  Level m_logLevel;
107 
108  friend class PSystemLogTarget;
109 };
110 
111 
112 class PSystemLogTarget : public PObject
113 {
114  PCLASSINFO(PSystemLogTarget, PObject);
115  public:
120 
127  PSystemLog::Level level
128  ) { m_thresholdLevel = level; }
129 
135  PSystemLog::Level GetThresholdLevel() const { return m_thresholdLevel; }
137 
138  protected:
143  virtual void Output(
144  PSystemLog::Level level,
145  const char * msg
146  ) = 0;
147 
150  void OutputToStream(
151  ostream & strm,
152  PSystemLog::Level level,
153  const char * msg
154  );
156 
157  protected:
158  PSystemLog::Level m_thresholdLevel;
159 
160  private:
161  PSystemLogTarget(const PSystemLogTarget & other);
162  PSystemLogTarget & operator=(const PSystemLogTarget &);
163 
164  friend class PSystemLog::Buffer;
165 };
166 
167 
171 {
173  public:
174  virtual void Output(PSystemLog::Level, const char *)
175  {
176  }
177 };
178 
179 
183 {
185  public:
190  virtual void Output(
191  PSystemLog::Level level,
192  const char * msg
193  );
195 };
196 
197 
198 #if PTRACING
199 
201 class PSystemLogToTrace : public PSystemLogTarget
202 {
203  PCLASSINFO(PSystemLogToTrace, PSystemLogTarget);
204  public:
207  PSystemLogToTrace();
209 
214  virtual void Output(
215  PSystemLog::Level level,
216  const char * msg
217  );
219 };
220 #endif
221 
222 
226 {
227  PCLASSINFO(PSystemLogToFile, PSystemLogTarget);
228  public:
232  const PString & filename
233  );
235 
240  virtual void Output(
241  PSystemLog::Level level,
242  const char * msg
243  );
245 
250  const PFilePath & GetFilePath() const { return m_file.GetFilePath(); }
252 
253  protected:
255 };
256 
257 
261 {
263  public:
264  enum { RFC3164_Port = 514 };
265 
269  const PIPSocket::Address & address,
270  WORD port = RFC3164_Port,
271  unsigned facility = 16
272  );
274  const PString & server,
275  WORD port = RFC3164_Port,
276  unsigned facility = 16
277  );
279 
284  virtual void Output(
285  PSystemLog::Level level,
286  const char * msg
287  );
289 
290  protected:
292  unsigned m_facility;
294 };
295 
296 
297 #ifdef WIN32
298 
300 class PSystemLogToDebug : public PSystemLogTarget
301 {
302  PCLASSINFO(PSystemLogToDebug, PSystemLogTarget);
303  public:
308  virtual void Output(
309  PSystemLog::Level level,
310  const char * msg
311  );
313 };
314 #elif !defined(P_VXWORKS)
315 
318 {
320  public:
324  const char * ident = NULL,
325  int priority = -1,
327  int options = -1,
328  int facility = -1
329  );
330 
333 
338  virtual void Output(
339  PSystemLog::Level level,
340  const char * msg
341  );
343 
344  protected:
347 };
348 #endif
349 
350 
355 #define PSYSTEMLOG(level, variables) \
356  if (PSystemLog::GetTarget().GetThresholdLevel() >= PSystemLog::level) { \
357  PSystemLog P_systemlog(PSystemLog::level); \
358  P_systemlog << variables; \
359  } else (void)0
360 
361 
362 #endif
363 
364 
365 // End Of File ///////////////////////////////////////////////////////////////