blob: 5b0b05caa3c675f2e1115fc79a17dcb6467ca217 [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
dburgess82c46ff2011-10-07 02:40:51 +000056/**
57 A C++ stream-based thread-safe logger.
dburgess82c46ff2011-10-07 02:40:51 +000058 This object is NOT the global logger;
59 every log record is an object of this class.
60*/
61class Log {
62
63 public:
64
65 protected:
66
Pau Espin Pedrol3da1f832018-02-20 20:01:10 +010067 std::ostringstream mStream; ///< This is where we buffer up the log entry.
68 int mCategory; ///< Priority of current report.
69 int mPriority; ///< Category of current report.
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020070 const char *filename; ///< Source File Name of current report.
71 int line; ///< Line number in source file of current report.
dburgess82c46ff2011-10-07 02:40:51 +000072
73 public:
74
Pau Espin Pedrol3b78cbf2018-04-25 16:43:02 +020075 Log(int wCategory, int wPriority, const char* filename, int line)
76 : mCategory(wCategory), mPriority(wPriority),
77 filename(filename), line(line)
dburgess82c46ff2011-10-07 02:40:51 +000078 { }
79
kurtis.heimerleb63ec92012-12-17 11:21:32 +000080 // Most of the work is in the destructor.
dburgess82c46ff2011-10-07 02:40:51 +000081 /** The destructor actually generates the log entry. */
82 ~Log();
83
84 std::ostringstream& get();
85};
86
Alexander Chemeris4793f462017-03-17 18:35:48 -070087std::ostream& operator<<(std::ostream& os, std::ostringstream& ss);
dburgess82c46ff2011-10-07 02:40:51 +000088
dburgess82c46ff2011-10-07 02:40:51 +000089#endif
90
91// vim: ts=4 sw=4