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__ << ": " |
| 43 | #define LOG(wLevel) \ |
| 44 | if (gGetLoggingLevel(__FILE__)>=LOG_##wLevel) _LOG(wLevel) |
| 45 | #define OBJLOG(wLevel) \ |
| 46 | if (gGetLoggingLevel(__FILE__)>=LOG_##wLevel) _LOG(wLevel) << "obj: " << this << ' ' |
| 47 | |
| 48 | #define LOG_ASSERT(x) { if (!(x)) LOG(EMERG) << "assertion " #x " failed"; } assert(x); |
| 49 | |
| 50 | |
| 51 | #define DEFAULT_MAX_ALARMS 10 |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | A C++ stream-based thread-safe logger. |
| 56 | Derived from Dr. Dobb's Sept. 2007 issue. |
| 57 | Updated to use syslog. |
| 58 | This object is NOT the global logger; |
| 59 | every log record is an object of this class. |
| 60 | */ |
| 61 | class Log { |
| 62 | |
| 63 | public: |
| 64 | |
| 65 | protected: |
| 66 | |
| 67 | std::ostringstream mStream; ///< This is where we buffer up the log entry. |
| 68 | int mPriority; ///< Priority of current repot. |
| 69 | |
| 70 | public: |
| 71 | |
| 72 | Log(int wPriority) |
| 73 | :mPriority(wPriority) |
| 74 | { } |
| 75 | |
| 76 | // Most of the work is in the desctructor. |
| 77 | /** The destructor actually generates the log entry. */ |
| 78 | ~Log(); |
| 79 | |
| 80 | std::ostringstream& get(); |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | |
| 85 | std::list<std::string> gGetLoggerAlarms(); ///< Get a copy of the recent alarm list. |
| 86 | |
| 87 | |
| 88 | /**@ Global control and initialization of the logging system. */ |
| 89 | //@{ |
| 90 | /** Initialize the global logging system. */ |
| 91 | void gLogInit(const char* name, const char* level=NULL, int facility=LOG_USER); |
| 92 | /** Get the logging level associated with a given file. */ |
| 93 | int gGetLoggingLevel(const char *filename=NULL); |
| 94 | //@} |
| 95 | |
| 96 | |
| 97 | #endif |
| 98 | |
| 99 | // vim: ts=4 sw=4 |