blob: e18ecfbb5bdc64e0e0f0215087d95e3a737c5b01 [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
dburgess82c46ff2011-10-07 02:40:51 +000026#ifndef LOGGER_H
27#define LOGGER_H
28
dburgess82c46ff2011-10-07 02:40:51 +000029#include <stdint.h>
30#include <stdio.h>
31#include <sstream>
dburgess82c46ff2011-10-07 02:40:51 +000032#include <string>
dburgess82c46ff2011-10-07 02:40:51 +000033
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010034extern "C" {
35#include <osmocom/core/logging.h>
36#include "debug.h"
37}
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010038
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010039/* Translation for old log statements */
40#ifndef LOGL_ALERT
41#define LOGL_ALERT LOGL_FATAL
dburgess7b2d5222011-11-20 00:22:41 +000042#endif
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010043#ifndef LOGL_ERR
44#define LOGL_ERR LOGL_ERROR
45#endif
46#ifndef LOGL_WARNING
47#define LOGL_WARNING LOGL_NOTICE
48#endif
49
50#define LOG(level) \
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020051 Log(DMAIN, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] "
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010052
53#define LOGC(category, level) \
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020054 Log(category, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] "
dburgess7b2d5222011-11-20 00:22:41 +000055
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020056#define LOGLV(category, level) \
57 Log(category, level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] "
58
dburgess82c46ff2011-10-07 02:40:51 +000059/**
60 A C++ stream-based thread-safe logger.
dburgess82c46ff2011-10-07 02:40:51 +000061 This object is NOT the global logger;
62 every log record is an object of this class.
63*/
64class Log {
65
66 public:
67
68 protected:
69
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010070 std::ostringstream mStream; ///< This is where we buffer up the log entry.
71 int mCategory; ///< Priority of current report.
72 int mPriority; ///< Category of current report.
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020073 const char *filename; ///< Source File Name of current report.
74 int line; ///< Line number in source file of current report.
dburgess82c46ff2011-10-07 02:40:51 +000075
76 public:
77
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020078 Log(int wCategory, int wPriority, const char* filename, int line)
79 : mCategory(wCategory), mPriority(wPriority),
80 filename(filename), line(line)
dburgess82c46ff2011-10-07 02:40:51 +000081 { }
82
kurtis.heimerleb63ec92012-12-17 11:21:32 +000083 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +000084 /** The destructor actually generates the log entry. */
85 ~Log();
86
87 std::ostringstream& get();
88};
89
Alexander Chemeris4793f462017-03-17 18:35:48 -070090std::ostream& operator<<(std::ostream& os, std::ostringstream& ss);
dburgess82c46ff2011-10-07 02:40:51 +000091
dburgess82c46ff2011-10-07 02:40:51 +000092#endif
93
94// vim: ts=4 sw=4