blob: a8fe44de992a7d9b73289ddfe6d198854dd87ba5 [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
dburgess82c46ff2011-10-07 02:40:51 +000035#include <stdint.h>
36#include <stdio.h>
37#include <sstream>
dburgess82c46ff2011-10-07 02:40:51 +000038#include <string>
dburgess82c46ff2011-10-07 02:40:51 +000039
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010040extern int config_log_level;
41
Pau Espin Pedrol11d50d92018-02-20 17:52:28 +010042#define LOG_EMERG 0 /* system is unusable */
43#define LOG_ALERT 1 /* action must be taken immediately */
44#define LOG_CRIT 2 /* critical conditions */
45#define LOG_ERR 3 /* error conditions */
46#define LOG_WARNING 4 /* warning conditions */
47#define LOG_NOTICE 5 /* normal but significant condition */
48#define LOG_INFO 6 /* informational */
49#define LOG_DEBUG 7 /* debug-level messages */
50
dburgess82c46ff2011-10-07 02:40:51 +000051#define _LOG(level) \
52 Log(LOG_##level).get() << pthread_self() \
kurtis.heimerl5a872472013-05-31 21:47:25 +000053 << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": "
54
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010055#define IS_LOG_LEVEL(wLevel) (config_log_level>=LOG_##wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000056
57#ifdef NDEBUG
58#define LOG(wLevel) \
kurtis.heimerl5a872472013-05-31 21:47:25 +000059 if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000060#else
dburgess82c46ff2011-10-07 02:40:51 +000061#define LOG(wLevel) \
kurtis.heimerl5a872472013-05-31 21:47:25 +000062 if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel)
dburgess7b2d5222011-11-20 00:22:41 +000063#endif
64
dburgess82c46ff2011-10-07 02:40:51 +000065/**
66 A C++ stream-based thread-safe logger.
dburgess82c46ff2011-10-07 02:40:51 +000067 This object is NOT the global logger;
68 every log record is an object of this class.
69*/
70class Log {
71
72 public:
73
74 protected:
75
76 std::ostringstream mStream; ///< This is where we buffer up the log entry.
kurtis.heimerleb63ec92012-12-17 11:21:32 +000077 int mPriority; ///< Priority of current report.
dburgess82c46ff2011-10-07 02:40:51 +000078
79 public:
80
81 Log(int wPriority)
Pau Espin Pedrol11d50d92018-02-20 17:52:28 +010082 :mPriority(wPriority)
dburgess82c46ff2011-10-07 02:40:51 +000083 { }
84
kurtis.heimerleb63ec92012-12-17 11:21:32 +000085 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +000086 /** The destructor actually generates the log entry. */
87 ~Log();
88
89 std::ostringstream& get();
90};
91
Alexander Chemeris4793f462017-03-17 18:35:48 -070092const std::string timestr(); // A timestamp to print in messages.
93std::ostream& operator<<(std::ostream& os, std::ostringstream& ss);
dburgess82c46ff2011-10-07 02:40:51 +000094
95/**@ Global control and initialization of the logging system. */
96//@{
97/** Initialize the global logging system. */
Pau Espin Pedrol11d50d92018-02-20 17:52:28 +010098void gLogInit(const char* level=NULL, char* fn=NULL);
dburgess82c46ff2011-10-07 02:40:51 +000099//@}
100
101
102#endif
103
104// vim: ts=4 sw=4