logger.cpp
3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#include "logger.h"
#include <ctime>
#include <sstream>
#include <iomanip>
#include <string.h>
#include "exceptions.h"
Logger *Logger::instance = nullptr;
std::string Logger::logPath = "";
Logger::Logger()
{
}
std::string Logger::getLogLevelString(int level) const
{
switch (level)
{
case LOG_NONE:
return "NONE";
case LOG_INFO:
return "INFO";
case LOG_NOTICE:
return "NOTICE";
case LOG_WARNING:
return "WARNING";
case LOG_ERR:
return "ERROR";
case LOG_DEBUG:
return "DEBUG";
case LOG_SUBSCRIBE:
return "SUBSCRIBE";
case LOG_UNSUBSCRIBE:
return "UNSUBSCRIBE";
default:
return "UNKNOWN LOG LEVEL";
}
}
Logger *Logger::getInstance()
{
if (instance == nullptr)
instance = new Logger();
return instance;
}
void Logger::logf(int level, const char *str, ...)
{
va_list valist;
va_start(valist, str);
this->logf(level, str, valist);
va_end(valist);
}
void Logger::reOpen()
{
std::lock_guard<std::mutex> locker(logMutex);
if (file)
{
fclose(file);
file = nullptr;
}
if (logPath.empty())
return;
if ((file = fopen(logPath.c_str(), "a")) == nullptr)
{
std::string msg(strerror(errno));
throw ConfigFileException("Error opening logfile: " + msg);
}
}
// I want all messages logged during app startup to also show on stdout/err, otherwise failure can look so silent. So, call this when the app started.
void Logger::noLongerLogToStd()
{
alsoLogToStd = false;
}
void Logger::setLogPath(const std::string &path)
{
Logger::logPath = path;
}
void Logger::setFlags(bool logDebug, bool logSubscriptions)
{
std::lock_guard<std::mutex> locker(logMutex);
if (logDebug)
curLogLevel |= LOG_DEBUG;
else
curLogLevel &= ~LOG_DEBUG;
if (logSubscriptions)
curLogLevel |= (LOG_UNSUBSCRIBE & LOG_SUBSCRIBE);
else
curLogLevel &= ~(LOG_UNSUBSCRIBE & LOG_SUBSCRIBE);
}
void Logger::logf(int level, const char *str, va_list valist)
{
if ((level & curLogLevel) == 0)
return;
std::lock_guard<std::mutex> locker(logMutex);
time_t time = std::time(nullptr);
struct tm tm = *std::localtime(&time);
std::ostringstream oss;
std::string str_(str);
oss << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "] [" << getLogLevelString(level) << "] " << str_;
oss.flush();
const std::string s = oss.str();
const char *logfmtstring = s.c_str();
if (this->file)
{
va_list valist2;
va_copy(valist2, valist);
vfprintf(this->file, logfmtstring, valist2);
fprintf(this->file, "\n");
fflush(this->file);
va_end(valist2);
}
if (!this->file || alsoLogToStd)
{
#ifdef TESTING
vfprintf(stderr, logfmtstring, valist); // the stdout interfers with Qt test XML output, so using stderr.
fprintf(stderr, "\n");
fflush(stderr);
#else
vfprintf(stdout, logfmtstring, valist);
fprintf(stdout, "\n");
fflush(stdout);
#endif
}
}
int logSslError(const char *str, size_t len, void *u)
{
Logger *logger = Logger::getInstance();
logger->logf(LOG_ERR, str);
return 0;
}