reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Log.hxx
Go to the documentation of this file.
1#ifndef RESIP_Log_hxx
2#define RESIP_Log_hxx
3
4#include "rutil/Data.hxx"
5
6#ifndef WIN32
7#include <syslog.h>
8#include <unistd.h>
9#endif
10
11#include <atomic>
12#include <set>
13
14#include "rutil/ConfigParse.hxx"
15#include "rutil/Mutex.hxx"
16#include "rutil/Lock.hxx"
17#include "rutil/HashMap.hxx"
18#include "rutil/ThreadIf.hxx"
19#include <iostream>
20
21// !ipse! I think that this remark is no more valid with recent changes,
22// but I don't have MacOs X to test with. Someone should take this duty.
23//
24// NOTE: disabling thread setting code for native mac os applications.
25// since some logging takes place during static initialization we can't
26// be sure all the pthread stuff is ready to go. this eventually causes
27// crashes in the Mac OS native API.
28#if !defined(TARGET_OS_MAC)
29#define LOG_ENABLE_THREAD_SETTING
30#endif
31
32extern "C"
33{
34 // Forward declaration to make it friend of Log class.
35 void freeLocalLogger(void* pThreadData);
36};
37
38
39namespace resip
40{
41
42class ExternalLogger;
43class Subsystem;
44
50class Log
51{
52 public:
64
65 enum Level
66 {
67 None = -1,
68#ifdef WIN32
69 Crit = 2,
70 Err = 3,
71 Warning = 4,
72 Info = 6,
73 Debug = 7,
74#else
75 Crit = LOG_CRIT,
76// #ifdef ERR // ncurses defines a macro called ERR
77// SIP2_ERR = LOG_ERR,
78// #else
79// ERR = LOG_ERR,
80// #endif
82 Warning = LOG_WARNING,
83 Info = LOG_INFO,
84 Debug = LOG_DEBUG,
85#endif
86 Stack = 8,
87 StdErr = 9,
88 Bogus = 666
89 };
90
96
98 typedef int LocalLoggerId;
99
105 class Guard
106 {
107 public:
111 const Subsystem& system,
112 const char* file,
113 int line,
114 const char* methodName);
115
117 ~Guard();
118
119 EncodeStream& asStream() {return mStream;}
120 operator EncodeStream&() {return mStream;}
121
122 private:
123 resip::Log::Level mLevel;
124 const resip::Subsystem& mSubsystem;
125 resip::Data::size_type mHeaderLength;
126 const char* mFile;
127 int mLine;
128 const char* mMethodName;
129 char mBuffer[128];
130 Data mData;
131 oDataStream mStream;
132 Guard& operator=(const Guard&);
133 };
134
136 {
137 public:
139 : mService(-1),
140 mLevel(Err)
141 {}
142
144 : mService(serv),
146 {
147 }
148
151 };
152
155 const Subsystem& subsystem,
156 const char* file,
157 int line,
158 const char* methodName,
159 EncodeStream& strm,
160 MessageStructure messageStructure);
161
162 static Data& timestamp(Data& result);
163 static Data timestamp();
165 {
167 }
169 {
170 return mAppName;
171 }
172
173 static int parseSyslogFacilityName(const Data& facilityName);
174
175 static void initialize(Type type,
176 Level level,
177 const Data& appName,
178 const char * logFileName = 0,
179 ExternalLogger* externalLogger = 0,
180 const Data& syslogFacility = "LOG_DAEMON",
181 MessageStructure messageStructure = Unstructured,
182 const Data& instanceName = "");
183 static void initialize(const Data& type,
184 const Data& level,
185 const Data& appName,
186 const char * logFileName = 0,
187 ExternalLogger* externalLogger = 0,
188 const Data& syslogFacility = "LOG_DAEMON",
189 const Data& messageStructure = "Unstructured",
190 const Data& instanceName = "");
191 static void initialize(const char* type,
192 const char* level,
193 const char* appName,
194 const char * logFileName = 0,
195 ExternalLogger* externalLogger = 0,
196 const char* syslogFacility = "LOG_DAEMON",
197 const char* messageStructure = "Unstructured",
198 const char* instanceName = "");
199 static void initialize(Type type,
200 Level level,
201 const Data& appName,
202 ExternalLogger& logger,
203 const Data& syslogFacility = "LOG_DAEMON",
204 MessageStructure messageStructure = Unstructured,
205 const Data& instanceName = "");
206 static void initialize(const ConfigParse& configParse,
207 const Data& appName,
208 ExternalLogger* externalLogger = 0);
209
213 static void setLevel(Level level);
215 static void setLevel(Level level, Subsystem& s);
217 static void setLevel(Level level, LocalLoggerId loggerId);
221 static Level level() { Lock lock(_mutex); return getLoggerData().mLevel; }
223 static Level level(LocalLoggerId loggerId);
224 static LocalLoggerId id() { Lock lock(_mutex); return getLoggerData().id(); }
225 static void setMaxLineCount(unsigned int maxLineCount);
226 static void setMaxLineCount(unsigned int maxLineCount, LocalLoggerId loggerId);
227 static void setMaxByteCount(unsigned int maxByteCount);
228 static void setMaxByteCount(unsigned int maxByteCount, LocalLoggerId loggerId);
229 static void setKeepAllLogFiles(bool keepAllLogFiles);
230 static void setKeepAllLogFiles(bool keepAllLogFiles, LocalLoggerId loggerId);
231 static Level toLevel(const Data& l);
232 static Type toType(const Data& t);
233 static Data toString(Level l);
234
236 static void setServiceLevel(int service, Level l);
237 static Level getServiceLevel(int service);
238
240 static const ThreadSetting* getThreadSetting();
241 static void setThreadSetting(ThreadSetting info);
242 static void setThreadSetting(int serv, Level l);
243 static void setThreadSetting(int serv);
244
247 Level level,
248 const char * logFileName = NULL,
249 ExternalLogger* externalLogger = NULL,
250 MessageStructure messageStructure = Unstructured);
251
256 static int localLoggerReinitialize(LocalLoggerId loggerId,
257 Type type,
258 Level level,
259 const char * logFileName = NULL,
260 ExternalLogger* externalLogger = NULL,
261 MessageStructure messageStructure = Unstructured);
262
269 static int localLoggerRemove(LocalLoggerId loggerId);
270
277 static int setThreadLocalLogger(LocalLoggerId loggerId);
278
279
280 static std::ostream& Instance(unsigned int bytesToWrite);
281 static bool isLogging(Log::Level level, const Subsystem&);
282 static void OutputToWin32DebugWindow(const Data& result);
283 static void reset();
284#ifndef WIN32
285 static void droppingPrivileges(uid_t uid, pid_t pid);
286#endif
287
288 protected:
289 static Mutex _mutex;
290 static std::atomic<unsigned int> touchCount;
291 static const Data delim;
292
293 static unsigned int MaxLineCount;
294 static unsigned int MaxByteCount;
295 static bool KeepAllLogFiles;
296
298 {
299 public:
301 const char *logFileName=NULL,
302 ExternalLogger *pExternalLogger=NULL,
303 MessageStructure messageStructure = Unstructured,
304 const Data& instanceName = "")
305 : mLevel(level),
306 mMaxLineCount(0),
307 mMaxByteCount(0),
308 mExternalLogger(pExternalLogger),
309 mMessageStructure(messageStructure),
310 mInstanceName(instanceName),
311 mKeepAllLogFiles(false),
312 mKeepAllLogFilesSet(false),
313 mId(id),
314 mType(type),
315 mLogger(NULL),
316 mLineCount(0)
317 {
318 if (logFileName)
319 {
320 mLogFileName = logFileName;
321 }
322 }
324
325 void set(Type type = Cout, Level level = Info,
326 const char* logFileName = NULL,
327 ExternalLogger* pExternalLogger = NULL,
328 MessageStructure messageStructure = Unstructured,
329 const Data& instanceName = "");
330
331 LocalLoggerId id() const {return mId;}
332 unsigned int maxLineCount() { return mMaxLineCount ? mMaxLineCount : MaxLineCount; } // return local max, if not set use global max
333 unsigned int maxByteCount() { return mMaxByteCount ? mMaxByteCount : MaxByteCount; } // return local max, if not set use global max
334 bool keepAllLogFiles() { return mKeepAllLogFilesSet ? mKeepAllLogFiles : KeepAllLogFiles; } // return local if set, if not use global setting
335 Type type() const {return mType;}
336
338
339 std::ostream& Instance(unsigned int bytesToWrite);
340 void reset();
341#ifndef WIN32
342 void droppingPrivileges(uid_t uid, pid_t pid);
343#endif
344 volatile Level mLevel;
345 volatile unsigned int mMaxLineCount;
346 volatile unsigned int mMaxByteCount;
348
349 protected:
352
353 volatile bool mKeepAllLogFiles;
354 volatile bool mKeepAllLogFilesSet;
355
356 friend class Guard;
360 std::ostream* mLogger;
361 unsigned int mLineCount;
362 };
363
367 static Data mFqdn;
368 static int mSyslogFacility;
369 static const char mDescriptions[][32];
370 static const char mCEEPri[][32];
371 static const int mSyslogPriority[];
373
379
382 {
383 public:
386
389 Level level,
390 const char * logFileName = NULL,
391 ExternalLogger* externalLogger = NULL,
392 MessageStructure messageStructure = Unstructured);
393
398 int reinitialize(LocalLoggerId loggerId,
399 Type type,
400 Level level,
401 const char * logFileName = NULL,
402 ExternalLogger* externalLogger = NULL,
403 MessageStructure messageStructure = Unstructured);
404
411 int remove(LocalLoggerId loggerId);
412
416
418 void decreaseUseCount(LocalLoggerId loggerId);
419
420 protected:
422 typedef HashMap<LocalLoggerId, std::pair<ThreadData*, int> > LoggerInstanceMap;
428 };
429
430 friend void ::freeLocalLogger(void* pThreadData);
434
435
437#ifdef LOG_ENABLE_THREAD_SETTING
441#endif
443};
444
448{
449public:
450 virtual ~ExternalLogger() {};
452 virtual bool operator()(Log::Level level,
453 const Subsystem& subsystem,
454 const Data& appName,
455 const char* file,
456 int line,
457 const Data& message,
458 const Data& messageWithHeaders,
459 const Data& instanceName) = 0;
460};
461
464public:
467protected:
468 static unsigned int mInstanceCounter;
469
470#ifdef WIN32
471 // LogStaticInitializer calls ThreadIf::tlsKeyCreate which
472 // relies on the static TlsDestructorInitializer having set
473 // up mTlsDestructorsMutex which is used to Lock TLS access
474 // Since the order of static initialization is not reliable,
475 // we must make sure that TlsDestructorInitializer is initialized
476 // before LogStaticInitializer is inizialized:
477 TlsDestructorInitializer tlsDestructorInitializer;
478#endif
479};
481
482}
483
484#endif
485
486/* ====================================================================
487 * The Vovida Software License, Version 1.0
488 *
489 * Copyright (c) 2000-2005.
490 *
491 * Redistribution and use in source and binary forms, with or without
492 * modification, are permitted provided that the following conditions
493 * are met:
494 *
495 * 1. Redistributions of source code must retain the above copyright
496 * notice, this list of conditions and the following disclaimer.
497 *
498 * 2. Redistributions in binary form must reproduce the above copyright
499 * notice, this list of conditions and the following disclaimer in
500 * the documentation and/or other materials provided with the
501 * distribution.
502 *
503 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
504 * and "Vovida Open Communication Application Library (VOCAL)" must
505 * not be used to endorse or promote products derived from this
506 * software without prior written permission. For written
507 * permission, please contact vocal@vovida.org.
508 *
509 * 4. Products derived from this software may not be called "VOCAL", nor
510 * may "VOCAL" appear in their name, without prior written
511 * permission of Vovida Networks, Inc.
512 *
513 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
514 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
515 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
516 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
517 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
518 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
519 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
520 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
521 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
522 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
523 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
524 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
525 * DAMAGE.
526 *
527 * ====================================================================
528 *
529 * This software consists of voluntary contributions made by Vovida
530 * Networks, Inc. and many individuals on behalf of Vovida Networks,
531 * Inc. For more information on Vovida Networks, Inc., please see
532 * <http://www.vovida.org/>.
533 *
534 */
Wraps your platform's implementation of hash_map, hash_multimap, and hash_set.
void freeLocalLogger(void *pThreadData)
Definition Log.cxx:100
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
size_t size_type
Definition Data.hxx:94
Interface functor for external logging.
Definition Log.hxx:448
virtual bool operator()(Log::Level level, const Subsystem &subsystem, const Data &appName, const char *file, int line, const Data &message, const Data &messageWithHeaders, const Data &instanceName)=0
return true to also do default logging, false to suppress default logging.
virtual ~ExternalLogger()
Definition Log.hxx:450
Class to initialize Log class static variables.
Definition Log.hxx:463
static unsigned int mInstanceCounter
Definition Log.hxx:468
Implementation for logging macros.
Definition Log.hxx:106
EncodeStream & asStream()
Definition Log.hxx:119
~Guard()
Commit logging.
Definition Log.cxx:1111
Thread Local logger settings storage.
Definition Log.hxx:382
int reinitialize(LocalLoggerId loggerId, Type type, Level level, const char *logFileName=NULL, ExternalLogger *externalLogger=NULL, MessageStructure messageStructure=Unstructured)
Reinitialize all new setting for a local logger instance.
Definition Log.cxx:1013
int remove(LocalLoggerId loggerId)
Remove existing logger instance from map and destroy.
Definition Log.cxx:1033
Mutex mLoggerInstancesMapMutex
Mutex to synchronize access to Thread Local logger settings storage.
Definition Log.hxx:427
LocalLoggerId create(Type type, Level level, const char *logFileName=NULL, ExternalLogger *externalLogger=NULL, MessageStructure messageStructure=Unstructured)
Create new logger instance and return its ID (zero on error)
Definition Log.cxx:998
HashMap< LocalLoggerId, std::pair< ThreadData *, int > > LoggerInstanceMap
Storage for Thread Local loggers and their use-counts.
Definition Log.hxx:422
LocalLoggerId mLastLocalLoggerId
Last used LocalLoggerId.
Definition Log.hxx:425
void decreaseUseCount(LocalLoggerId loggerId)
Decrease use counter for given loggerId.
Definition Log.cxx:1067
LoggerInstanceMap mLoggerInstancesMap
Definition Log.hxx:423
ThreadData * getData(LocalLoggerId loggerId)
Get pointer to ThreadData for given ID and increase use counter.
Definition Log.cxx:1054
volatile MessageStructure mMessageStructure
Definition Log.hxx:350
ExternalLogger * mExternalLogger
Definition Log.hxx:347
volatile Level mLevel
Definition Log.hxx:344
unsigned int maxByteCount()
Definition Log.hxx:333
std::ostream & Instance(unsigned int bytesToWrite)
Return logger stream instance, creating it if needed.
Definition Log.cxx:1202
void droppingPrivileges(uid_t uid, pid_t pid)
Definition Log.cxx:1311
const LocalLoggerId mId
Definition Log.hxx:357
LocalLoggerId id() const
Definition Log.hxx:331
void setKeepAllLogFiles(bool keepAllLogFiles)
Definition Log.hxx:337
volatile unsigned int mMaxByteCount
Definition Log.hxx:346
void reset()
Frees logger stream.
Definition Log.cxx:1303
volatile bool mKeepAllLogFiles
Definition Log.hxx:353
volatile unsigned int mMaxLineCount
Definition Log.hxx:345
volatile bool mKeepAllLogFilesSet
Definition Log.hxx:354
ThreadData(LocalLoggerId id, Type type=Cout, Level level=Info, const char *logFileName=NULL, ExternalLogger *pExternalLogger=NULL, MessageStructure messageStructure=Unstructured, const Data &instanceName="")
Definition Log.hxx:300
unsigned int mLineCount
Definition Log.hxx:361
Type type() const
Definition Log.hxx:335
unsigned int maxLineCount()
Definition Log.hxx:332
std::ostream * mLogger
Definition Log.hxx:360
ThreadSetting(int serv, Level level)
Definition Log.hxx:143
Singleton that handles logging calls.
Definition Log.hxx:51
static bool isLogging(Log::Level level, const Subsystem &)
Definition Log.cxx:971
static const int mSyslogPriority[]
Definition Log.hxx:371
static HashMap< ThreadIf::Id, std::pair< ThreadSetting, bool > > mThreadToLevel
DEPRECATED! Left for backward compatibility - use localLoggers instead.
Definition Log.hxx:438
static ExternalLogger * getExternal()
Definition Log.hxx:164
static ThreadData & getLoggerData()
Definition Log.hxx:374
static void setMaxByteCount(unsigned int maxByteCount)
Definition Log.cxx:512
@ Syslog
Definition Log.hxx:56
@ OnlyExternal
log messages are only written to external logger
Definition Log.hxx:60
@ VSDebugWindow
Use only for Visual Studio Debug Window logging - WIN32 must be defined.
Definition Log.hxx:59
@ OnlyExternalNoHeaders
same as OnlyExternal, only the messageWithHeaders param of the ExternalLogger will be empty.
Definition Log.hxx:61
static const char mCEEPri[][32]
Definition Log.hxx:370
static Data mInstanceName
Definition Log.hxx:372
static int mSyslogFacility
Definition Log.hxx:368
static const ThreadSetting * getThreadSetting()
DEPRECATED! Left for backward compatibility - use localLoggers instead.
Definition Log.cxx:823
static ThreadData mDefaultLoggerData
Default logger settings.
Definition Log.hxx:364
static int parseSyslogFacilityName(const Data &facilityName)
Definition Log.cxx:173
static void reset()
Frees logger stream.
Definition Log.cxx:957
static Data toString(Level l)
Definition Log.cxx:574
static Level getServiceLevel(int service)
Definition Log.cxx:808
static unsigned int MaxLineCount
Definition Log.hxx:293
static Data mFqdn
Definition Log.hxx:367
static void setServiceLevel(int service, Level l)
DEPRECATED! Left for backward compatibility - use localLoggers instead.
Definition Log.cxx:890
static Data timestamp()
Definition Log.cxx:733
static int setThreadLocalLogger(LocalLoggerId loggerId)
Set logger instance with given ID as a thread local logger.
Definition Log.cxx:932
static LocalLoggerMap mLocalLoggerMap
Definition Log.hxx:432
int LocalLoggerId
Thread Local logger ID type.
Definition Log.hxx:98
static Data getAppName()
Definition Log.hxx:168
static std::atomic< unsigned int > touchCount
Definition Log.hxx:290
static Level toLevel(const Data &l)
Definition Log.cxx:580
static HashMap< int, Level > mServiceToLevel
DEPRECATED! Left for backward compatibility - use localLoggers instead.
Definition Log.hxx:442
static void setThreadSetting(ThreadSetting info)
Definition Log.cxx:866
static ThreadIf::TlsKey * mLocalLoggerKey
Definition Log.hxx:433
static void setLevel(Level level)
Set logging level for current thread.
Definition Log.cxx:423
static int localLoggerReinitialize(LocalLoggerId loggerId, Type type, Level level, const char *logFileName=NULL, ExternalLogger *externalLogger=NULL, MessageStructure messageStructure=Unstructured)
Reinitialize all new setting for a local logger instance.
Definition Log.cxx:917
static unsigned int MaxByteCount
Definition Log.hxx:294
static EncodeStream & tags(Log::Level level, const Subsystem &subsystem, const char *file, int line, const char *methodName, EncodeStream &strm, MessageStructure messageStructure)
output the loglevel, hostname, appname, pid, tid, subsystem
Definition Log.cxx:620
static HashMap< int, std::set< ThreadIf::Id > > mServiceToThreads
Definition Log.hxx:439
static int localLoggerRemove(LocalLoggerId loggerId)
Destroy existing logger instance.
Definition Log.cxx:927
static Mutex _mutex
Definition Log.hxx:289
static Data mAppName
Definition Log.hxx:365
static const Data delim
Definition Log.hxx:291
static Level level()
Return logging level for current thread.
Definition Log.hxx:221
static void setKeepAllLogFiles(bool keepAllLogFiles)
Definition Log.cxx:542
static void setMaxLineCount(unsigned int maxLineCount)
Definition Log.cxx:482
static LocalLoggerId id()
Definition Log.hxx:224
static void droppingPrivileges(uid_t uid, pid_t pid)
Definition Log.cxx:964
static bool KeepAllLogFiles
Definition Log.hxx:295
@ Bogus
Definition Log.hxx:88
@ Debug
Definition Log.hxx:84
@ Stack
Definition Log.hxx:86
@ StdErr
Definition Log.hxx:87
@ Warning
Definition Log.hxx:82
MessageStructure
Definition Log.hxx:92
@ JSON_CEE
Definition Log.hxx:94
@ Unstructured
Definition Log.hxx:93
static const char mDescriptions[][32]
Definition Log.hxx:369
static Data mHostname
Definition Log.hxx:366
static void initialize(Type type, Level level, const Data &appName, const char *logFileName=0, ExternalLogger *externalLogger=0, const Data &syslogFacility="LOG_DAEMON", MessageStructure messageStructure=Unstructured, const Data &instanceName="")
Definition Log.cxx:268
static void OutputToWin32DebugWindow(const Data &result)
Definition Log.cxx:984
static Type toType(const Data &t)
Definition Log.cxx:599
static ThreadIf::TlsKey * mLevelKey
Definition Log.hxx:440
static std::ostream & Instance(unsigned int bytesToWrite)
Definition Log.cxx:951
static LocalLoggerId localLoggerCreate(Type type, Level level, const char *logFileName=NULL, ExternalLogger *externalLogger=NULL, MessageStructure messageStructure=Unstructured)
Create new logger instance and return its ID (zero on error)
Definition Log.cxx:908
Class used to specify what sub-system given sections of code belong to, for use by the logging system...
Definition Subsystem.hxx:34
static void * tlsGetValue(TlsKey key)
This function follows pthread_getspecific() signature.
Definition ThreadIf.cxx:179
pthread_key_t TlsKey
Definition ThreadIf.hxx:85
An ostream that operates on an existing Data.
dcm! – add UnusedChecking(_enum) below;
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
static LogStaticInitializer _staticLogInit
Definition Log.hxx:480
std::unique_lock< Mutex > Lock
A convenience class to lock a Lockable (such as a Mutex) on object creation, and unlock on destructio...
Definition Lock.hxx:28
#define EncodeStream
void list()
Definition userAdmin.cxx:66