blob: 6b1c44da577e2bc187f7815f3ff898fcd9bfcef5 [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
26
27#ifndef LOGGER_H
28#define LOGGER_H
29
30#include <syslog.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <sstream>
34#include <list>
35#include <map>
36#include <string>
37#include "Threads.h"
38
39
40#define _LOG(level) \
41 Log(LOG_##level).get() << pthread_self() \
42 << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": "
dburgess7b2d5222011-11-20 00:22:41 +000043
44#ifdef NDEBUG
45#define LOG(wLevel) \
46 if (LOG_##wLevel!=LOG_DEBUG && gGetLoggingLevel(__FILE__)>=LOG_##wLevel) _LOG(wLevel)
47#else
dburgess82c46ff2011-10-07 02:40:51 +000048#define LOG(wLevel) \
49 if (gGetLoggingLevel(__FILE__)>=LOG_##wLevel) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000050#endif
51
52
dburgess82c46ff2011-10-07 02:40:51 +000053#define OBJLOG(wLevel) \
dburgess7b2d5222011-11-20 00:22:41 +000054 LOG(wLevel) << "obj: " << this << ' '
dburgess82c46ff2011-10-07 02:40:51 +000055
56#define LOG_ASSERT(x) { if (!(x)) LOG(EMERG) << "assertion " #x " failed"; } assert(x);
57
58
59#define DEFAULT_MAX_ALARMS 10
60
61
62/**
63 A C++ stream-based thread-safe logger.
64 Derived from Dr. Dobb's Sept. 2007 issue.
65 Updated to use syslog.
66 This object is NOT the global logger;
67 every log record is an object of this class.
68*/
69class Log {
70
71 public:
72
73 protected:
74
75 std::ostringstream mStream; ///< This is where we buffer up the log entry.
kurtis.heimerleb63ec92012-12-17 11:21:32 +000076 int mPriority; ///< Priority of current report.
dburgess7b2d5222011-11-20 00:22:41 +000077 bool mDummyInit;
dburgess82c46ff2011-10-07 02:40:51 +000078
79 public:
80
81 Log(int wPriority)
dburgess7b2d5222011-11-20 00:22:41 +000082 :mPriority(wPriority), mDummyInit(false)
dburgess82c46ff2011-10-07 02:40:51 +000083 { }
84
dburgess7b2d5222011-11-20 00:22:41 +000085 Log(const char* name, const char* level=NULL, int facility=LOG_USER);
86
kurtis.heimerleb63ec92012-12-17 11:21:32 +000087 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +000088 /** The destructor actually generates the log entry. */
89 ~Log();
90
91 std::ostringstream& get();
92};
93
94
95
96std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm list.
97
98
99/**@ Global control and initialization of the logging system. */
100//@{
101/** Initialize the global logging system. */
102void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER);
103/** Get the logging level associated with a given file. */
104int gGetLoggingLevel(const char *filename=NULL);
kurtis.heimerl00913d72012-12-16 06:08:18 +0000105/** Allow early logging when still in constructors */
106void gLogEarly(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
dburgess82c46ff2011-10-07 02:40:51 +0000107//@}
108
109
110#endif
111
112// vim: ts=4 sw=4