dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009, 2010 Free Software Foundation, Inc. |
| 3 | * Copyright 2010 Kestrel Signal Processing, Inc. |
| 4 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: AGPL-3.0+ |
| 6 | * |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 7 | * 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 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 28 | #ifndef LOGGER_H |
| 29 | #define LOGGER_H |
| 30 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <sstream> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 34 | #include <string> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 36 | extern "C" { |
| 37 | #include <osmocom/core/logging.h> |
| 38 | #include "debug.h" |
| 39 | } |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 40 | |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 41 | /* Translation for old log statements */ |
| 42 | #ifndef LOGL_ALERT |
| 43 | #define LOGL_ALERT LOGL_FATAL |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 44 | #endif |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 45 | #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 Pedrol | 3b78cbf | 2018-04-25 16:43:02 +0200 | [diff] [blame] | 53 | Log(DMAIN, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] " |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 54 | |
| 55 | #define LOGC(category, level) \ |
Pau Espin Pedrol | 3b78cbf | 2018-04-25 16:43:02 +0200 | [diff] [blame] | 56 | Log(category, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] " |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 57 | |
Pau Espin Pedrol | c7a0bf1 | 2018-04-25 12:17:10 +0200 | [diff] [blame] | 58 | #define LOGLV(category, level) \ |
| 59 | Log(category, level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "] " |
| 60 | |
Pau Espin Pedrol | fc73c07 | 2019-05-03 19:40:00 +0200 | [diff] [blame] | 61 | #define LOGCHAN(chan, category, level) \ |
| 62 | Log(category, LOGL_##level, __BASE_FILE__, __LINE__).get() << "[tid=" << pthread_self() << "][chan=" << chan << "] " |
| 63 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 64 | /** |
| 65 | A C++ stream-based thread-safe logger. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 66 | This object is NOT the global logger; |
| 67 | every log record is an object of this class. |
| 68 | */ |
| 69 | class Log { |
| 70 | |
| 71 | public: |
| 72 | |
| 73 | protected: |
| 74 | |
Pau Espin Pedrol | 3da1f83 | 2018-02-20 20:01:10 +0100 | [diff] [blame] | 75 | std::ostringstream mStream; ///< This is where we buffer up the log entry. |
| 76 | int mCategory; ///< Priority of current report. |
| 77 | int mPriority; ///< Category of current report. |
Pau Espin Pedrol | 3b78cbf | 2018-04-25 16:43:02 +0200 | [diff] [blame] | 78 | const char *filename; ///< Source File Name of current report. |
| 79 | int line; ///< Line number in source file of current report. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 80 | |
| 81 | public: |
| 82 | |
Pau Espin Pedrol | 3b78cbf | 2018-04-25 16:43:02 +0200 | [diff] [blame] | 83 | Log(int wCategory, int wPriority, const char* filename, int line) |
| 84 | : mCategory(wCategory), mPriority(wPriority), |
| 85 | filename(filename), line(line) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 86 | { } |
| 87 | |
kurtis.heimerl | eb63ec9 | 2012-12-17 11:21:32 +0000 | [diff] [blame] | 88 | // Most of the work is in the destructor. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 89 | /** The destructor actually generates the log entry. */ |
| 90 | ~Log(); |
| 91 | |
| 92 | std::ostringstream& get(); |
| 93 | }; |
| 94 | |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 95 | std::ostream& operator<<(std::ostream& os, std::ostringstream& ss); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 96 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | // vim: ts=4 sw=4 |