blob: 58dfa228f6c7fc772a2c01655a2f154d756b6882 [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
43#define _LOG(level) \
44 Log(LOG_##level).get() << pthread_self() \
kurtis.heimerl5a872472013-05-31 21:47:25 +000045 << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": "
46
47#define IS_LOG_LEVEL(wLevel) (gGetLoggingLevel(__FILE__)>=LOG_##wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000048
49#ifdef NDEBUG
50#define LOG(wLevel) \
kurtis.heimerl5a872472013-05-31 21:47:25 +000051 if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000052#else
dburgess82c46ff2011-10-07 02:40:51 +000053#define LOG(wLevel) \
kurtis.heimerl5a872472013-05-31 21:47:25 +000054 if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000055#endif
56
kurtis.heimerl5a872472013-05-31 21:47:25 +000057// pat: And for your edification here are the 'levels' as defined in syslog.h:
58// LOG_EMERG 0 system is unusable
59// LOG_ALERT 1 action must be taken immediately
60// LOG_CRIT 2 critical conditions
61// LOG_ERR 3 error conditions
62// LOG_WARNING 4 warning conditions
63// LOG_NOTICE 5 normal, but significant, condition
64// LOG_INFO 6 informational message
65// LOG_DEBUG 7 debug-level message
66
67// (pat) added - print out a var and its name.
68// Use like this: int descriptive_name; LOG(INFO)<<LOGVAR(descriptive_name);
69#define LOGVAR2(name,val) " " << name << "=" << (val)
70#define LOGVAR(var) (" " #var "=") << var
71#define LOGHEX(var) (" " #var "=0x") << hex << ((unsigned)var) << dec
72#define LOGHEX2(name,val) " " << name << "=0x" << hex << ((unsigned)(val)) << dec
73// These are kind of cheesy, but you can use for bitvector
74#define LOGBV2(name,val) " " << name << "=(" << val<<" size:"<<val.size()<<")"
75#define LOGBV(bv) LOGBV2(#bv,bv)
76#define LOGVARRANGE(name,cur,lo,hi) " "<<name <<"=("<<(cur) << " range:"<<(lo) << " to "<<(hi) <<")"
77
dburgess7b2d5222011-11-20 00:22:41 +000078
dburgess82c46ff2011-10-07 02:40:51 +000079#define OBJLOG(wLevel) \
dburgess7b2d5222011-11-20 00:22:41 +000080 LOG(wLevel) << "obj: " << this << ' '
dburgess82c46ff2011-10-07 02:40:51 +000081
82#define LOG_ASSERT(x) { if (!(x)) LOG(EMERG) << "assertion " #x " failed"; } assert(x);
83
84
kurtis.heimerl5a872472013-05-31 21:47:25 +000085#include "Threads.h" // must be after defines above, if these files are to be allowed to use LOG()
86#include "Utils.h"
dburgess82c46ff2011-10-07 02:40:51 +000087
88/**
89 A C++ stream-based thread-safe logger.
90 Derived from Dr. Dobb's Sept. 2007 issue.
91 Updated to use syslog.
92 This object is NOT the global logger;
93 every log record is an object of this class.
94*/
95class Log {
96
97 public:
98
99 protected:
100
101 std::ostringstream mStream; ///< This is where we buffer up the log entry.
kurtis.heimerleb63ec92012-12-17 11:21:32 +0000102 int mPriority; ///< Priority of current report.
dburgess7b2d5222011-11-20 00:22:41 +0000103 bool mDummyInit;
dburgess82c46ff2011-10-07 02:40:51 +0000104
105 public:
106
107 Log(int wPriority)
dburgess7b2d5222011-11-20 00:22:41 +0000108 :mPriority(wPriority), mDummyInit(false)
dburgess82c46ff2011-10-07 02:40:51 +0000109 { }
110
dburgess7b2d5222011-11-20 00:22:41 +0000111 Log(const char* name, const char* level=NULL, int facility=LOG_USER);
112
kurtis.heimerleb63ec92012-12-17 11:21:32 +0000113 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +0000114 /** The destructor actually generates the log entry. */
115 ~Log();
116
117 std::ostringstream& get();
118};
kurtis.heimerl5a872472013-05-31 21:47:25 +0000119extern bool gLogToConsole; // Pat added for easy debugging.
dburgess82c46ff2011-10-07 02:40:51 +0000120
121
122
123std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm list.
124
125
126/**@ Global control and initialization of the logging system. */
127//@{
128/** Initialize the global logging system. */
129void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER);
130/** Get the logging level associated with a given file. */
131int gGetLoggingLevel(const char *filename=NULL);
kurtis.heimerl00913d72012-12-16 06:08:18 +0000132/** Allow early logging when still in constructors */
133void gLogEarly(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
dburgess82c46ff2011-10-07 02:40:51 +0000134//@}
135
136
137#endif
138
139// vim: ts=4 sw=4