PTLib  Version 2.14.3
 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: 32174 $
27  * $Author: rjongbloed $
28  * $Date: 2014-06-19 11:50:57 +1000 (Thu, 19 Jun 2014) $
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 std::ostream
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 PFilePath & 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(); }
251 
254  bool Clear();
255 
265  struct RotateInfo
266  {
267  RotateInfo(const PDirectory & dir);
268 
269  bool CanRotate() const
270  {
271  return m_maxSize > 0 && !m_timestamp.IsEmpty();
272  }
273 
278  off_t m_maxSize;
279  unsigned m_maxFileCount;
281  };
282 
285  virtual void SetRotateInfo(
286  const RotateInfo & info,
287  bool force = false
288  );
289 
292  const RotateInfo & GetRotateInfo() const { return m_rotateInfo; }
293 
301  virtual bool Rotate(
302  bool force
303  );
305 
306  protected:
307  bool InternalOpen();
308 
312 };
313 
314 
318 {
320  public:
321  enum { RFC3164_Port = 514 };
322 
326  const PIPSocket::Address & address,
327  WORD port = RFC3164_Port,
328  unsigned facility = 16
329  );
331  const PString & server,
332  WORD port = RFC3164_Port,
333  unsigned facility = 16
334  );
336 
341  virtual void Output(
342  PSystemLog::Level level,
343  const char * msg
344  );
346 
347  const PIPSocket::AddressAndPort & GetServer() const { return m_server; }
348 
349  protected:
351  unsigned m_facility;
353 };
354 
355 
356 #ifdef WIN32
357 
359 class PSystemLogToDebug : public PSystemLogTarget
360 {
361  PCLASSINFO(PSystemLogToDebug, PSystemLogTarget);
362  public:
367  virtual void Output(
368  PSystemLog::Level level,
369  const char * msg
370  );
372 };
373 #elif !defined(P_VXWORKS)
374 
377 {
379  public:
383  const char * ident = NULL,
384  int priority = -1,
386  int options = -1,
387  int facility = -1
388  );
389 
392 
397  virtual void Output(
398  PSystemLog::Level level,
399  const char * msg
400  );
402 
403  protected:
406 };
407 #endif
408 
409 
414 #define PSYSTEMLOG(level, variables) \
415  if (PSystemLog::GetTarget().GetThresholdLevel() >= PSystemLog::level) { \
416  PSystemLog P_systemlog(PSystemLog::level); \
417  P_systemlog << variables; \
418  } else (void)0
419 
420 
421 #endif
422 
423 
424 // End Of File ///////////////////////////////////////////////////////////////