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 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 26 | // (pat) WARNING is stupidly defined in /usr/local/include/osipparser2/osip_const.h. |
| 27 | // This must be outside the #ifndef LOGGER_H to fix it as long as Logger.h included after the above file. |
| 28 | #ifdef WARNING |
| 29 | #undef WARNING |
| 30 | #endif |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 31 | |
| 32 | #ifndef LOGGER_H |
| 33 | #define LOGGER_H |
| 34 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | #include <stdint.h> |
| 36 | #include <stdio.h> |
| 37 | #include <sstream> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 38 | #include <string> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 39 | |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 40 | extern int config_log_level; |
| 41 | |
Pau Espin Pedrol | 11d50d9 | 2018-02-20 17:52:28 +0100 | [diff] [blame] | 42 | #define LOG_EMERG 0 /* system is unusable */ |
| 43 | #define LOG_ALERT 1 /* action must be taken immediately */ |
| 44 | #define LOG_CRIT 2 /* critical conditions */ |
| 45 | #define LOG_ERR 3 /* error conditions */ |
| 46 | #define LOG_WARNING 4 /* warning conditions */ |
| 47 | #define LOG_NOTICE 5 /* normal but significant condition */ |
| 48 | #define LOG_INFO 6 /* informational */ |
| 49 | #define LOG_DEBUG 7 /* debug-level messages */ |
| 50 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 51 | #define _LOG(level) \ |
| 52 | Log(LOG_##level).get() << pthread_self() \ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 53 | << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " |
| 54 | |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 55 | #define IS_LOG_LEVEL(wLevel) (config_log_level>=LOG_##wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 56 | |
| 57 | #ifdef NDEBUG |
| 58 | #define LOG(wLevel) \ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 59 | if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 60 | #else |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 61 | #define LOG(wLevel) \ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 62 | if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 63 | #endif |
| 64 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 65 | /** |
| 66 | A C++ stream-based thread-safe logger. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 67 | This object is NOT the global logger; |
| 68 | every log record is an object of this class. |
| 69 | */ |
| 70 | class Log { |
| 71 | |
| 72 | public: |
| 73 | |
| 74 | protected: |
| 75 | |
| 76 | std::ostringstream mStream; ///< This is where we buffer up the log entry. |
kurtis.heimerl | eb63ec9 | 2012-12-17 11:21:32 +0000 | [diff] [blame] | 77 | int mPriority; ///< Priority of current report. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 78 | |
| 79 | public: |
| 80 | |
| 81 | Log(int wPriority) |
Pau Espin Pedrol | 11d50d9 | 2018-02-20 17:52:28 +0100 | [diff] [blame] | 82 | :mPriority(wPriority) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 83 | { } |
| 84 | |
kurtis.heimerl | eb63ec9 | 2012-12-17 11:21:32 +0000 | [diff] [blame] | 85 | // Most of the work is in the destructor. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 86 | /** The destructor actually generates the log entry. */ |
| 87 | ~Log(); |
| 88 | |
| 89 | std::ostringstream& get(); |
| 90 | }; |
| 91 | |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 92 | const std::string timestr(); // A timestamp to print in messages. |
| 93 | std::ostream& operator<<(std::ostream& os, std::ostringstream& ss); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 94 | |
| 95 | /**@ Global control and initialization of the logging system. */ |
| 96 | //@{ |
| 97 | /** Initialize the global logging system. */ |
Pau Espin Pedrol | 11d50d9 | 2018-02-20 17:52:28 +0100 | [diff] [blame] | 98 | void gLogInit(const char* level=NULL, char* fn=NULL); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 99 | //@} |
| 100 | |
| 101 | |
| 102 | #endif |
| 103 | |
| 104 | // vim: ts=4 sw=4 |