blob: 9743b884aa7d5008e99e42c6bb00b81acb656717 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2009, 2010 Free Software Foundation, Inc.
3* Copyright 2010 Kestrel Signal Processing, Inc.
4*
5* This software is distributed under the terms of the GNU Affero Public License.
6* See the COPYING file in the main directory for details.
7*
8* This use of this software may be subject to additional restrictions.
9* See the LEGAL file in the main directory for details.
10
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU Affero General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Affero General Public License for more details.
20
21 You should have received a copy of the GNU Affero General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24*/
25
kurtis.heimerl5a872472013-05-31 21:47:25 +000026// (pat) WARNING is stupidly defined in /usr/local/include/osipparser2/osip_const.h.
27// This must be outside the #ifndef LOGGER_H to fix it as long as Logger.h included after the above file.
28#ifdef WARNING
29#undef WARNING
30#endif
dburgess82c46ff2011-10-07 02:40:51 +000031
32#ifndef LOGGER_H
33#define LOGGER_H
34
35#include <syslog.h>
36#include <stdint.h>
37#include <stdio.h>
38#include <sstream>
39#include <list>
40#include <map>
41#include <string>
dburgess82c46ff2011-10-07 02:40:51 +000042
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010043extern int config_log_level;
44
dburgess82c46ff2011-10-07 02:40:51 +000045#define _LOG(level) \
46 Log(LOG_##level).get() << pthread_self() \
kurtis.heimerl5a872472013-05-31 21:47:25 +000047 << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": "
48
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010049#define IS_LOG_LEVEL(wLevel) (config_log_level>=LOG_##wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000050
51#ifdef NDEBUG
52#define LOG(wLevel) \
kurtis.heimerl5a872472013-05-31 21:47:25 +000053 if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000054#else
dburgess82c46ff2011-10-07 02:40:51 +000055#define LOG(wLevel) \
kurtis.heimerl5a872472013-05-31 21:47:25 +000056 if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000057#endif
58
kurtis.heimerl5a872472013-05-31 21:47:25 +000059// pat: And for your edification here are the 'levels' as defined in syslog.h:
60// LOG_EMERG 0 system is unusable
61// LOG_ALERT 1 action must be taken immediately
62// LOG_CRIT 2 critical conditions
63// LOG_ERR 3 error conditions
64// LOG_WARNING 4 warning conditions
65// LOG_NOTICE 5 normal, but significant, condition
66// LOG_INFO 6 informational message
67// LOG_DEBUG 7 debug-level message
68
69// (pat) added - print out a var and its name.
70// Use like this: int descriptive_name; LOG(INFO)<<LOGVAR(descriptive_name);
71#define LOGVAR2(name,val) " " << name << "=" << (val)
72#define LOGVAR(var) (" " #var "=") << var
73#define LOGHEX(var) (" " #var "=0x") << hex << ((unsigned)var) << dec
74#define LOGHEX2(name,val) " " << name << "=0x" << hex << ((unsigned)(val)) << dec
75// These are kind of cheesy, but you can use for bitvector
76#define LOGBV2(name,val) " " << name << "=(" << val<<" size:"<<val.size()<<")"
77#define LOGBV(bv) LOGBV2(#bv,bv)
78#define LOGVARRANGE(name,cur,lo,hi) " "<<name <<"=("<<(cur) << " range:"<<(lo) << " to "<<(hi) <<")"
79
dburgess7b2d5222011-11-20 00:22:41 +000080
dburgess82c46ff2011-10-07 02:40:51 +000081#define OBJLOG(wLevel) \
dburgess7b2d5222011-11-20 00:22:41 +000082 LOG(wLevel) << "obj: " << this << ' '
dburgess82c46ff2011-10-07 02:40:51 +000083
84#define LOG_ASSERT(x) { if (!(x)) LOG(EMERG) << "assertion " #x " failed"; } assert(x);
85
86
kurtis.heimerl5a872472013-05-31 21:47:25 +000087#include "Threads.h" // must be after defines above, if these files are to be allowed to use LOG()
dburgess82c46ff2011-10-07 02:40:51 +000088
89/**
90 A C++ stream-based thread-safe logger.
91 Derived from Dr. Dobb's Sept. 2007 issue.
92 Updated to use syslog.
93 This object is NOT the global logger;
94 every log record is an object of this class.
95*/
96class Log {
97
98 public:
99
100 protected:
101
102 std::ostringstream mStream; ///< This is where we buffer up the log entry.
kurtis.heimerleb63ec92012-12-17 11:21:32 +0000103 int mPriority; ///< Priority of current report.
dburgess7b2d5222011-11-20 00:22:41 +0000104 bool mDummyInit;
dburgess82c46ff2011-10-07 02:40:51 +0000105
106 public:
107
108 Log(int wPriority)
dburgess7b2d5222011-11-20 00:22:41 +0000109 :mPriority(wPriority), mDummyInit(false)
dburgess82c46ff2011-10-07 02:40:51 +0000110 { }
111
dburgess7b2d5222011-11-20 00:22:41 +0000112 Log(const char* name, const char* level=NULL, int facility=LOG_USER);
113
kurtis.heimerleb63ec92012-12-17 11:21:32 +0000114 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +0000115 /** The destructor actually generates the log entry. */
116 ~Log();
117
118 std::ostringstream& get();
119};
Alexander Chemeris57219202015-05-24 13:20:44 -0400120extern bool gLogToConsole; // Output log messages to stdout
121extern bool gLogToSyslog; // Output log messages to syslog
dburgess82c46ff2011-10-07 02:40:51 +0000122
123
124
125std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm list.
126
Alexander Chemeris4793f462017-03-17 18:35:48 -0700127const std::string timestr(); // A timestamp to print in messages.
128std::ostream& operator<<(std::ostream& os, std::ostringstream& ss);
dburgess82c46ff2011-10-07 02:40:51 +0000129
130/**@ Global control and initialization of the logging system. */
131//@{
132/** Initialize the global logging system. */
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +0100133void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER, char* fn=NULL);
dburgess82c46ff2011-10-07 02:40:51 +0000134//@}
135
136
137#endif
138
139// vim: ts=4 sw=4