blob: 4c2a2d373d1a3baed5561fa701f1370d675991c9 [file] [log] [blame]
dburgess82c46ff2011-10-07 02:40:51 +00001/*
2* Copyright 2009, 2010 Free Software Foundation, Inc.
3* Copyright 2010 Kestrel Signal Processing, Inc.
kurtis.heimerl5a872472013-05-31 21:47:25 +00004* Copyright 2011, 2012 Range Networks, Inc.
dburgess82c46ff2011-10-07 02:40:51 +00005*
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.heimerl00913d72012-12-16 06:08:18 +000032#include <stdarg.h>
Alexander Chemeris4793f462017-03-17 18:35:48 -070033#include <sys/time.h> // For gettimeofday
dburgess82c46ff2011-10-07 02:40:51 +000034
dburgess82c46ff2011-10-07 02:40:51 +000035#include "Logger.h"
kurtis.heimerl5a872472013-05-31 21:47:25 +000036#include "Threads.h" // pat added
dburgess82c46ff2011-10-07 02:40:51 +000037
dburgess82c46ff2011-10-07 02:40:51 +000038using namespace std;
39
Alexander Chemerisa8cf2082015-07-30 20:04:18 -040040Mutex gLogToLock;
41
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +010042// Global log level threshold:
43int config_log_level;
dburgess82c46ff2011-10-07 02:40:51 +000044
dburgess82c46ff2011-10-07 02:40:51 +000045/** Names of the logging levels. */
46const char *levelNames[] = {
47 "EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG"
48};
49int numLevels = 8;
50
51
kurtis.heimerl5a872472013-05-31 21:47:25 +000052int levelStringToInt(const string& name)
dburgess82c46ff2011-10-07 02:40:51 +000053{
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.heimerl5a872472013-05-31 21:47:25 +000058
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 Chemeris4793f462017-03-17 18:35:48 -070070static 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
81const 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
91std::ostream& operator<<(std::ostream& os, std::ostringstream& ss)
92{
93 return os << ss.str();
94}
dburgess82c46ff2011-10-07 02:40:51 +000095
dburgess82c46ff2011-10-07 02:40:51 +000096Log::~Log()
97{
98 // Anything at or above LOG_CRIT is an "alarm".
Alexander Chemeriscc6f79b2015-03-01 10:30:12 +010099 if (mPriority <= LOG_ERR) {
dburgess82c46ff2011-10-07 02:40:51 +0000100 cerr << mStream.str() << endl;
101 }
Pau Espin Pedrol61837c02018-02-20 18:22:18 +0100102
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";
dburgess82c46ff2011-10-07 02:40:51 +0000110}
111
dburgess82c46ff2011-10-07 02:40:51 +0000112ostringstream& Log::get()
113{
114 assert(mPriority<numLevels);
115 mStream << levelNames[mPriority] << ' ';
116 return mStream;
117}
118
119
120
Pau Espin Pedrol11d50d92018-02-20 17:52:28 +0100121void gLogInit(const char* level, char *fn)
dburgess82c46ff2011-10-07 02:40:51 +0000122{
kurtis.heimerl5a872472013-05-31 21:47:25 +0000123 // Set the level if one has been specified.
Pau Espin Pedrolf3837d22018-01-09 14:49:33 +0100124 if (level)
125 config_log_level = levelStringToInt(level);
dburgess82c46ff2011-10-07 02:40:51 +0000126}
127
dburgess82c46ff2011-10-07 02:40:51 +0000128// vim: ts=4 sw=4