| 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 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include "e2config.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
#include <iostream> |
| 15 |
#include <fstream> |
| 16 |
#include <sstream> |
| 17 |
#include <string> |
| 18 |
#include <syslog.h> |
| 19 |
#include <cstdio> |
| 20 |
#include <sys/socket.h> |
| 21 |
#include <netdb.h> |
| 22 |
#include "Logger.hpp" |
| 23 |
|
| 24 |
extern bool is_daemonised; |
| 25 |
|
| 26 |
// ------------------------------------------------------------- |
| 27 |
// --- Global Logger |
| 28 |
// ------------------------------------------------------------- |
| 29 |
|
| 30 |
Logger e2logger; |
| 31 |
|
| 32 |
// ------------------------------------------------------------- |
| 33 |
// --- Constructor |
| 34 |
// ------------------------------------------------------------- |
| 35 |
Logger::Logger() { |
| 36 |
setSyslogName(PACKAGE); |
| 37 |
// set up default output formats |
| 38 |
// bools are no_format, show_tag, show_func, func_last |
| 39 |
setMultiFormat(&working_logs,true,false,false,false,false,false); |
| 40 |
setMultiFormat(&working_messages,false,false,false,false, true, true); |
| 41 |
setMultiFormat(&debug_messages,false,false,true, true, true, true); // this can be overwritten by the debuglevel option |
| 42 |
|
| 43 |
setFormat(LoggerSource::storytrace,false,false,false,false,true, true); |
| 44 |
|
| 45 |
setLogOutput(LoggerSource::info, LoggerDestination::syslog, "LOG_INFO"); |
| 46 |
setLogOutput(LoggerSource::error, LoggerDestination::syslog, "LOG_ERR"); |
| 47 |
setLogOutput(LoggerSource::warning, LoggerDestination::syslog, "LOG_WARNING"); |
| 48 |
|
| 49 |
setLogOutput(LoggerSource::storytrace, LoggerDestination::syslog,"LOG_INFO", false); |
| 50 |
|
| 51 |
setLogOutput(LoggerSource::debug, LoggerDestination::STDOUT, "", false); |
| 52 |
setLogOutput(LoggerSource::trace, LoggerDestination::STDOUT, "", false); |
| 53 |
} |
| 54 |
|
| 55 |
Logger::~Logger() { |
| 56 |
closelog(); |
| 57 |
if (!Files.empty()) { |
| 58 |
for (std::vector<FileRec *>::iterator i = Files.begin(); i != Files.end(); i++) { |
| 59 |
if (*i != nullptr) { |
| 60 |
if ((*i)->open) { |
| 61 |
((*i)->file_stream)->close(); |
| 62 |
delete ((*i)->file_stream); |
| 63 |
} |
| 64 |
delete *i; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
if (!Udps.empty()) { |
| 69 |
for (std::vector<UdpRec *>::iterator i = Udps.begin(); i != Udps.end(); i++) { |
| 70 |
if (*i != nullptr) { |
| 71 |
if ((*i)->open) { |
| 72 |
((*i)->socket)->close(); |
| 73 |
delete ((*i)->socket); |
| 74 |
} |
| 75 |
delete *i; |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// ------------------------------------------------------------- |
| 82 |
// --- Helper |
| 83 |
// ------------------------------------------------------------- |
| 84 |
|
| 85 |
struct Logger::Helper { |
| 86 |
static std::string build_message(SourceRec *rec, std::string &prepend, const std::string &func, |
| 87 |
const std::string &file, const int &line, std::string what) { |
| 88 |
// TODO: add time properly in separate commit PIP |
| 89 |
std::string message; |
| 90 |
if (rec->show_timestamp_active) { |
| 91 |
time_t t = time(NULL); |
| 92 |
message = (std::to_string((long)t)); |
| 93 |
message += " "; |
| 94 |
} |
| 95 |
if (rec->show_source_category) { |
| 96 |
((message = "(") += prepend) += ") "; |
| 97 |
} |
| 98 |
if (rec->show_thread_id) |
| 99 |
message += thread_id; |
| 100 |
if (rec->show_funct_line && !rec->funct_line_last) { |
| 101 |
message.append(func).append("():").append(file).append(":").append(std::to_string(line)).append(" "); |
| 102 |
} |
| 103 |
message += what; |
| 104 |
if (rec->show_funct_line && rec->funct_line_last) { |
| 105 |
message.append(" ").append(func).append("():").append(file).append(":").append(std::to_string(line)); |
| 106 |
} |
| 107 |
if( rec->destination == LoggerDestination::udp) { |
| 108 |
message.append("\n"); |
| 109 |
} |
| 110 |
return message; |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
class Logger::Udp { |
| 115 |
public: |
| 116 |
|
| 117 |
}; |
| 118 |
|
| 119 |
bool UdpRec::send(std::string &msg) { |
| 120 |
if (socket == nullptr) { |
| 121 |
std::cerr << "socket is null" << std::endl; |
| 122 |
} else { |
| 123 |
//std::cerr << "socket is not null" << std::endl; |
| 124 |
if (!socket->writeString(msg)) { |
| 125 |
std::cerr << "udp write to " << host << " failed"; |
| 126 |
return false; |
| 127 |
}; |
| 128 |
//std::cerr << "udp write to " << host << " OK"; |
| 129 |
} |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
bool FileRec::write(std::string &msg) { |
| 134 |
if (file_stream == nullptr) { |
| 135 |
std::cerr << "file_stream is null" << std::endl; |
| 136 |
} else { |
| 137 |
// std::cerr << "file_stream is not null" << std::endl; |
| 138 |
*file_stream << msg << std::endl; |
| 139 |
if (file_stream->fail()) { |
| 140 |
//std::cerr << "log write to " << filename << " failed"; |
| 141 |
return false; |
| 142 |
} |
| 143 |
// std::cerr << "log write to " << filename << " OK"; |
| 144 |
} |
| 145 |
return true; |
| 146 |
} |
| 147 |
bool FileRec::flush() { |
| 148 |
if(file_stream == nullptr) |
| 149 |
return false; |
| 150 |
file_stream->flush(); |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
bool FileRec::rotate() { // this must only be called by a single thread which controls all output to this file |
| 155 |
if(file_stream == nullptr) |
| 156 |
return false; |
| 157 |
std::string rfn = filename; |
| 158 |
mode_t old_umask; |
| 159 |
old_umask = umask(S_IWGRP | S_IWOTH); |
| 160 |
rfn += ".old"; |
| 161 |
if(link(filename.c_str(), rfn.c_str()) == 0) { |
| 162 |
unlink(filename.c_str()); |
| 163 |
file_stream->close(); |
| 164 |
delete file_stream; |
| 165 |
file_stream = new std::ofstream(filename.c_str(), std::ios::app); |
| 166 |
umask(old_umask); |
| 167 |
return true; |
| 168 |
} |
| 169 |
umask(old_umask); |
| 170 |
return false; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
// ------------------------------------------------------------- |
| 175 |
// --- static Functions |
| 176 |
// ------------------------------------------------------------- |
| 177 |
|
| 178 |
LoggerSource Logger::string2source(std::string source) { |
| 179 |
for (int i = 0; i < static_cast<int>(LoggerSource::__Max_Value); i++) { |
| 180 |
if (source == Sources[i]) return static_cast<LoggerSource>(i); |
| 181 |
} |
| 182 |
return LoggerSource::none; |
| 183 |
} |
| 184 |
|
| 185 |
LoggerDestination Logger::string2dest(std::string destination) { |
| 186 |
for (int i = 0; i < static_cast<int>(LoggerDestination::__Max_Value); i++) { |
| 187 |
if (Destinations[i] == destination) return static_cast<LoggerDestination>(i); |
| 188 |
} |
| 189 |
return LoggerDestination::none; |
| 190 |
} |
| 191 |
|
| 192 |
std::string Logger::source2string(LoggerSource source) { |
| 193 |
return Sources[static_cast<int>(source)]; |
| 194 |
} |
| 195 |
|
| 196 |
std::string Logger::dest2string(LoggerDestination dest) { |
| 197 |
return Destinations[static_cast<int>(dest)]; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
// ------------------------------------------------------------- |
| 202 |
// --- Properties |
| 203 |
// ------------------------------------------------------------- |
| 204 |
|
| 205 |
void Logger::setSyslogName(const std::string logname) { |
| 206 |
_logname = logname; |
| 207 |
closelog(); |
| 208 |
openlog(logname.c_str(), LOG_PID | LOG_CONS, LOG_USER); |
| 209 |
}; |
| 210 |
|
| 211 |
void Logger::enable(const LoggerSource source) { |
| 212 |
sourceRecs[static_cast<int>(source)].enabled = true; |
| 213 |
}; |
| 214 |
|
| 215 |
void Logger::enable(const char *source) { |
| 216 |
enable(string2source(std::string(source))); |
| 217 |
} |
| 218 |
|
| 219 |
void Logger::disable(const LoggerSource source) { |
| 220 |
sourceRecs[static_cast<int>(source)].enabled = false; |
| 221 |
}; |
| 222 |
|
| 223 |
void Logger::disable(const char *source) { |
| 224 |
disable(string2source(std::string(source))); |
| 225 |
} |
| 226 |
|
| 227 |
bool Logger::isEnabled(const LoggerSource source) { |
| 228 |
return sourceRecs[static_cast<int>(source)].enabled; |
| 229 |
}; |
| 230 |
|
| 231 |
bool Logger::isEnabled(const char *source) { |
| 232 |
return isEnabled(string2source(std::string(source))); |
| 233 |
} |
| 234 |
|
| 235 |
bool Logger::rotate(const LoggerSource source) { // this must only be called by a single thread which controls all output to this file |
| 236 |
if(sourceRecs[static_cast<int>(source)].destination != LoggerDestination::file) |
| 237 |
return false; |
| 238 |
if(sourceRecs[static_cast<int>(source)].fileRec == nullptr) |
| 239 |
return false; |
| 240 |
return sourceRecs[static_cast<int>(source)].fileRec->rotate(); |
| 241 |
}; |
| 242 |
|
| 243 |
bool Logger::flush(const LoggerSource source) { // this must only be called by a single thread which controls all output to this file |
| 244 |
if(sourceRecs[static_cast<int>(source)].destination != LoggerDestination::file) |
| 245 |
return false; |
| 246 |
if(sourceRecs[static_cast<int>(source)].fileRec == nullptr) |
| 247 |
return false; |
| 248 |
return sourceRecs[static_cast<int>(source)].fileRec->flush(); |
| 249 |
}; |
| 250 |
|
| 251 |
bool Logger::setLogOutput(const LoggerSource source, const LoggerDestination destination, const std::string filename, |
| 252 |
const bool alsoEnable) { |
| 253 |
if (destination == LoggerDestination::file) { |
| 254 |
if (!setFilename(source, filename)) |
| 255 |
return false; |
| 256 |
} else if (sourceRecs[static_cast<int>(source)].destination == LoggerDestination::file) { // unlink file if previously set |
| 257 |
rmFileLink(sourceRecs[static_cast<int>(source)].fileRec); |
| 258 |
sourceRecs[static_cast<int>(source)].fileRec = nullptr; |
| 259 |
} |
| 260 |
|
| 261 |
if (destination == LoggerDestination::udp) { |
| 262 |
if (!setUdpname(source, filename)) |
| 263 |
return false; |
| 264 |
} else if (sourceRecs[static_cast<int>(source)].destination == LoggerDestination::udp) { // unlink file if previously set |
| 265 |
rmUdpLink(sourceRecs[static_cast<int>(source)].udpRec); |
| 266 |
sourceRecs[static_cast<int>(source)].udpRec = nullptr; |
| 267 |
} |
| 268 |
|
| 269 |
// if (destination == LoggerDestination::udp) { |
| 270 |
// if (!setUdpDestination(source, filename)) |
| 271 |
// return false; |
| 272 |
// } |
| 273 |
|
| 274 |
if (destination == LoggerDestination::syslog) { |
| 275 |
setSyslogLevel(source, filename); |
| 276 |
sourceRecs[static_cast<int>(source)].show_timestamp_active = false; |
| 277 |
} else { |
| 278 |
if (sourceRecs[static_cast<int>(source)].show_timestamp) { |
| 279 |
sourceRecs[static_cast<int>(source)].show_timestamp_active = true; |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
setDestination(source, destination); |
| 284 |
|
| 285 |
if (destination == LoggerDestination::none) |
| 286 |
disable(source); |
| 287 |
else if (alsoEnable) |
| 288 |
enable(source); |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
void Logger::setFormat(const LoggerSource source, bool no_format, bool show_tag, bool show_func, bool func_last, bool show_thread_id, bool show_timestamp) { |
| 293 |
sourceRecs[static_cast<int>(source)].no_format = no_format; // used for logs that do not required any further formating |
| 294 |
sourceRecs[static_cast<int>(source)].show_source_category = show_tag; |
| 295 |
sourceRecs[static_cast<int>(source)].show_funct_line = show_func; |
| 296 |
sourceRecs[static_cast<int>(source)].funct_line_last = func_last; |
| 297 |
sourceRecs[static_cast<int>(source)].show_thread_id = show_thread_id; |
| 298 |
sourceRecs[static_cast<int>(source)].show_timestamp = show_timestamp; |
| 299 |
}; |
| 300 |
|
| 301 |
void Logger::setMultiFormat(std::vector <LoggerSource> *source_list, bool no_format, bool show_tag, bool show_func, |
| 302 |
bool func_last, bool show_thread_id, bool show_timestamp) { |
| 303 |
for (std::vector<LoggerSource>::iterator i = source_list->begin(); i != source_list->end(); i++) { |
| 304 |
setFormat(*i, no_format, show_tag, show_func, func_last, show_thread_id, show_timestamp); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
FileRec *Logger::findFileRec(std::string filename) { |
| 309 |
if (!Files.empty()) { |
| 310 |
for (std::vector<FileRec*>::iterator i = Files.begin(); i != Files.end(); i++) { |
| 311 |
if ((*i)->filename == filename) |
| 312 |
return (*i); |
| 313 |
} |
| 314 |
} |
| 315 |
return nullptr; |
| 316 |
} |
| 317 |
|
| 318 |
void Logger::deleteFileEntry(std::string filename) { |
| 319 |
if (!Files.empty()) { |
| 320 |
for (std::vector<FileRec*>::iterator i = Files.begin(); i != Files.end(); i++) { |
| 321 |
if ((*i)->filename == filename) { |
| 322 |
if ((*i)->file_stream != nullptr) { |
| 323 |
((*i)->file_stream)->close(); |
| 324 |
delete (*i)->file_stream; |
| 325 |
(*i)->file_stream = nullptr; |
| 326 |
} |
| 327 |
delete *i; |
| 328 |
Files.erase(i); |
| 329 |
return; |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
FileRec *Logger::addFile(std::string filename) { |
| 336 |
FileRec *fileRec = findFileRec(filename); |
| 337 |
if (fileRec == nullptr) { // new unique filename - add to Files and open |
| 338 |
FileRec* fileRec1 = new FileRec; |
| 339 |
fileRec1->filename = filename; |
| 340 |
fileRec1->link_count = 1; |
| 341 |
Files.push_back(fileRec1); |
| 342 |
fileRec = findFileRec(filename); |
| 343 |
if (fileRec == nullptr) { |
| 344 |
std::cerr << "failure to find new Files record for " << filename << std::endl; |
| 345 |
return nullptr; |
| 346 |
} |
| 347 |
mode_t old_umask; |
| 348 |
old_umask = umask(S_IWGRP | S_IWOTH); |
| 349 |
// fileRec->file_stream = fopen(filename.c_str(), "a"); |
| 350 |
fileRec->file_stream = new std::ofstream(filename.c_str(), std::ios::app); |
| 351 |
if (!fileRec->file_stream) { |
| 352 |
std::cerr << "Failed to open/create logfile: " << filename << " (check ownership and access rights)" |
| 353 |
<< std::endl; |
| 354 |
deleteFileEntry(filename); |
| 355 |
umask(old_umask); |
| 356 |
return nullptr; |
| 357 |
} |
| 358 |
umask(old_umask); |
| 359 |
//std::cerr << "Opened new file: " << filename << std::endl; |
| 360 |
//std::cerr << "Opened new filename in record : " << fileRec->filename << std::endl; |
| 361 |
fileRec->open = true; |
| 362 |
//std::cerr << "File link count is " << fileRec->link_count << std::endl; |
| 363 |
} else { |
| 364 |
fileRec->link_count++; |
| 365 |
//std::cerr << "File link count is " << fileRec->link_count << std::endl; |
| 366 |
} |
| 367 |
return fileRec; |
| 368 |
}; |
| 369 |
|
| 370 |
void Logger::rmFileLink(FileRec *fileRec) { |
| 371 |
if(fileRec == nullptr) |
| 372 |
return; |
| 373 |
if (fileRec->link_count > 1) { |
| 374 |
fileRec->link_count--; |
| 375 |
//std::cerr << "rmFileLink File link count is " << fileRec->link_count << std::endl; |
| 376 |
return; |
| 377 |
} |
| 378 |
// link count will now be zero, close file, delete stream and remove record |
| 379 |
//std::cerr << "Close and delete " << fileRec->filename << std::endl; |
| 380 |
//fclose(fileRec->file_stream); |
| 381 |
if (fileRec->file_stream != nullptr) { |
| 382 |
fileRec->file_stream->close(); |
| 383 |
delete fileRec->file_stream; |
| 384 |
fileRec->file_stream = nullptr; |
| 385 |
} |
| 386 |
deleteFileEntry(fileRec->filename); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
void Logger::setDockerMode() { |
| 391 |
// docker stdout/stderr are not in sync |
| 392 |
// so for debugging send everything to stderr (unbuffered) |
| 393 |
setDestination(LoggerSource::info, LoggerDestination::STDERR); |
| 394 |
setDestination(LoggerSource::error, LoggerDestination::STDERR); |
| 395 |
setDestination(LoggerSource::warning, LoggerDestination::STDERR); |
| 396 |
setDestination(LoggerSource::accesslog, LoggerDestination::STDOUT); |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
// ------------------------------------------------------------- |
| 401 |
// --- Public Functions |
| 402 |
// ------------------------------------------------------------- |
| 403 |
|
| 404 |
void Logger::log(const LoggerSource source, std::string message) { |
| 405 |
log(source,(const std::string)"",(const std::string)"",(int)0, message); |
| 406 |
} |
| 407 |
|
| 408 |
void Logger::log(const LoggerSource source, const std::string func, const std::string file, const int line, std::string message) { |
| 409 |
SourceRec *sourceRec = &(sourceRecs[static_cast<int>(source)]); |
| 410 |
if (sourceRec->no_format && sourceRec->destination == LoggerDestination::file) { |
| 411 |
|
| 412 |
// following check is dane in sendMessage so remove |
| 413 |
// if (sourceRec->fileRec == nullptr ) { |
| 414 |
// std::cerr << "Error: in Log: filename is nullptr" << std::endl; |
| 415 |
// } else { |
| 416 |
// } |
| 417 |
|
| 418 |
//std::cerr << "in Log: filename is " << sourceRec->fileRec->filename << std::endl; |
| 419 |
sendMessage(source, message); |
| 420 |
} else { |
| 421 |
std::string tag; |
| 422 |
std::string msg; |
| 423 |
tag = source2string(source); |
| 424 |
msg = Helper::build_message(sourceRec, tag, func, file, line, message); |
| 425 |
sendMessage(source, msg); |
| 426 |
} |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
// ------------------------------------------------------------- |
| 431 |
// --- Private Functions |
| 432 |
// ------------------------------------------------------------- |
| 433 |
|
| 434 |
void Logger::sendMessage(const LoggerSource source, std::string &message) { |
| 435 |
SourceRec *srec = &(sourceRecs[static_cast<int>(source)]); |
| 436 |
LoggerDestination destination = srec->destination; |
| 437 |
|
| 438 |
//std::cerr << "sendMessage source " << source2string(source) << " dest " << dest2string(destination) << std::endl; |
| 439 |
|
| 440 |
switch (destination) { |
| 441 |
case LoggerDestination::none: |
| 442 |
break; |
| 443 |
case LoggerDestination::STDOUT: |
| 444 |
std::cout << message << std::endl; |
| 445 |
break; |
| 446 |
case LoggerDestination::STDERR: |
| 447 |
std::cerr << message << std::endl; |
| 448 |
break; |
| 449 |
case LoggerDestination::syslog: |
| 450 |
if (g_is_starting) |
| 451 |
std::cerr << message << std::endl; // show to console as well as syslog when not daemonised |
| 452 |
syslog(srec->syslog_flag, "%s", message.c_str()); |
| 453 |
break; |
| 454 |
case LoggerDestination::file: |
| 455 |
if (srec->fileRec == nullptr) { |
| 456 |
std::cerr << "dest fileRec is nullptr" << std::endl; |
| 457 |
} else { |
| 458 |
if (!srec->fileRec->open ) |
| 459 |
std::cerr << "Log file output stream is closed"; |
| 460 |
else { |
| 461 |
//std::cerr << "Log filename is " << srec->fileRec->filename << std::endl; |
| 462 |
//std::cerr << "log msg: " << message << std::endl; |
| 463 |
srec->fileRec->write(message); |
| 464 |
} |
| 465 |
} |
| 466 |
break; |
| 467 |
case LoggerDestination::udp: |
| 468 |
if (srec->udpRec == nullptr) { |
| 469 |
std::cerr << "dest udpRec is nullptr" << std::endl; |
| 470 |
} else { |
| 471 |
// std::cerr << "Log udp is " << srec->udpRec->host << std::endl; |
| 472 |
if(srec->udpRec->send(message)) { |
| 473 |
// std::cerr << "udp message sent" << std::endl; |
| 474 |
} else { |
| 475 |
std::cerr << "udp message failed" << std::endl; |
| 476 |
} |
| 477 |
} |
| 478 |
break; |
| 479 |
case LoggerDestination::__Max_Value: |
| 480 |
break; |
| 481 |
} |
| 482 |
|
| 483 |
} |
| 484 |
|
| 485 |
void Logger::setDestination(const LoggerSource source, const LoggerDestination destination) { |
| 486 |
sourceRecs[static_cast<int>(source)].destination = destination; |
| 487 |
// std::cerr << "Logger::setDestination " << source2string(source) << " -> " << dest2string(destination) << std::endl; |
| 488 |
} |
| 489 |
|
| 490 |
bool Logger::setSyslogLevel(const LoggerSource source, const std::string filename) { |
| 491 |
|
| 492 |
int loglevel = LOG_INFO; |
| 493 |
|
| 494 |
if (filename == "LOG_ERR") |
| 495 |
loglevel = LOG_ERR; |
| 496 |
else if (filename == "LOG_WARNING") |
| 497 |
loglevel = LOG_WARNING; |
| 498 |
else if (filename == "LOG_INFO") |
| 499 |
loglevel = LOG_INFO; |
| 500 |
else if (filename == "LOG_DEBUG") |
| 501 |
loglevel = LOG_DEBUG; |
| 502 |
else if (filename == "LOG_ALERT") |
| 503 |
loglevel = LOG_ALERT; |
| 504 |
else if (filename == "LOG_CRIT") |
| 505 |
loglevel = LOG_CRIT; |
| 506 |
else if (filename == "LOG_NOTICE") |
| 507 |
loglevel = LOG_NOTICE; |
| 508 |
else if (filename == "LOG_EMERG") |
| 509 |
loglevel = LOG_EMERG; |
| 510 |
|
| 511 |
sourceRecs[static_cast<int>(source)].syslog_flag = loglevel; |
| 512 |
return true; |
| 513 |
} |
| 514 |
|
| 515 |
bool Logger::setFilename(const LoggerSource source, const std::string filename) { |
| 516 |
|
| 517 |
std::string name; |
| 518 |
|
| 519 |
if (filename.empty()) { |
| 520 |
return false; |
| 521 |
} |
| 522 |
|
| 523 |
if (filename.front() == '/') // absolute path |
| 524 |
name = filename; |
| 525 |
else // relative to __LOGLOCATION |
| 526 |
name = std::string(__LOGLOCATION) + filename; |
| 527 |
|
| 528 |
if (sourceRecs[static_cast<int>(source)].destination == LoggerDestination::file) { // |
| 529 |
rmFileLink(sourceRecs[static_cast<int>(source)].fileRec); |
| 530 |
sourceRecs[static_cast<int>(source)].fileRec = nullptr; |
| 531 |
} |
| 532 |
|
| 533 |
FileRec *file_rec = addFile(name); |
| 534 |
if (file_rec == nullptr) { |
| 535 |
std::cerr << "Null returned from addFile()" << std::endl; |
| 536 |
return false; |
| 537 |
} |
| 538 |
|
| 539 |
sourceRecs[static_cast<int>(source)].fileRec = file_rec; |
| 540 |
// std::cerr << "Lookup filename after added to source_dests is " |
| 541 |
// << source_dests[static_cast<int>(source)].fileRec->filename << std::endl; |
| 542 |
|
| 543 |
return true; |
| 544 |
} |
| 545 |
|
| 546 |
bool Logger::setUdpname(const LoggerSource source, const std::string filename) { |
| 547 |
|
| 548 |
std::string host=""; |
| 549 |
String port; |
| 550 |
String temp = filename; |
| 551 |
|
| 552 |
|
| 553 |
if (filename.empty()) { |
| 554 |
return false; |
| 555 |
} |
| 556 |
host = temp.before(":"); |
| 557 |
port = temp.after(":"); |
| 558 |
|
| 559 |
if (sourceRecs[static_cast<int>(source)].destination == LoggerDestination::udp) { // |
| 560 |
rmUdpLink(sourceRecs[static_cast<int>(source)].udpRec); |
| 561 |
sourceRecs[static_cast<int>(source)].udpRec = nullptr; |
| 562 |
} |
| 563 |
|
| 564 |
UdpRec *udp_rec = addUdp(host, port.toInteger()); |
| 565 |
if (udp_rec == nullptr) { |
| 566 |
std::cerr << "Null returned from addUdp()" << std::endl; |
| 567 |
return false; |
| 568 |
} |
| 569 |
|
| 570 |
sourceRecs[static_cast<int>(source)].udpRec = udp_rec; |
| 571 |
// std::cerr << "Lookup filename after added to source_dests is " << |
| 572 |
// source_dests[static_cast<int>(source)].fileRec->filename << std::endl; |
| 573 |
|
| 574 |
return true; |
| 575 |
} |
| 576 |
#ifdef NOT_DEF |
| 577 |
bool Logger::setUdpDestination(const LoggerSource source, const std::string udp_destination) { |
| 578 |
|
| 579 |
std::string host=""; |
| 580 |
std::string port=""; |
| 581 |
|
| 582 |
std::size_t pos = udp_destination.find_last_of(':'); |
| 583 |
if ( pos > 0 ) { |
| 584 |
host = udp_destination.substr(0,pos); |
| 585 |
port = udp_destination.substr(pos+1); |
| 586 |
} |
| 587 |
if (host.empty() || port.empty()) |
| 588 |
{ |
| 589 |
std::cerr << "missing host and port for UDP destination " << udp_destination << std::endl; |
| 590 |
return false; |
| 591 |
} |
| 592 |
|
| 593 |
sourceRecs[static_cast<int>(source)].host = host; |
| 594 |
sourceRecs[static_cast<int>(source)].port = port; |
| 595 |
return true; |
| 596 |
} |
| 597 |
#endif |
| 598 |
|
| 599 |
|
| 600 |
UdpRec *Logger::addUdp(std::string host, int port) { |
| 601 |
UdpRec *udpRec = findUdpRec(host, port); |
| 602 |
if (udpRec == nullptr) { // new unique udpname - add to Udps and open |
| 603 |
UdpRec *udpRec1 = new UdpRec; |
| 604 |
udpRec1->host = host; |
| 605 |
udpRec1->port = port; |
| 606 |
udpRec1->link_count = 1; |
| 607 |
Udps.push_back(udpRec1); |
| 608 |
udpRec = findUdpRec(host, port); |
| 609 |
if (udpRec == nullptr) { |
| 610 |
std::cerr << "failure to find new Udps record for " << host << ":" << port << std::endl; |
| 611 |
return nullptr; |
| 612 |
} |
| 613 |
int rc = -1; |
| 614 |
String shost(host); |
| 615 |
if (shost.isIp()) { |
| 616 |
// std::cerr << "host is an ip" << std::endl; |
| 617 |
udpRec->socket = new UdpSocket; |
| 618 |
rc = udpRec->socket->connect(host, port); |
| 619 |
} else { // do dns lookup |
| 620 |
// std::cerr << "host is NOT an ip" << std::endl; |
| 621 |
struct addrinfo hints, *addrs, *addr; |
| 622 |
memset(&hints, 0, sizeof(addrinfo)); |
| 623 |
|
| 624 |
hints.ai_family = AF_INET; |
| 625 |
hints.ai_socktype = SOCK_DGRAM; |
| 626 |
hints.ai_protocol = IPPROTO_UDP; |
| 627 |
hints.ai_flags = 0; |
| 628 |
hints.ai_canonname = NULL; |
| 629 |
hints.ai_addr = NULL; |
| 630 |
hints.ai_next = NULL; |
| 631 |
|
| 632 |
String sport = port; |
| 633 |
|
| 634 |
int status = getaddrinfo(host.c_str(), NULL, &hints, &addrs); |
| 635 |
if (status != 0) { |
| 636 |
std::cerr << "can not find host " << host << std::endl; |
| 637 |
return nullptr; |
| 638 |
} |
| 639 |
char t[256]; |
| 640 |
for (addr = addrs; addr != nullptr; addr = addr->ai_next) { |
| 641 |
getnameinfo(addr->ai_addr, addr->ai_addrlen, t, sizeof(t), NULL, 0, NI_NUMERICHOST); |
| 642 |
udpRec->socket = new UdpSocket; |
| 643 |
if ((rc = udpRec->socket->connect(t, port)) < 0) |
| 644 |
continue; |
| 645 |
else |
| 646 |
break; |
| 647 |
} |
| 648 |
} |
| 649 |
if (rc < 0) { // so not able to connect on any ip |
| 650 |
std::cerr << "can not open socket to " << host << std::endl; |
| 651 |
deleteUdpEntry(host, port); |
| 652 |
return nullptr; |
| 653 |
} |
| 654 |
//std::cerr << "Opened new udpname in record : " << udpRec->host << " port " <<udpRec->port << std::endl; |
| 655 |
udpRec->open = true; |
| 656 |
//std::cerr << "Udp link count is " << udpRec->link_count << std::endl; |
| 657 |
} else { |
| 658 |
udpRec->link_count++; |
| 659 |
//std::cerr << "Udp link count is " << udpRec->link_count << std::endl; |
| 660 |
}; |
| 661 |
return udpRec; |
| 662 |
} |
| 663 |
|
| 664 |
void Logger::rmUdpLink(UdpRec *udpRec) { |
| 665 |
if (udpRec == nullptr) |
| 666 |
return; |
| 667 |
if (udpRec->link_count > 1) { |
| 668 |
udpRec->link_count--; |
| 669 |
//std::cerr << "rmUdpLink Udp link count is " << udpRec->link_count << std::endl; |
| 670 |
return; |
| 671 |
} |
| 672 |
// link count will now be zero, close udp, delete stream and remove record |
| 673 |
//std::cerr << "Close and delete " << udpRec->udpname << std::endl; |
| 674 |
//fclose(udpRec->udp_stream); |
| 675 |
if (udpRec->socket != nullptr) { |
| 676 |
udpRec->socket->close(); |
| 677 |
delete udpRec->socket; |
| 678 |
udpRec->socket = nullptr; |
| 679 |
} |
| 680 |
deleteUdpEntry(udpRec->host, udpRec->port); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
UdpRec *Logger::findUdpRec(std::string host, int port) { |
| 685 |
if (!Udps.empty()) { |
| 686 |
for (std::vector<UdpRec*>::iterator i = Udps.begin(); i != Udps.end(); i++) { |
| 687 |
if (((*i)->host == host) && ((*i)->port == port)) |
| 688 |
return (*i); |
| 689 |
} |
| 690 |
} |
| 691 |
return nullptr; |
| 692 |
} |
| 693 |
|
| 694 |
void Logger::deleteUdpEntry(std::string host, int port) { |
| 695 |
if (!Udps.empty()) { |
| 696 |
for (std::vector<UdpRec*>::iterator i = Udps.begin(); i != Udps.end(); i++) { |
| 697 |
if (((*i)->host == host) && ((*i)->port == port)) { |
| 698 |
if ((*i)->socket != nullptr) { |
| 699 |
((*i)->socket)->close(); |
| 700 |
delete (*i)->socket; |
| 701 |
(*i)->socket = nullptr; |
| 702 |
} |
| 703 |
delete *i; |
| 704 |
Udps.erase(i); |
| 705 |
return; |
| 706 |
} |
| 707 |
} |
| 708 |
} |
| 709 |
} |