| 1 |
// Logger class - for central logging to console/syslog/file |
| 2 |
// |
| 3 |
// Author : Klaus-Dieter Gundermann |
| 4 |
// Created : 24.06.2020 |
| 5 |
// |
| 6 |
// For all support, instructions and copyright go to: |
| 7 |
// http://e2guardian.org/ |
| 8 |
// Released under the GPL v2, with the OpenSSL exception described in the README file. |
| 9 |
|
| 10 |
#ifndef __HPP_LOGGING |
| 11 |
#define __HPP_LOGGING |
| 12 |
|
| 13 |
#include <vector> |
| 14 |
//#include <sstream> |
| 15 |
#include <fstream> |
| 16 |
#include <sstream> |
| 17 |
#include <cstdio> |
| 18 |
#include <atomic> |
| 19 |
#include <syslog.h> |
| 20 |
#include <unistd.h> |
| 21 |
#include <sys/stat.h> |
| 22 |
#include "String.hpp" |
| 23 |
#include "UdpSocket.hpp" |
| 24 |
|
| 25 |
// only C++14 : using namespace std::string_literals; |
| 26 |
|
| 27 |
class FileRec { |
| 28 |
public: |
| 29 |
std::string filename; |
| 30 |
std::ofstream *file_stream = nullptr; |
| 31 |
int link_count = 0; |
| 32 |
bool open = false; |
| 33 |
bool write(std::string &msg); |
| 34 |
bool rotate(); |
| 35 |
bool flush(); |
| 36 |
}; |
| 37 |
|
| 38 |
|
| 39 |
class UdpRec { |
| 40 |
public: |
| 41 |
std::string host = ""; |
| 42 |
int port = 0; |
| 43 |
std::string ip = ""; |
| 44 |
UdpSocket *socket = nullptr; |
| 45 |
int link_count = 0; |
| 46 |
bool open = false; |
| 47 |
|
| 48 |
bool send(std::string &msg); |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
enum class LoggerSource { |
| 53 |
none, |
| 54 |
// used in production: |
| 55 |
info, error, warning, accesslog, requestlog, storytrace, dstatslog, responselog, alertlog, |
| 56 |
// only usable when compiled with DEBUG_LOW: |
| 57 |
debug, trace, network, story, regexp, config, content, |
| 58 |
// only usable when compiled with DEBUG_HIGH: |
| 59 |
icap, avscan, auth, dwload, proxy, thttps, |
| 60 |
__Max_Value |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
enum class LoggerDestination { |
| 65 |
// stdout and stderr refactored to upper case to avoid conflict with library stdout stderr macros |
| 66 |
none, STDOUT, STDERR, syslog, file, udp, |
| 67 |
__Max_Value |
| 68 |
}; |
| 69 |
|
| 70 |
class Logger { |
| 71 |
public: |
| 72 |
Logger(); // constructor |
| 73 |
~Logger(); // destructor |
| 74 |
|
| 75 |
void setSyslogName(const std::string logname); |
| 76 |
|
| 77 |
// enable/disable Logging Sources |
| 78 |
void enable(const LoggerSource source); |
| 79 |
|
| 80 |
void enable(const char *source); |
| 81 |
|
| 82 |
void disable(const LoggerSource source); |
| 83 |
|
| 84 |
void disable(const char *source); |
| 85 |
|
| 86 |
bool isEnabled(const LoggerSource source); |
| 87 |
|
| 88 |
bool isEnabled(const char *source); |
| 89 |
|
| 90 |
bool rotate(const LoggerSource source); |
| 91 |
bool flush(const LoggerSource source); |
| 92 |
|
| 93 |
bool setLogOutput(const LoggerSource source, const LoggerDestination destination, const std::string filename = "", |
| 94 |
const bool alsoEnable = true); |
| 95 |
|
| 96 |
void setDockerMode(); |
| 97 |
|
| 98 |
void log(const LoggerSource source, std::string message); |
| 99 |
|
| 100 |
void log(const LoggerSource source, const std::string func, const std::string file, const int line, std::string message); |
| 101 |
|
| 102 |
// Conversion Enum <-> string |
| 103 |
std::vector <std::string> Sources = {"none", |
| 104 |
"info", "error", "warning", "accesslog", "requestlog", "storytrace", "dstatslog", "responselog", "alertlog", |
| 105 |
// only usable when compiled with DEBUG_LOW: |
| 106 |
"debug", "trace", "network", "story", "regexp", "config", "content", |
| 107 |
// only usable when compiled with DEBUG_HIGH: |
| 108 |
"icap", "avscan", "auth", "dwload", "proxy", "thttps"}; |
| 109 |
|
| 110 |
std::vector <std::string> Destinations = {"none", "stdout", "stderr", "syslog", "file", "udp"}; |
| 111 |
|
| 112 |
std::vector <LoggerSource> working_messages = { |
| 113 |
LoggerSource::info, |
| 114 |
LoggerSource::error, |
| 115 |
LoggerSource::warning, |
| 116 |
}; |
| 117 |
|
| 118 |
std::vector <LoggerSource> working_logs = { |
| 119 |
LoggerSource::accesslog, |
| 120 |
LoggerSource::requestlog, |
| 121 |
LoggerSource::dstatslog, |
| 122 |
LoggerSource::responselog, |
| 123 |
LoggerSource::alertlog, |
| 124 |
}; |
| 125 |
|
| 126 |
std::vector <LoggerSource> debug_messages = { |
| 127 |
LoggerSource::trace, |
| 128 |
LoggerSource::config, |
| 129 |
LoggerSource::debug, |
| 130 |
LoggerSource::avscan, |
| 131 |
LoggerSource::icap, |
| 132 |
LoggerSource::network, |
| 133 |
LoggerSource::regexp, |
| 134 |
LoggerSource::story, |
| 135 |
LoggerSource::auth, |
| 136 |
LoggerSource::dwload, |
| 137 |
LoggerSource::proxy, |
| 138 |
LoggerSource::thttps, |
| 139 |
LoggerSource::content, |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
LoggerSource string2source(std::string source); |
| 144 |
|
| 145 |
LoggerDestination string2dest(std::string destination); |
| 146 |
|
| 147 |
std::string source2string(LoggerSource source); |
| 148 |
|
| 149 |
std::string dest2string(LoggerDestination dest); |
| 150 |
|
| 151 |
void setFormat(const LoggerSource source, bool no_format, bool show_tag = false, bool show_func = false, |
| 152 |
bool func_last = true, bool show_thread_id = true, bool show_timestamp = true); |
| 153 |
|
| 154 |
void setMultiFormat(std::vector <LoggerSource> *source_list, bool no_format, bool show_tag, bool show_func, |
| 155 |
bool func_last, bool show_thread_id = true, bool show_timestamp = true); |
| 156 |
|
| 157 |
// Variable Args |
| 158 |
template<typename T> |
| 159 |
void cat_vars(std::stringstream &mess, T e) { |
| 160 |
mess << e; |
| 161 |
} |
| 162 |
|
| 163 |
template<typename T, typename... Args> |
| 164 |
void cat_vars(std::stringstream &mess, T e, Args... args) { |
| 165 |
mess << e; |
| 166 |
cat_vars(mess, args...); |
| 167 |
} |
| 168 |
|
| 169 |
template<typename... Args> |
| 170 |
std::string cat_all_vars(Args... args) { |
| 171 |
std::stringstream mess; |
| 172 |
cat_vars(mess, args...); |
| 173 |
return mess.str(); |
| 174 |
} |
| 175 |
|
| 176 |
template<typename... Args> |
| 177 |
void vlog(const LoggerSource source, const std::string func, const std::string file, const int line, Args... args) { |
| 178 |
log(source, func, file, line, cat_all_vars(args...)); |
| 179 |
|
| 180 |
}; |
| 181 |
|
| 182 |
|
| 183 |
private: |
| 184 |
|
| 185 |
std::vector <FileRec*> Files; |
| 186 |
std::vector <UdpRec*> Udps; |
| 187 |
|
| 188 |
struct SourceRec { |
| 189 |
bool enabled = false; |
| 190 |
LoggerDestination destination = LoggerDestination::none; |
| 191 |
FileRec *fileRec = nullptr; |
| 192 |
UdpRec *udpRec = nullptr; |
| 193 |
std::string host = ""; |
| 194 |
std::string port = ""; |
| 195 |
int syslog_flag = LOG_INFO; |
| 196 |
bool show_funct_line = false; |
| 197 |
bool funct_line_last = true; |
| 198 |
bool show_source_category = false; |
| 199 |
bool show_thread_id = true; |
| 200 |
bool no_format = false; |
| 201 |
bool show_timestamp = true; |
| 202 |
bool show_timestamp_active = true; |
| 203 |
}; |
| 204 |
|
| 205 |
SourceRec sourceRecs[static_cast<int>(LoggerSource::__Max_Value)]; |
| 206 |
|
| 207 |
|
| 208 |
FileRec *findFileRec(std::string filename); |
| 209 |
|
| 210 |
FileRec *addFile(std::string filename); |
| 211 |
|
| 212 |
void rmFileLink(FileRec *fileRec); |
| 213 |
|
| 214 |
void deleteFileEntry(std::string filename); |
| 215 |
|
| 216 |
UdpRec *findUdpRec(std::string host, int port); |
| 217 |
|
| 218 |
UdpRec *addUdp(std::string host, int port); |
| 219 |
|
| 220 |
void rmUdpLink(UdpRec *udpRec); |
| 221 |
|
| 222 |
void deleteUdpEntry(std::string host, int port); |
| 223 |
|
| 224 |
std::string _logname; |
| 225 |
|
| 226 |
// arrays below replaced with array of source_rec |
| 227 |
//bool _enabled[static_cast<int>(LoggerSource::__Max_Value)]; |
| 228 |
//LoggerDestination _destination[static_cast<int>(LoggerSource::__Max_Value)]; |
| 229 |
//std::string _filename[static_cast<int>(LoggerSource::__Max_Value)]; |
| 230 |
|
| 231 |
struct Helper; |
| 232 |
|
| 233 |
class Udp; |
| 234 |
|
| 235 |
void sendMessage(const LoggerSource source, std::string &message); |
| 236 |
|
| 237 |
void setDestination(const LoggerSource source, const LoggerDestination destination); |
| 238 |
|
| 239 |
bool setFilename(const LoggerSource source, const std::string filename); |
| 240 |
|
| 241 |
bool setUdpname(const LoggerSource source, const std::string filename); |
| 242 |
|
| 243 |
bool setUdpDestination(const LoggerSource source, const std::string udp_destination); |
| 244 |
|
| 245 |
bool setSyslogLevel(const LoggerSource source, const std::string filename); |
| 246 |
|
| 247 |
|
| 248 |
}; |
| 249 |
|
| 250 |
extern thread_local std::string thread_id; |
| 251 |
extern std::atomic<bool> g_is_starting; |
| 252 |
|
| 253 |
extern Logger e2logger; |
| 254 |
|
| 255 |
#define E2LOGGER_info(...) \ |
| 256 |
if (e2logger.isEnabled(LoggerSource::info)) \ |
| 257 |
e2logger.vlog(LoggerSource::info, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 258 |
|
| 259 |
#define E2LOGGER_error(...) \ |
| 260 |
if (e2logger.isEnabled(LoggerSource::error)) \ |
| 261 |
e2logger.vlog(LoggerSource::error, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 262 |
|
| 263 |
#define E2LOGGER_warning(...) \ |
| 264 |
if (e2logger.isEnabled(LoggerSource::warning)) \ |
| 265 |
e2logger.vlog(LoggerSource::warning, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 266 |
|
| 267 |
#define E2LOGGER_accesslog(STR) \ |
| 268 |
if (e2logger.isEnabled(LoggerSource::accesslog)) \ |
| 269 |
e2logger.log(LoggerSource::accesslog, STR) |
| 270 |
|
| 271 |
#define E2LOGGER_requestlog(STR) \ |
| 272 |
if (e2logger.isEnabled(LoggerSource::requestlog)) \ |
| 273 |
e2logger.log(LoggerSource::requestlog, STR) |
| 274 |
|
| 275 |
#define E2LOGGER_alertlog(STR) \ |
| 276 |
if (e2logger.isEnabled(LoggerSource::alertlog)) \ |
| 277 |
e2logger.log(LoggerSource::alertlog, STR) |
| 278 |
|
| 279 |
#define E2LOGGER_responselog(STR) \ |
| 280 |
if (e2logger.isEnabled(LoggerSource::responselog)) \ |
| 281 |
e2logger.log(LoggerSource::responselog, STR) |
| 282 |
|
| 283 |
#define E2LOGGER_dstatslog(STR) \ |
| 284 |
if (e2logger.isEnabled(LoggerSource::dstatslog)) \ |
| 285 |
e2logger.log(LoggerSource::dstatslog, STR) |
| 286 |
|
| 287 |
#define E2LOGGER_storytrace(...) \ |
| 288 |
if (e2logger.isEnabled(LoggerSource::storytrace)) \ |
| 289 |
e2logger.vlog(LoggerSource::storytrace, (const std::string) "", (const std::string) "", (int) 0, __VA_ARGS__) |
| 290 |
|
| 291 |
|
| 292 |
#ifdef DEBUG_HIGH |
| 293 |
#define DEBUG_icap(...) \ |
| 294 |
if (e2logger.isEnabled(LoggerSource::icap)) \ |
| 295 |
e2logger.vlog(LoggerSource::icap, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 296 |
|
| 297 |
#define DEBUG_avscan(...) \ |
| 298 |
if (e2logger.isEnabled(LoggerSource::avscan)) \ |
| 299 |
e2logger.vlog(LoggerSource::avscan, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 300 |
|
| 301 |
#define DEBUG_auth(...) \ |
| 302 |
if (e2logger.isEnabled(LoggerSource::auth)) \ |
| 303 |
e2logger.vlog(LoggerSource::auth, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 304 |
|
| 305 |
#define DEBUG_dwload(...) \ |
| 306 |
if (e2logger.isEnabled(LoggerSource::dwload)) \ |
| 307 |
e2logger.vlog(LoggerSource::dwload, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 308 |
|
| 309 |
#define DEBUG_proxy(...) \ |
| 310 |
if (e2logger.isEnabled(LoggerSource::proxy)) \ |
| 311 |
e2logger.vlog(LoggerSource::proxy, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 312 |
|
| 313 |
#define DEBUG_thttps(...) \ |
| 314 |
if (e2logger.isEnabled(LoggerSource::thttps)) \ |
| 315 |
e2logger.vlog(LoggerSource::thttps, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 316 |
|
| 317 |
#else |
| 318 |
#define DEBUG_icap(...) |
| 319 |
#define DEBUG_avscan(...) |
| 320 |
#define DEBUG_auth(...) |
| 321 |
#define DEBUG_dwload(...) |
| 322 |
#define DEBUG_proxy(...) |
| 323 |
#define DEBUG_thttps(...) |
| 324 |
#endif |
| 325 |
|
| 326 |
|
| 327 |
#ifdef DEBUG_LOW |
| 328 |
|
| 329 |
#define DEBUG_debug(...) \ |
| 330 |
if (e2logger.isEnabled(LoggerSource::debug)) \ |
| 331 |
e2logger.vlog(LoggerSource::debug, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 332 |
|
| 333 |
#define DEBUG_trace(...) \ |
| 334 |
if (e2logger.isEnabled(LoggerSource::trace)) \ |
| 335 |
e2logger.vlog(LoggerSource::trace, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 336 |
|
| 337 |
#define DEBUG_network(...) \ |
| 338 |
if (e2logger.isEnabled(LoggerSource::network)) \ |
| 339 |
e2logger.vlog(LoggerSource::network, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 340 |
|
| 341 |
#define DEBUG_regexp(...) \ |
| 342 |
if (e2logger.isEnabled(LoggerSource::regexp)) \ |
| 343 |
e2logger.vlog(LoggerSource::regexp, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 344 |
|
| 345 |
#define DEBUG_story(...) \ |
| 346 |
if (e2logger.isEnabled(LoggerSource::story)) \ |
| 347 |
e2logger.vlog(LoggerSource::story, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 348 |
|
| 349 |
#define DEBUG_config(...) \ |
| 350 |
if (e2logger.isEnabled(LoggerSource::config)) \ |
| 351 |
e2logger.vlog(LoggerSource::config, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 352 |
|
| 353 |
#define DEBUG_content(...) \ |
| 354 |
if (e2logger.isEnabled(LoggerSource::content)) \ |
| 355 |
e2logger.vlog(LoggerSource::content, __func__, __FILE__,__LINE__, __VA_ARGS__) |
| 356 |
|
| 357 |
#else |
| 358 |
#define DEBUG_debug(...) |
| 359 |
#define DEBUG_trace(...) |
| 360 |
#define DEBUG_network(...) |
| 361 |
#define DEBUG_regexp(...) |
| 362 |
#define DEBUG_story(...) |
| 363 |
#define DEBUG_config(...) |
| 364 |
#define DEBUG_content(...) |
| 365 |
#define DEBUG_low_icap(...) |
| 366 |
#define DEBUG_low_avscan(...) |
| 367 |
#define DEBUG_low_auth(...) |
| 368 |
#define DEBUG_low_dwload(...) |
| 369 |
#define DEBUG_low_proxy(...) |
| 370 |
#define DEBUG_low_thttps(...) |
| 371 |
#endif |
| 372 |
|
| 373 |
|
| 374 |
#endif |