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