sipxportlib  Version 3.3
OsSysLog.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006-2013 SIPez LLC. All rights reserved.
3 //
4 // Copyright (C) 2004-2006 SIPfoundry Inc.
5 // Licensed by SIPfoundry under the LGPL license.
6 //
7 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
8 // Licensed to SIPfoundry under a Contributor Agreement.
9 //
10 // $$
12 
13 #ifndef _OsSysLog_h_
14 #define _OsSysLog_h_
15 
16 // SYSTEM INCLUDES
17 #include <stdarg.h>
18 // APPLICATION INCLUDES
19 #include <utl/UtlDefs.h>
20 #include <os/OsSysLogFacilities.h>
21 #include <os/OsSocket.h>
22 #include <os/OsDefs.h>
23 #include <os/OsStatus.h>
24 #include <os/OsTime.h>
25 #include <os/OsAtomics.h>
26 #include <os/OsTaskId.h>
27 
28 // DEFINES
29 #define SYSLOG_NUM_PRIORITIES 8 // Number of OsSysLogPriority entries
30 
31 // MACROS
32 // EXTERNAL FUNCTIONS
33 // EXTERNAL VARIABLES
34 // CONSTANTS
35 // STRUCTS
36 // ENUMS
37 
38 // TYPEDEFS
39 typedef enum tagOsSysLogPriority
40 {
41  PRI_DEBUG, // debug-level message
42  PRI_INFO, // informational message
43  PRI_NOTICE, // normal, but significant, condition
44  PRI_WARNING, // warning conditions
45  PRI_ERR, // error conditions
46  PRI_CRIT, // critical conditions
47  PRI_ALERT, // action must be taken immediately
48  PRI_EMERG // system is unusable
49 
50  // NOTE: If adding/removing priorities, please adjust static name
51  // initializer in OsSysLog.cpp and the SYSLOG_NUM_PRIORITIES define above.
52 
53  // NOTE: The levels are listed in priority order, so they can be
54  // compared with "<", etc.
55 
57  //:The priority of log messages ordered from lowest priority to
58  //:highest.
59  //
61  // be understandable by third party reviewers, however, are
62  // not informative/significant enought to be be an INFO. These
63  // messages tend to be high volume and disruptive to those who did
64  // not author the message.
65  // Example: Session reinvite received from party XXXX
67  // clearly inform the reviewer of routine and non-special event
68  // within the system. Generally, these events tend to only affect
69  // the closed system and not external systems/parties. The volume
70  // of INFO messages is also smaller than DEBUG messages.
71  // Example: Using codec XXXX for call YYYY
73  // expected events, however, are significant enough to warrant
74  // a closer review from administrator. These events may also
75  // involve an external system.
76  // Example: Replicating data to server XXXX
77  // Example: New User 'XXXX' added to database YYYY
79  // expected errors that are easily recoverable. A warning does
80  // not result in any loss of functionality or data.
81  // Example: Received older form of XXXX
83  // expected error is observed and some minor amount of
84  // functionality is lost.
85  // Example: Unknown/Invalid command received.
87  // where some major amount of functionality/data is lost, however,
88  // the system as a whole will continue to function.
89  // Example: Unable to commit record XXX to disk, index is invalid.
91  // errors or events which require immediate action before the
92  // situation grows worse. There is most likely functionality
93  // and/or data loss, however, the entire system may also fail.
94  // Example: Unable to create a new file; file system full.
96  // where the system will ultimately fail.
97  // Example: Memory corrupted; closing down.
98 
99 // Signature for a callback function that takes three parameters: priority,
100 // source Id of the generating module, and the message itself.
101 typedef void (*OsSysLogCallback)(const char* szPriority,
102  const char* szSource,
103  const char* szMsg);
104 
105 
106 // FORWARD DECLARATIONS
107 class OsSysLogTask;
108 class OsTimer;
109 
110 //:The OsSysLog provides a system wide logger and alternative to printf or
111 //:osPrintf for error/informational/debugging purposes.
112 //
113 // OsSysLog is designed to work on both the xpressa phone where fixed size
114 // circular buffer is needed along with server-type environments where a
115 // simple log is required.
116 //
117 // Daemon Setup
118 // ============
119 //
120 // Setting up a rolling log:
121 //
122 // To setup a rolling log, you need call the static initialize method
123 // followed by a call to setOutputFile. For example:
124 //
125 // OsSysLog::initialize(1024, "myXpressa3422") ;
126 //
127 // This tells OsSysLog to create log of up to 1024 entries and to use the
128 // string "myXpressa3422" to uniquely identify this process.
129 //
130 // OsSysLog::setOutputFile(90, "/flash0/syslog.txt") ;
131 //
132 // This tell OsSysLog to save the logged data to disk every 90 seconds into
133 // the file "/flash0/syslog.txt".
134 //
135 // Setting up a server-style unbounded log
136 //
137 // Like setting up a rolling log, to setup a unbounded log, you will need to
138 // call the static initialize method followed by a call to setOutputFile. For
139 // example:
140 //
141 // OsSysLog::initialize(0, "ServerXYZ") ;
142 //
143 // This instructs OsSysLog not to create an in-memory buffer and names the
144 // process "ServerXYZ". It is important that you specify "0" as the in-memory
145 // buffer size; otherwise, your log may limited in size.
146 //
147 // OsSysLog::setOutputFile(0, "/usr/var/myserverlog") ;
148 //
149 // This last statement instructs the server to write data immediately to the
150 // log located at "/usr/var/myserverlog".
151 //
152 // Usage
153 // =====
154 //
155 // Ideally, you should use the syslog method build ontop of OsTask. This will
156 // make sure that that task id, and task name are included as part of the
157 // log entry.
158 //
159 // syslog(FAC_HTTP, PRI_ERR, "The url '%s' is invalid", szUrl) ;
160 //
161 // Alternatively, you can also call static methods on OsSysLog. I would
162 // recommend using a OsTask if possible, however, if you are unable to
163 // reference an OsTask:
164 //
165 // OsSysLog::add(FAC_HTTP, PRI_ERR, "The url '%s' is invalid", szUrl) ;
166 //
167 // or
168 //
169 // OsSysLog::add("MyTaskName", taskId, FAC_HTTP, PRI_ERR,
170 // "The url '%s' is invalid", szUrl) ;
171 //
172 //
173 // The "FAC_HTTP" defines the facility or functional related to the log
174 // message. This helps users and reviewer filter and categorize different
175 // log messages. Please see OsSysLogFacilities.h for a list of available
176 // facilities. Please also feel free to add additional facilities.
177 //
178 // The "PRI_ERR" defines the priority of the log message. Please see the
179 // description above for the different priority levels.
180 //
181 class OsSysLog
182 {
183 /* //////////////////////////// PUBLIC //////////////////////////////////// */
184 public:
185 
186  static const char* sPriorityNames[] ;
187  //:List of Priority Names orders in the same order as OsSysLogPriority.
188  static const int sPriorityNamesNum;
189  //:Number of elements in sPriorityNames array;
190 
191  static const char* sFacilityNames[] ;
192  //:List of Facility Names orders in the same order as OsSysLogFacility.
193  static const int sFacilityNamesNum;
194  //:Number of elements in sFacilityNames array;
195 
197  {
198  OPT_NONE = 0x00000000, // No Options
199  OPT_SHARED_LOGFILE = 0x00000001 // Assume a shared log file
200 
201  // NOTE: Options are designed to be used as bitmasks (and ORed together).
202  // Make sure new additions are defined as power of twos (0x01,
203  // 0x02, 0x04, 0x08, 0x10, 0x10, 0x20, 0x40, ...)
204  } ;
205  //:Defines the set of possible options passed to the initialize method.
206  //
208  //
210  // multiple loggers (processes) to write to the same log file.
211  // This option should only be set if required to avoid
212  // performance hits.
213 
214 /* ============================ CREATORS ================================== */
215 
216 
217 /* ============================ MANIPULATORS ============================== */
218 
219  static OsStatus initialize(const int maxInMemoryLogEntries,
220  const char* processId,
221  const int options = OPT_NONE);
222  //:Initialize the sys log facility and the in-memory circular buffer
223  //:size.
224  // This must be called prior to invoking any of the set, add, or enable
225  // manipulators defined in this class.
226  //
228  // log entries stored at any one point. This setting may also
229  // impact file based output logging. If the minFlushPeriod is
230  // greater than 0, the output file will never grow larger than
231  // this value.
232  //
234  // that uniquely differentiates this process from other processes.
235  // Examples could range from a phones mac address or server's name.
236  //
238  // the OsSysLogOptions enum defined above for valid settings.
239 
240  static OsStatus shutdown() ;
241  //:Shutdown the OsSysLog task
242 
243  static OsStatus setOutputFile(const int minFlushPeriod,
244  const char* logfile);
245  //:Set an output file to collect logging results.
246  //
248  // pass before a log entries are flushed to the log file in
249  // seconds. Specifying a value of less than or equal to zero will
250  // force immediate writes. A positive value will limit only allow
251  // log writes periodically and will limit the log file size to the
252  // maxInMemoryLogEntries defined in the call to
253  // OsSysLog::initialize(maxInMemoryLogEntries).
255 
257  //:Set a callback function to collect logging results.
258  //
260  // strings as parameters: Logging priority, source
261  // of log entry, and the entry itself
262 
263  static OsStatus addOutputSocket(const char* remoteHost);
264  //:Add an output socket to the list of output targets.
265  // Log events are fired to output sockets on a low-priority thread and
266  // are not subject to minFlushPeriods such as file output targets.
267 
268  static OsStatus enableConsoleOutput(const UtlBoolean enable);
269  //:Enables or Disable console output for logging events.
270  // Log events are displayed on a low-priority thread and are not subject
271  // to minFlushPeriods as file output targets are.
272  //
274  // disable it. The default is disabled.
275 
276  static OsStatus setLoggingPriority(const OsSysLogPriority priority);
277  //:Sets the priority logging level.
278  // All log events of lesser priority are disgarded immediately. These
279  // events are not delievered to sockets, file logs, or the console.
280  //
281  // Resetting the global logging priority will clear any facility
282  // priorities.
283 
285  // should be logged.
286 
288  const OsSysLogPriority priority);
289  //:Set the priority logging level for a specific facility.
290  // A facilities's logging level overrides the global logging level. This
291  // allow developers to dynamically increase verbosity for a specific
292  // facility.
293  //
294  // Resetting the global logging priority will clear any facility
295  // priorities.
296  //
298  // either promoted or demoted.
300  // should be logged.
301 
302  static OsStatus add(const char* taskName,
303  const int taskId,
304  const OsSysLogFacility facility,
305  const OsSysLogPriority priority,
306  const char* format,
307  ...
308  )
309 #ifdef __GNUC__
310  // with the -Wformat switch, this enables format string checking
311  __attribute__ ((format (printf, 5, 6)))
312 #endif
313  ;
314  //:Adds an event to the sys log. If the sys log has not been
315  //:initialized, the message is printed to the console.
316  //
320  // event. See the OsSysLogFacility for more information.
322  // OsSysLogPriority for more information.
323 
324  static OsStatus add(const OsSysLogFacility facility,
325  const OsSysLogPriority priority,
326  const char* format,
327  ...)
328 #ifdef __GNUC__
329  // with the -Wformat switch, this enables format string checking
330  __attribute__ ((format (printf, 3, 4)))
331 #endif
332  ;
333  //:Adds an event to the sys log. If the sys log has not been
334  //:initialized, the message is printed to the console.
335  //
337  // event. See the OsSysLogFacility for more information.
339  // OsSysLogPriority for more information.
340 
341  static OsStatus vadd(const OsSysLogFacility facility,
342  const OsSysLogPriority priority,
343  const char* format,
344  va_list ap)
345 #ifdef __GNUC__
346  // with the -Wformat switch, this enables format string checking
347  __attribute__ ((format (printf, 3, 0)))
348 #endif
349  ;
350 
351  //:Adds an event to the sys log. If the sys log has not been
352  //:initialized, the message is printed to the console.
353  //
355  // event. See the OsSysLogFacility for more information.
357  // OsSysLogPriority for more information.
358 
359  static OsStatus vadd(const char* taskName,
360  const OsTaskId_t taskId,
361  const OsSysLogFacility facility,
362  const OsSysLogPriority priority,
363  const char* format,
364  va_list ap)
365 #ifdef __GNUC__
366  // with the -Wformat switch, this enables format string checking
367  __attribute__ ((format (printf, 5, 0)))
368 #endif
369  ;
370 
371  //:Adds an event to the sys log. If the sys log has not been
372  //:initialized, the message is printed to the console.
373  //
377  // event. See the OsSysLogFacility for more information.
379  // OsSysLogPriority for more information.
380 
381  static OsStatus clearInMemoryLog() ;
382  //:Purges all entries from the in memory log.
383  // If configured to using a flush period (setOutputFile), then entries
384  // entries will also be flushed from the file on next flush.
385 
386  static OsStatus flush(const OsTime& rTimeout = OsTime::OS_INFINITY) ;
387  //:Flushes the in-memory circular buffer log to disk or an unbounded
388  //:log file.
389 
390  static void initSysLog(const OsSysLogFacility facility,
391  const char* processID,
392  const char* logname,
393  const char* loglevel);
394  // Initialize the OsSysLog priority
395 
396 /* ============================ ACCESSORS ================================= */
397 
398  static OsStatus getMaxInMemoryLogEntries(int& maxEntries) ;
399  //:Obtains the maximum number of in-memory log entries.
400  // This value is specified as part of initialize() process and cannot
401  // be changed at runtime.
403 
404  static OsStatus tailMemoryLog(const int numEntries) ;
405  //:Displays the last numEntries log entries available within the
406  //:in-memory log buffer.
407  //
409  // the end (or tail) of the log. Specify a value of <= 0 to
410  // display the entire in-memory log contents.
411 
412  static OsStatus headMemoryLog(const int numEntries) ;
413  //:Displays the first numEntries log entries available within the
414  //:in-memory log buffer.
415  //
417  // the beginning (or head) of the log. Specify a value of <= 0
418  // to display the entire in-memory log contents.
419 
420  static OsStatus getLogEntries( const int maxEntries,
421  char* entries[],
422  int& actualEntries) ;
423  //:Gets the last <maxEntries> log entries ordered with the most recent
424  //:entry first.
427  // maxEntries entries. It is the caller responsibility to free
428  // all of the char* pointers.
430  // will always be less than or equal to maxEntries.
431 
432  static OsStatus parseLogString(const char *szSource,
433  UtlString& date,
434  UtlString& eventCount,
435  UtlString& facility,
436  UtlString& priority,
437  UtlString& hostname,
438  UtlString& taskname,
439  UtlString& taskId,
440  UtlString& processId,
441  UtlString& content) ;
442  //:Parses a log string into its parts.
443 
444  static OsStatus getPriorityName(OsSysLogPriority priorityId, UtlString& name);
445  //: Get the string name for the given priority level
446 
447  static OsStatus getPriorityForName(const UtlString& name, OsSysLogPriority& priorityId);
448  //: Get the priority level for the given string (case sensitive)
449 
450 /* ============================ INQUIRY =================================== */
451 
453  //:Return the logging level priority.
454  // All log events added with a lower priority are discarded.
455 
457  //:Return the logging level for a specific facility.
458  // A facilities's logging level overrides the global logging level. This
459  // allow developers to dynamically increase verbosity for a specific
460  // facility.
461 
462  static UtlBoolean willLog(OsSysLogFacility facility, OsSysLogPriority priority) ;
463  //:Determine if a message of a given facility/priority will be logged or
464  //:digarded.
465 
466  static int getNumFacilities();
467  //:Return the number of available facilities.
468 
469  static OsTimer* getTimer();
470 
471  static UtlBoolean isTaskPtrNull() { return(spOsSysLogTask.load() == NULL); };
472 
473 /* //////////////////////////// PROTECTED ///////////////////////////////// */
474 protected:
475  static OsAtomicLightPtr<OsSysLogTask> spOsSysLogTask;
476 
483 
484  OsSysLog(const OsSysLog& rOsSysLog);
485  //:Copy constructor
486 
487  OsSysLog& operator=(const OsSysLog& rhs);
488  //:Assignment operator
489 
490  OsSysLog();
491  //:Default constructor
492 
493  virtual ~OsSysLog();
494  //:Destructor
495 
496  static UtlString escape(const UtlString& source) ;
497  //:Escapes Quotes and CrLfs within a string
498 
499  static UtlString unescape(const UtlString& source) ;
500  //:Unescapes previously escaped Quotes and CrLfs
501 
502  static void getTaskInfo(UtlString& taskName, OsTaskId_t& taskId);
503  //:Get current task name and id
504 
505 /* //////////////////////////// PRIVATE /////////////////////////////////// */
506 private:
507 
509  static void initializePriorities() ;
510 
511 };
512 
514 {
515 public:
516  OsStackTraceLogger(const OsSysLogFacility facility,
517  const OsSysLogPriority priority,
518  const char* methodName);
519  OsStackTraceLogger(const OsSysLogFacility facility,
520  const OsSysLogPriority priority,
521  const char* methodName,
522  const OsStackTraceLogger& oneBackInStack);
524 private:
525  // disallow copy
527  mFacility(FAC_LOG),
528  mPriority(PRI_ERR)
529  {
530  // should never get here
531  }
532 
533  // disallow assignment
535  {
536  // should never get here
537  return *this; // avoid compiler warning
538  }
539 
540  UtlString mMethodName;
541  const OsSysLogFacility mFacility;
542  const OsSysLogPriority mPriority;
543  int mTid;
544 };
545 
546 /* ============================ INLINE METHODS ============================ */
547 
548 #endif // _OsSysLog_h_
static OsStatus tailMemoryLog(const int numEntries)
param maxEntries - The maximum number of in-memory log entries
Definition: OsSysLog.cpp:585
Definition: OsSysLogTask.h:45
static OsStatus flush(const OsTime &rTimeout=OsTime::OS_INFINITY)
Definition: OsSysLog.cpp:505
static OsSysLogPriority getLoggingPriority()
Definition: OsSysLog.cpp:773
enum tagOsSysLogFacility OsSysLogFacility
enumcode: FAC_PERF - performance related enumcode: FAC_KERNEL - kernel/os related enumcode: FAC_AUTH ...
Definition: OsSysLogFacilities.h:140
static OsSysLogPriority sLoggingPriority
Definition: OsSysLog.h:479
Definition: OsSysLogFacilities.h:82
static OsStatus getPriorityForName(const UtlString &name, OsSysLogPriority &priorityId)
Definition: OsSysLog.cpp:753
static OsStatus getMaxInMemoryLogEntries(int &maxEntries)
Definition: OsSysLog.cpp:569
static OsStatus clearInMemoryLog()
param: taskName - The name of the task if available. param: taskId - The TaskID of the task if availa...
Definition: OsSysLog.cpp:489
static OsStatus vadd(const OsSysLogFacility facility, const OsSysLogPriority priority, const char *format, va_list ap)
param: facility - Defines the facility responsible for adding the
Definition: OsSysLog.cpp:393
static UtlString sProcessId
Definition: OsSysLog.h:480
static const int sFacilityNamesNum
Definition: OsSysLog.h:193
Definition: OsSysLog.h:42
static const char * sFacilityNames[]
Definition: OsSysLog.h:191
OsStatus
Definition: OsStatus.h:27
#define NULL
Definition: UtlDefs.h:29
Definition: OsSysLogFacilities.h:52
static UtlBoolean bPrioritiesInitialized
Definition: OsSysLog.h:482
static UtlBoolean isTaskPtrNull()
Definition: OsSysLog.h:471
static OsSysLogPriority getLoggingPriorityForFacility(const OsSysLogFacility facility)
Definition: OsSysLog.cpp:780
static OsAtomicULong sEventCount
Definition: OsSysLog.h:477
static OsStatus parseLogString(const char *szSource, UtlString &date, UtlString &eventCount, UtlString &facility, UtlString &priority, UtlString &hostname, UtlString &taskname, UtlString &taskId, UtlString &processId, UtlString &content)
param: maxEntries - The maximum number of entries to fetch. param: entries - Array of char* large eno...
Definition: OsSysLog.cpp:638
static const int sPriorityNamesNum
Definition: OsSysLog.h:188
static UtlString sHostname
Definition: OsSysLog.h:481
static void initSysLog(const OsSysLogFacility facility, const char *processID, const char *logname, const char *loglevel)
Definition: OsSysLog.cpp:520
Definition: OsSysLog.h:181
static OsStatus headMemoryLog(const int numEntries)
param: numEntries - The number of log entries display starting from
Definition: OsSysLog.cpp:602
Definition: OsSysLog.h:199
Definition: OsAtomics.h:47
OsSysLogOptions
Definition: OsSysLog.h:196
static OsAtomicLightPtr< OsSysLogTask > spOsSysLogTask
Definition: OsSysLog.h:471
static void getTaskInfo(UtlString &taskName, OsTaskId_t &taskId)
Definition: OsSysLog.cpp:344
Definition: UtlString.h:48
static OsStatus setOutputFile(const int minFlushPeriod, const char *logfile)
Definition: OsSysLog.cpp:164
Definition: OsSysLog.h:46
static OsStatus addOutputSocket(const char *remoteHost)
param pCallback - Pointer to a callback function that takes three
Definition: OsSysLog.cpp:213
static int getNumFacilities()
Definition: OsSysLog.cpp:815
static UtlBoolean willLog(OsSysLogFacility facility, OsSysLogPriority priority)
Definition: OsSysLog.cpp:793
Definition: OsTime.h:45
tagOsSysLogPriority
Definition: OsSysLog.h:39
static OsStatus setCallbackFunction(OsSysLogCallback pCallback)
param minFlushPeriod - Defines the minimum amount of that that must
Definition: OsSysLog.cpp:193
enum tagOsSysLogPriority OsSysLogPriority
static UtlString escape(const UtlString &source)
Definition: OsSysLog.cpp:849
Definition: OsSysLog.h:513
Definition: OsSysLog.h:44
static OsTimer * getTimer()
Definition: OsSysLog.cpp:157
static OsStatus initialize(const int maxInMemoryLogEntries, const char *processId, const int options=OPT_NONE)
enumcode: OPT_NONE - No Extra Options
Definition: OsSysLog.cpp:117
OsSysLog()
Definition: OsSysLog.cpp:834
Definition: OsTime.h:37
Definition: OsSysLog.h:198
Definition: OsSysLog.h:45
Definition: OsTimer.h:105
OsSysLog & operator=(const OsSysLog &rhs)
Definition: OsSysLog.cpp:825
int UtlBoolean
Definition: UtlDefs.h:41
static OsStatus getPriorityName(OsSysLogPriority priorityId, UtlString &name)
Definition: OsSysLog.cpp:741
Definition: OsSysLog.h:48
void(* OsSysLogCallback)(const char *szPriority, const char *szSource, const char *szMsg)
enumcode: LOG_DEBUG - Debug-level message. Debug level messages should
Definition: OsSysLog.h:101
virtual ~OsSysLog()
Definition: OsSysLog.cpp:844
static OsStatus getLogEntries(const int maxEntries, char *entries[], int &actualEntries)
param: numEntries - The number of log entries display starting from
Definition: OsSysLog.cpp:619
static OsStatus setLoggingPriorityForFacility(const OsSysLogFacility facility, const OsSysLogPriority priority)
param priority - Defines the minimum priority level of log events that
Definition: OsSysLog.cpp:270
static OsStatus shutdown()
param maxInMemoryLogEntries - Defines the maximum number of in-memory
Definition: OsSysLog.cpp:142
static OsSysLogPriority spPriorities[FAC_MAX_FACILITY]
Definition: OsSysLog.h:478
static OsStatus setLoggingPriority(const OsSysLogPriority priority)
param enable - Specify TRUE to enable console logging or FALSE to
Definition: OsSysLog.cpp:248
static OsStatus add(const char *taskName, const int taskId, const OsSysLogFacility facility, const OsSysLogPriority priority, const char *format,...)
param facility - Defines the facility whose logging events should be
Definition: OsSysLog.cpp:298
static OsStatus enableConsoleOutput(const UtlBoolean enable)
Definition: OsSysLog.cpp:230
static UtlString unescape(const UtlString &source)
Definition: OsSysLog.cpp:925
Definition: OsSysLog.h:43
static const char * sPriorityNames[]
Definition: OsSysLog.h:186
Definition: OsSysLog.h:47
Definition: OsSysLog.h:41