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 | |
| 35 | #include <syslog.h> |
| 36 | #include <stdint.h> |
| 37 | #include <stdio.h> |
| 38 | #include <sstream> |
| 39 | #include <list> |
| 40 | #include <map> |
| 41 | #include <string> |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 42 | |
| 43 | #define _LOG(level) \ |
| 44 | Log(LOG_##level).get() << pthread_self() \ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 45 | << timestr() << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " |
| 46 | |
| 47 | #define IS_LOG_LEVEL(wLevel) (gGetLoggingLevel(__FILE__)>=LOG_##wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 48 | |
| 49 | #ifdef NDEBUG |
| 50 | #define LOG(wLevel) \ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 51 | if (LOG_##wLevel!=LOG_DEBUG && IS_LOG_LEVEL(wLevel)) _LOG(wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 52 | #else |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 53 | #define LOG(wLevel) \ |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 54 | if (IS_LOG_LEVEL(wLevel)) _LOG(wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 55 | #endif |
| 56 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 57 | // pat: And for your edification here are the 'levels' as defined in syslog.h: |
| 58 | // LOG_EMERG 0 system is unusable |
| 59 | // LOG_ALERT 1 action must be taken immediately |
| 60 | // LOG_CRIT 2 critical conditions |
| 61 | // LOG_ERR 3 error conditions |
| 62 | // LOG_WARNING 4 warning conditions |
| 63 | // LOG_NOTICE 5 normal, but significant, condition |
| 64 | // LOG_INFO 6 informational message |
| 65 | // LOG_DEBUG 7 debug-level message |
| 66 | |
| 67 | // (pat) added - print out a var and its name. |
| 68 | // Use like this: int descriptive_name; LOG(INFO)<<LOGVAR(descriptive_name); |
| 69 | #define LOGVAR2(name,val) " " << name << "=" << (val) |
| 70 | #define LOGVAR(var) (" " #var "=") << var |
| 71 | #define LOGHEX(var) (" " #var "=0x") << hex << ((unsigned)var) << dec |
| 72 | #define LOGHEX2(name,val) " " << name << "=0x" << hex << ((unsigned)(val)) << dec |
| 73 | // These are kind of cheesy, but you can use for bitvector |
| 74 | #define LOGBV2(name,val) " " << name << "=(" << val<<" size:"<<val.size()<<")" |
| 75 | #define LOGBV(bv) LOGBV2(#bv,bv) |
| 76 | #define LOGVARRANGE(name,cur,lo,hi) " "<<name <<"=("<<(cur) << " range:"<<(lo) << " to "<<(hi) <<")" |
| 77 | |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 78 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 79 | #define OBJLOG(wLevel) \ |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 80 | LOG(wLevel) << "obj: " << this << ' ' |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 81 | |
| 82 | #define LOG_ASSERT(x) { if (!(x)) LOG(EMERG) << "assertion " #x " failed"; } assert(x); |
| 83 | |
| 84 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 85 | #include "Threads.h" // must be after defines above, if these files are to be allowed to use LOG() |
| 86 | #include "Utils.h" |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | A C++ stream-based thread-safe logger. |
| 90 | Derived from Dr. Dobb's Sept. 2007 issue. |
| 91 | Updated to use syslog. |
| 92 | This object is NOT the global logger; |
| 93 | every log record is an object of this class. |
| 94 | */ |
| 95 | class Log { |
| 96 | |
| 97 | public: |
| 98 | |
| 99 | protected: |
| 100 | |
| 101 | std::ostringstream mStream; ///< This is where we buffer up the log entry. |
kurtis.heimerl | eb63ec9 | 2012-12-17 11:21:32 +0000 | [diff] [blame] | 102 | int mPriority; ///< Priority of current report. |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 103 | bool mDummyInit; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 104 | |
| 105 | public: |
| 106 | |
| 107 | Log(int wPriority) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 108 | :mPriority(wPriority), mDummyInit(false) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 109 | { } |
| 110 | |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 111 | Log(const char* name, const char* level=NULL, int facility=LOG_USER); |
| 112 | |
kurtis.heimerl | eb63ec9 | 2012-12-17 11:21:32 +0000 | [diff] [blame] | 113 | // Most of the work is in the destructor. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 114 | /** The destructor actually generates the log entry. */ |
| 115 | ~Log(); |
| 116 | |
| 117 | std::ostringstream& get(); |
| 118 | }; |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 119 | extern bool gLogToConsole; // Pat added for easy debugging. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 120 | |
| 121 | |
| 122 | |
| 123 | std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm list. |
| 124 | |
| 125 | |
| 126 | /**@ Global control and initialization of the logging system. */ |
| 127 | //@{ |
| 128 | /** Initialize the global logging system. */ |
| 129 | void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER); |
| 130 | /** Get the logging level associated with a given file. */ |
| 131 | int gGetLoggingLevel(const char *filename=NULL); |
kurtis.heimerl | 00913d7 | 2012-12-16 06:08:18 +0000 | [diff] [blame] | 132 | /** Allow early logging when still in constructors */ |
| 133 | void gLogEarly(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3))); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 134 | //@} |
| 135 | |
| 136 | |
| 137 | #endif |
| 138 | |
| 139 | // vim: ts=4 sw=4 |