blob: 6452e251680d1a3f071f8345fda0c2fdc5b37fec [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*
Pau Espin Pedrol21d03d32019-07-22 12:05:52 +02005* SPDX-License-Identifier: AGPL-3.0+
6*
dburgess82c46ff2011-10-07 02:40:51 +00007* This software is distributed under the terms of the GNU Affero Public License.
8* See the COPYING file in the main directory for details.
9*
10* This use of this software may be subject to additional restrictions.
11* See the LEGAL file in the main directory for details.
12
13 This program is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Affero General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Affero General Public License for more details.
22
23 You should have received a copy of the GNU Affero General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26*/
27
dburgess82c46ff2011-10-07 02:40:51 +000028#ifndef LOGGER_H
29#define LOGGER_H
30
dburgess82c46ff2011-10-07 02:40:51 +000031#include <stdint.h>
32#include <stdio.h>
33#include <sstream>
dburgess82c46ff2011-10-07 02:40:51 +000034#include <string>
dburgess82c46ff2011-10-07 02:40:51 +000035
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010036extern "C" {
37#include <osmocom/core/logging.h>
38#include "debug.h"
39}
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010040
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010041/* Translation for old log statements */
42#ifndef LOGL_ALERT
43#define LOGL_ALERT LOGL_FATAL
dburgess7b2d5222011-11-20 00:22:41 +000044#endif
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010045#ifndef LOGL_ERR
46#define LOGL_ERR LOGL_ERROR
47#endif
48#ifndef LOGL_WARNING
49#define LOGL_WARNING LOGL_NOTICE
50#endif
51
52#define LOG(level) \
Pau Espin Pedrold06259f2021-03-01 13:22:26 +010053 Log(DMAIN, LOGL_##level, __BASE_FILE__, __LINE__).get()
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010054
55#define LOGC(category, level) \
Pau Espin Pedrold06259f2021-03-01 13:22:26 +010056 Log(category, LOGL_##level, __BASE_FILE__, __LINE__).get()
dburgess7b2d5222011-11-20 00:22:41 +000057
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020058#define LOGLV(category, level) \
Pau Espin Pedrold06259f2021-03-01 13:22:26 +010059 Log(category, level, __BASE_FILE__, __LINE__).get()
Pau Espin Pedrolc7a0bf12018-04-25 12:17:10 +020060
Pau Espin Pedrol84231bd2019-12-20 22:47:06 +010061#define LOGSRC(category, level, file, line) \
Pau Espin Pedrold06259f2021-03-01 13:22:26 +010062 Log(category, level, file, line).get()
Pau Espin Pedrol84231bd2019-12-20 22:47:06 +010063
Pau Espin Pedrolfc73c072019-05-03 19:40:00 +020064#define LOGCHAN(chan, category, level) \
Pau Espin Pedrold06259f2021-03-01 13:22:26 +010065 Log(category, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[chan=" << chan << "] "
Pau Espin Pedrolfc73c072019-05-03 19:40:00 +020066
dburgess82c46ff2011-10-07 02:40:51 +000067/**
68 A C++ stream-based thread-safe logger.
dburgess82c46ff2011-10-07 02:40:51 +000069 This object is NOT the global logger;
70 every log record is an object of this class.
71*/
72class Log {
73
74 public:
75
76 protected:
77
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010078 std::ostringstream mStream; ///< This is where we buffer up the log entry.
79 int mCategory; ///< Priority of current report.
80 int mPriority; ///< Category of current report.
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020081 const char *filename; ///< Source File Name of current report.
82 int line; ///< Line number in source file of current report.
dburgess82c46ff2011-10-07 02:40:51 +000083
84 public:
85
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020086 Log(int wCategory, int wPriority, const char* filename, int line)
87 : mCategory(wCategory), mPriority(wPriority),
88 filename(filename), line(line)
dburgess82c46ff2011-10-07 02:40:51 +000089 { }
90
kurtis.heimerleb63ec92012-12-17 11:21:32 +000091 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +000092 /** The destructor actually generates the log entry. */
93 ~Log();
94
95 std::ostringstream& get();
96};
97
Alexander Chemeris4793f462017-03-17 18:35:48 -070098std::ostream& operator<<(std::ostream& os, std::ostringstream& ss);
dburgess82c46ff2011-10-07 02:40:51 +000099
dburgess82c46ff2011-10-07 02:40:51 +0000100#endif
101
102// vim: ts=4 sw=4