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 | |
| 26 | |
| 27 | #ifndef LOGGER_H |
| 28 | #define LOGGER_H |
| 29 | |
| 30 | #include <syslog.h> |
| 31 | #include <stdint.h> |
| 32 | #include <stdio.h> |
| 33 | #include <sstream> |
| 34 | #include <list> |
| 35 | #include <map> |
| 36 | #include <string> |
| 37 | #include "Threads.h" |
| 38 | |
| 39 | |
| 40 | #define _LOG(level) \ |
| 41 | Log(LOG_##level).get() << pthread_self() \ |
| 42 | << " " __FILE__ ":" << __LINE__ << ":" << __FUNCTION__ << ": " |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 43 | |
| 44 | #ifdef NDEBUG |
| 45 | #define LOG(wLevel) \ |
| 46 | if (LOG_##wLevel!=LOG_DEBUG && gGetLoggingLevel(__FILE__)>=LOG_##wLevel) _LOG(wLevel) |
| 47 | #else |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 48 | #define LOG(wLevel) \ |
| 49 | if (gGetLoggingLevel(__FILE__)>=LOG_##wLevel) _LOG(wLevel) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
| 52 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 53 | #define OBJLOG(wLevel) \ |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 54 | LOG(wLevel) << "obj: " << this << ' ' |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 55 | |
| 56 | #define LOG_ASSERT(x) { if (!(x)) LOG(EMERG) << "assertion " #x " failed"; } assert(x); |
| 57 | |
| 58 | |
| 59 | #define DEFAULT_MAX_ALARMS 10 |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | A C++ stream-based thread-safe logger. |
| 64 | Derived from Dr. Dobb's Sept. 2007 issue. |
| 65 | Updated to use syslog. |
| 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 | |
| 75 | std::ostringstream mStream; ///< This is where we buffer up the log entry. |
| 76 | int mPriority; ///< Priority of current repot. |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 77 | bool mDummyInit; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 78 | |
| 79 | public: |
| 80 | |
| 81 | Log(int wPriority) |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 82 | :mPriority(wPriority), mDummyInit(false) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 83 | { } |
| 84 | |
dburgess | 7b2d522 | 2011-11-20 00:22:41 +0000 | [diff] [blame] | 85 | Log(const char* name, const char* level=NULL, int facility=LOG_USER); |
| 86 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 87 | // Most of the work is in the desctructor. |
| 88 | /** The destructor actually generates the log entry. */ |
| 89 | ~Log(); |
| 90 | |
| 91 | std::ostringstream& get(); |
| 92 | }; |
| 93 | |
| 94 | |
| 95 | |
| 96 | std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm list. |
| 97 | |
| 98 | |
| 99 | /**@ Global control and initialization of the logging system. */ |
| 100 | //@{ |
| 101 | /** Initialize the global logging system. */ |
| 102 | void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER); |
| 103 | /** Get the logging level associated with a given file. */ |
| 104 | int gGetLoggingLevel(const char *filename=NULL); |
kurtis.heimerl | 00913d7 | 2012-12-16 06:08:18 +0000 | [diff] [blame^] | 105 | /** Allow early logging when still in constructors */ |
| 106 | void gLogEarly(int level, const char *fmt, ...) __attribute__((format(printf, 2, 3))); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 107 | //@} |
| 108 | |
| 109 | |
| 110 | #endif |
| 111 | |
| 112 | // vim: ts=4 sw=4 |