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 | Mutex gLogToLock; |
| 41 | |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 42 | // Global log level threshold: |
| 43 | int config_log_level; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 44 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 45 | /** Names of the logging levels. */ |
| 46 | const char *levelNames[] = { |
| 47 | "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG" |
| 48 | }; |
| 49 | int numLevels = 8; |
| 50 | |
| 51 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 52 | int levelStringToInt(const string& name) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 53 | { |
| 54 | // Reverse search, since the numerically larger levels are more common. |
| 55 | for (int i=numLevels-1; i>=0; i--) { |
| 56 | if (name == levelNames[i]) return i; |
| 57 | } |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 58 | |
| 59 | // Common substitutions. |
| 60 | if (name=="INFORMATION") return 6; |
| 61 | if (name=="WARN") return 4; |
| 62 | if (name=="ERROR") return 3; |
| 63 | if (name=="CRITICAL") return 2; |
| 64 | if (name=="EMERGENCY") return 0; |
| 65 | |
| 66 | // Unknown level. |
| 67 | return -1; |
| 68 | } |
| 69 | |
Alexander Chemeris | 4793f46 | 2017-03-17 18:35:48 -0700 | [diff] [blame] | 70 | static std::string format(const char *fmt, ...) |
| 71 | { |
| 72 | va_list ap; |
| 73 | char buf[300]; |
| 74 | va_start(ap,fmt); |
| 75 | int n = vsnprintf(buf,300,fmt,ap); |
| 76 | va_end(ap); |
| 77 | if (n >= (300-4)) { strcpy(&buf[(300-4)],"..."); } |
| 78 | return std::string(buf); |
| 79 | } |
| 80 | |
| 81 | const std::string timestr() |
| 82 | { |
| 83 | struct timeval tv; |
| 84 | struct tm tm; |
| 85 | gettimeofday(&tv,NULL); |
| 86 | localtime_r(&tv.tv_sec,&tm); |
| 87 | unsigned tenths = tv.tv_usec / 100000; // Rounding down is ok. |
| 88 | return format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths); |
| 89 | } |
| 90 | |
| 91 | std::ostream& operator<<(std::ostream& os, std::ostringstream& ss) |
| 92 | { |
| 93 | return os << ss.str(); |
| 94 | } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 95 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 96 | Log::~Log() |
| 97 | { |
| 98 | // Anything at or above LOG_CRIT is an "alarm". |
Alexander Chemeris | cc6f79b | 2015-03-01 10:30:12 +0100 | [diff] [blame] | 99 | if (mPriority <= LOG_ERR) { |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 100 | cerr << mStream.str() << endl; |
| 101 | } |
Pau Espin Pedrol | 61837c0 | 2018-02-20 18:22:18 +0100 | [diff] [blame^] | 102 | |
| 103 | int mlen = mStream.str().size(); |
| 104 | int neednl = (mlen==0 || mStream.str()[mlen-1] != '\n'); |
| 105 | ScopedLock lock(gLogToLock); |
| 106 | // The COUT() macro prevents messages from stomping each other but adds uninteresting thread numbers, |
| 107 | // so just use std::cout. |
| 108 | std::cout << mStream.str(); |
| 109 | if (neednl) std::cout<<"\n"; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 110 | } |
| 111 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 112 | ostringstream& Log::get() |
| 113 | { |
| 114 | assert(mPriority<numLevels); |
| 115 | mStream << levelNames[mPriority] << ' '; |
| 116 | return mStream; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
Pau Espin Pedrol | 11d50d9 | 2018-02-20 17:52:28 +0100 | [diff] [blame] | 121 | void gLogInit(const char* level, char *fn) |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 122 | { |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 123 | // Set the level if one has been specified. |
Pau Espin Pedrol | f3837d2 | 2018-01-09 14:49:33 +0100 | [diff] [blame] | 124 | if (level) |
| 125 | config_log_level = levelStringToInt(level); |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 126 | } |
| 127 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 128 | // vim: ts=4 sw=4 |