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