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. |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 4 | * Copyright 2011, 2012 Range Networks, Inc. |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 5 | * |
| 6 | * |
| 7 | * This software is distributed under the terms of the GNU Affero Public License. |
| 8 | * See the COPYING file in the main directory for details. |
| 9 | * |
| 10 | * This use of this software may be subject to additional restrictions. |
| 11 | * See the LEGAL file in the main directory for details. |
| 12 | |
| 13 | This program is free software: you can redistribute it and/or modify |
| 14 | it under the terms of the GNU Affero General Public License as published by |
| 15 | the Free Software Foundation, either version 3 of the License, or |
| 16 | (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, |
| 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | GNU Affero General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU Affero General Public License |
| 24 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | |
| 26 | */ |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <cstdio> |
| 30 | #include <fstream> |
| 31 | #include <string> |
kurtis.heimerl | 00913d7 | 2012-12-16 06:08:18 +0000 | [diff] [blame] | 32 | #include <stdarg.h> |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 33 | #include <sys/time.h> // For gettimeofday |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 34 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | #include "Logger.h" |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 36 | #include "Threads.h" // pat added |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 37 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 38 | using namespace std; |
| 39 | |
Alexander Chemeris | a8cf208 | 2015-07-30 20:04:18 -0400 | [diff] [blame] | 40 | // Switches to enable/disable logging targets |
Alexander Chemeris | a8cf208 | 2015-07-30 20:04:18 -0400 | [diff] [blame] | 41 | bool gLogToConsole = true; |
Alexander Chemeris | a8cf208 | 2015-07-30 20:04:18 -0400 | [diff] [blame] | 42 | Mutex gLogToLock; |
| 43 | |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 44 | // Global log level threshold: |
| 45 | int config_log_level; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 46 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 47 | /** Names of the logging levels. */ |
| 48 | const char *levelNames[] = { |
| 49 | "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG" |
| 50 | }; |
| 51 | int numLevels = 8; |
| 52 | |
| 53 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 54 | int levelStringToInt(const string& name) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 55 | { |
| 56 | // Reverse search, since the numerically larger levels are more common. |
| 57 | for (int i=numLevels-1; i>=0; i--) { |
| 58 | if (name == levelNames[i]) return i; |
| 59 | } |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 60 | |
| 61 | // Common substitutions. |
| 62 | if (name=="INFORMATION") return 6; |
| 63 | if (name=="WARN") return 4; |
| 64 | if (name=="ERROR") return 3; |
| 65 | if (name=="CRITICAL") return 2; |
| 66 | if (name=="EMERGENCY") return 0; |
| 67 | |
| 68 | // Unknown level. |
| 69 | return -1; |
| 70 | } |
| 71 | |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 72 | static std::string format(const char *fmt, ...) |
| 73 | { |
| 74 | va_list ap; |
| 75 | char buf[300]; |
| 76 | va_start(ap,fmt); |
| 77 | int n = vsnprintf(buf,300,fmt,ap); |
| 78 | va_end(ap); |
| 79 | if (n >= (300-4)) { strcpy(&buf[(300-4)],"..."); } |
| 80 | return std::string(buf); |
| 81 | } |
| 82 | |
| 83 | const std::string timestr() |
| 84 | { |
| 85 | struct timeval tv; |
| 86 | struct tm tm; |
| 87 | gettimeofday(&tv,NULL); |
| 88 | localtime_r(&tv.tv_sec,&tm); |
| 89 | unsigned tenths = tv.tv_usec / 100000; // Rounding down is ok. |
| 90 | return format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); |
| 91 | } |
| 92 | |
| 93 | std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) |
| 94 | { |
| 95 | return os << ss.str(); |
| 96 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 97 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 98 | Log::~Log() |
| 99 | { |
| 100 | // Anything at or above LOG_CRIT is an "alarm". |
Alexander Chemeris | cc6f79b | 2015-03-01 10:30:12 +0100 | [diff] [blame] | 101 | if (mPriority <= LOG_ERR) { |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 102 | cerr << mStream.str() << endl; |
| 103 | } |
Alexander Chemeris | 5721920 | 2015-05-24 13:20:44 -0400 | [diff] [blame] | 104 | // Log to file and console |
Pau Espin Pedrol | 01aff88 | 2018-02-20 17:56:59 +0100 | [diff] [blame] | 105 | if (gLogToConsole) { |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 106 | int mlen = mStream.str().size(); |
| 107 | int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n'); |
Alexander Chemeris | 909ffbf | 2015-06-04 00:09:29 -0400 | [diff] [blame] | 108 | ScopedLock lock(gLogToLock); |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 109 | if (gLogToConsole) { |
| 110 | // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers, |
| 111 | // so just use std::cout. |
| 112 | std::cout << mStream.str(); |
| 113 | if (neednl) std::cout<<"\n"; |
| 114 | } |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 115 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 116 | } |
| 117 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 118 | ostringstream& Log::get() |
| 119 | { |
| 120 | assert(mPriority<numLevels); |
| 121 | mStream << levelNames[mPriority] << ' '; |
| 122 | return mStream; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
Pau Espin Pedrol | 11d50d9 | 2018-02-20 17:52:28 +0100 | [diff] [blame] | 127 | void gLogInit(const char* level, char *fn) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 128 | { |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 129 | // Set the level if one has been specified. |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 130 | if (level) |
| 131 | config_log_level = levelStringToInt(level); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 132 | } |
| 133 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 134 | // vim: ts=4 sw=4 |