@@ -19,50 +19,70 @@ void Log::init(std::string path) {
1919
2020void Log::setLogToTerminal (bool log, bool overwrite) {
2121 static bool initialized = false ;
22- if (overwrite || ! initialized) {
22+ if (overwrite || initialized == false ) {
2323 _log_to_terminal = log;
2424 initialized = true ;
2525 }
2626}
2727
2828void Log::setLevel (log_level_t level, bool overwrite) {
2929 static bool initialized = false ;
30- if (overwrite || ! initialized) {
30+ if (overwrite || initialized == false ) {
3131 _log_level = level;
3232 initialized = true ;
3333 }
3434}
3535
3636void Log::setFile (std::string path, bool overwrite) {
37- if (overwrite || ! _fileSet) {
37+ if (overwrite || _fileSet == false ) {
3838 File (path).createDirPath ();
3939 _path = path;
4040 if (_file.is_open ()) _file.close ();
4141 _file.open (path.c_str (), std::ios_base::app);
42- if (_file.good ()) _error = false ;
42+ if (_file.good ()) _logFileWriteError = false ;
4343 _fileSet = true ;
4444 }
4545}
4646
47- void Log::setInitialized (bool initialized ) { _initialized = initialized ; }
47+ void Log::setLogToFile (bool logToFile ) { _logToFile = logToFile ; }
4848
4949log_level_t Log::getLevel () { return _log_level; }
5050
51- bool Log::getInitialized () { return _initialized; }
51+ static std::string getLevelString (log_level_t level) {
52+ switch (level)
53+ {
54+ case ERROR:
55+ return " Error " ;
56+ case WARNING:
57+ return " Warning" ;
58+ case INFO:
59+ return " Info " ;
60+ case DEBUG:
61+ return " Debug " ;
62+ case VERBOSE:
63+ return " Verbose" ;
64+
65+ default :
66+ return " Unknown" ;
67+ }
68+ }
69+
70+ bool Log::getLogToFile () { return _logToFile; }
5271
5372void Log::write (std::string const &msg, log_level_t level, std::string color) {
5473 if (level > _log_level) return ;
55- std::string timeStamp = " [" + getTime (_dateFormat + " " + _timeFormat) + " ] " ;
74+ std::string timeStamp = " [" + getTime (_dateFormat + " " + _timeFormat) + " ]" ;
75+ std::string levelString = " [" + getLevelString (level) + " ] " ;
5676 if (_log_to_terminal) {
5777 if (color == RESET) color = getLevelColor (level);
58- _terminal << timeStamp << color << msg << RESET << std::endl;
78+ _terminal << timeStamp << color << levelString << msg << RESET << std::endl;
5979 }
60- if (!_initialized ) return ;
61- _file << timeStamp << msg << std::endl;
62- if (_file.good () || _error ) return ;
80+ if (getLogToFile () == false ) return ;
81+ _file << timeStamp << levelString << msg << std::endl;
82+ if (_file.good () || _logFileWriteError == true ) return ;
6383 std::cerr << timeStamp << BRIGHT_RED << " ERROR: " << RESET
6484 << " Unable to write to log file: " << _path << std::endl;
65- _error = true ;
85+ _logFileWriteError = true ;
6686}
6787
6888std::string Log::getLevelColor (log_level_t level) {
0 commit comments