|
|
@ -1,7 +1,11 @@ |
|
|
|
#include "../lib/log.hpp" |
|
|
|
#include "../lib/log.hpp" |
|
|
|
|
|
|
|
|
|
|
|
marcelb::log::log(string _dir, bool _isKeepOpen, bool _printInConsole) { |
|
|
|
namespace marcelb { |
|
|
|
|
|
|
|
namespace logging { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) { |
|
|
|
dir = _dir; |
|
|
|
dir = _dir; |
|
|
|
|
|
|
|
loglevel = _loglevel; |
|
|
|
isKeepOpen = _isKeepOpen; |
|
|
|
isKeepOpen = _isKeepOpen; |
|
|
|
printInConsole = _printInConsole; |
|
|
|
printInConsole = _printInConsole; |
|
|
|
|
|
|
|
|
|
|
@ -21,35 +25,38 @@ marcelb::log::log(string _dir, bool _isKeepOpen, bool _printInConsole) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool marcelb::log::isdir() { |
|
|
|
bool log::isdir() { |
|
|
|
struct stat sb; |
|
|
|
struct stat sb; |
|
|
|
return stat(dir.c_str(), &sb) == 0; |
|
|
|
return stat(dir.c_str(), &sb) == 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool marcelb::log::open() { |
|
|
|
bool log::open() { |
|
|
|
logfile = ofstream (path, ios_base::app); |
|
|
|
logfile = ofstream (path, ios_base::app); |
|
|
|
return logfile.is_open(); |
|
|
|
return logfile.is_open(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void marcelb::log::loose() { |
|
|
|
void log::loose() { |
|
|
|
logfile.close(); |
|
|
|
logfile.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void marcelb::log::setMoment() { |
|
|
|
void log::setMoment() { |
|
|
|
time_t rawtime; |
|
|
|
time_t rawtime; |
|
|
|
time (&rawtime); |
|
|
|
time (&rawtime); |
|
|
|
moment = localtime (&rawtime); |
|
|
|
moment = localtime (&rawtime); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void marcelb::log::put(string logline) { |
|
|
|
void log::put(string logline, Level _level) { |
|
|
|
|
|
|
|
if (_level < loglevel) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
io.lock(); |
|
|
|
io.lock(); |
|
|
|
if (printInConsole) { |
|
|
|
if (printInConsole) { |
|
|
|
cout << logline << endl; |
|
|
|
cout << logline << endl; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setMoment(); |
|
|
|
setMoment(); |
|
|
|
setPrefix(logline); |
|
|
|
setPrefix(logline, _level); |
|
|
|
|
|
|
|
|
|
|
|
if (day != moment->tm_mday) { |
|
|
|
if (day != moment->tm_mday) { |
|
|
|
if (isKeepOpen && logfile.is_open()) { |
|
|
|
if (isKeepOpen && logfile.is_open()) { |
|
|
@ -79,7 +86,7 @@ void marcelb::log::put(string logline) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void marcelb::log::setPath() { |
|
|
|
void log::setPath() { |
|
|
|
if (dir[dir.length()-1] != '/') { |
|
|
|
if (dir[dir.length()-1] != '/') { |
|
|
|
dir.push_back('/'); |
|
|
|
dir.push_back('/'); |
|
|
|
} |
|
|
|
} |
|
|
@ -90,17 +97,62 @@ void marcelb::log::setPath() { |
|
|
|
path = dir + to_string(moment->tm_year+1900) + '-' + mon.str() + '-' + _day.str() + ".log"; |
|
|
|
path = dir + to_string(moment->tm_year+1900) + '-' + mon.str() + '-' + _day.str() + ".log"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void marcelb::log::setPrefix(string &logline) { |
|
|
|
void log::setPrefix(string &logline, Level &_level) { |
|
|
|
stringstream hour, min, sec; |
|
|
|
stringstream hour, min, sec; |
|
|
|
hour << setw(2) << setfill('0') << moment->tm_hour; |
|
|
|
hour << setw(2) << setfill('0') << moment->tm_hour; |
|
|
|
min << setw(2) << setfill('0') << moment->tm_min; |
|
|
|
min << setw(2) << setfill('0') << moment->tm_min; |
|
|
|
sec << setw(2) << setfill('0') << moment->tm_sec; |
|
|
|
sec << setw(2) << setfill('0') << moment->tm_sec; |
|
|
|
|
|
|
|
|
|
|
|
string _logline = hour.str() + ':' + min.str() + ':' + sec.str() + ' ' + logline; |
|
|
|
string _logline = hour.str() + ':' + min.str() + ':' + sec.str();// + logline;
|
|
|
|
logline = _logline; |
|
|
|
switch (_level) { |
|
|
|
|
|
|
|
case DEBUG: |
|
|
|
|
|
|
|
_logline += " [DEBUG] "; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case INFO: |
|
|
|
|
|
|
|
_logline += " [INFO] "; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case WARNING: |
|
|
|
|
|
|
|
_logline += " [WARNING] "; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case ERROR: |
|
|
|
|
|
|
|
_logline += " [ERROR] "; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
case FATAL: |
|
|
|
|
|
|
|
_logline += " [FATAL] "; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
_logline += " [UNAKOWN] "; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
logline = _logline + logline; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void log::debug(string logline) { |
|
|
|
|
|
|
|
put(logline, DEBUG); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void log::info(string logline) { |
|
|
|
|
|
|
|
put(logline, INFO); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void log::warning(string logline) { |
|
|
|
|
|
|
|
put(logline, WARNING); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void log::error(string logline) { |
|
|
|
|
|
|
|
put(logline, ERROR); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void log::fatal(string logline) { |
|
|
|
|
|
|
|
put(logline, FATAL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
marcelb::log::~log() { |
|
|
|
|
|
|
|
|
|
|
|
log::~log() { |
|
|
|
loose(); |
|
|
|
loose(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|