diff --git a/README.md b/README.md index becfe07..014082f 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Logging errors to a file, daily file rotation, date and time stamps. Configurabl - Customizable directory for files - Daily file rotation - Protection of recording consecutive errors +- Log levels - Thread safe - Exceptions - The possibility of printing logs in the console @@ -31,12 +32,16 @@ using namespace marcelb; /** * Initialization and declaration */ -log mylog("../log"); +log mylog("../log", Level::INFO); /** * Put log in file */ -mylog.put("[EVENT] Start loging"); +mylog.debug("Debug loging"); +mylog.info("Info loging"); +mylog.warning("Warning loging"); +mylog.error("Error loging"); +mylog.fatal("Fatal loging"); ``` ## License diff --git a/lib/log.hpp b/lib/log.hpp index 88b98b6..a90ebbb 100644 --- a/lib/log.hpp +++ b/lib/log.hpp @@ -17,11 +17,20 @@ namespace marcelb { using namespace std; +typedef enum { + DEBUG = 0, + INFO, + WARNING, + ERROR, + FATAL, +} Level; + /** * Log class - used at the level of one log directory */ class log { string dir; + Level loglevel; bool isKeepOpen; bool printInConsole; ofstream logfile; @@ -58,7 +67,12 @@ class log { /** * Set log line time prefix */ - void setPrefix(string &logline); + void setPrefix(string &logline, Level &_level); + + /** + * Put string log in file + */ + void put(string logline, Level _level); public: @@ -68,12 +82,33 @@ class log { * optional: a bool variable if it keeps the file open, * and a bool variable if it prints log lines to the console */ - log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false); + log (string _dir, Level loglevel = WARNING, bool _isKeepOpen = true, bool _printInConsole = false); + /** - * Put string log in file + * Debug log line + */ + void debug(string logline); + + /** + * Info log line + */ + void info(string logline); + + /** + * Warning log line + */ + void warning(string logline); + + /** + * Error log line + */ + void error(string logline); + + /** + * Fatal log line */ - void put(string logline); + void fatal(string logline); /** * Destruktor, close log files diff --git a/src/log.cpp b/src/log.cpp index 624b4c1..0bf6bfa 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,7 +1,8 @@ #include "../lib/log.hpp" -marcelb::log::log(string _dir, bool _isKeepOpen, bool _printInConsole) { +marcelb::log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) { dir = _dir; + loglevel = _loglevel; isKeepOpen = _isKeepOpen; printInConsole = _printInConsole; @@ -42,14 +43,17 @@ void marcelb::log::setMoment() { moment = localtime (&rawtime); } -void marcelb::log::put(string logline) { +void marcelb::log::put(string logline, Level _level) { + if (_level < loglevel) { + return; + } io.lock(); if (printInConsole) { cout << logline << endl; } setMoment(); - setPrefix(logline); + setPrefix(logline, _level); if (day != moment->tm_mday) { if (isKeepOpen && logfile.is_open()) { @@ -90,16 +94,58 @@ void marcelb::log::setPath() { path = dir + to_string(moment->tm_year+1900) + '-' + mon.str() + '-' + _day.str() + ".log"; } -void marcelb::log::setPrefix(string &logline) { +void marcelb::log::setPrefix(string &logline, Level &_level) { 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; - logline = _logline; + 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 marcelb::log::debug(string logline) { + put(logline, DEBUG); +} + +void marcelb::log::info(string logline) { + put(logline, INFO); } +void marcelb::log::warning(string logline) { + put(logline, WARNING); +} + +void marcelb::log::error(string logline) { + put(logline, ERROR); +} + +void marcelb::log::fatal(string logline) { + put(logline, FATAL); +} + + marcelb::log::~log() { loose(); } diff --git a/test/test.cpp b/test/test.cpp index f609f90..7e27e24 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -5,13 +5,14 @@ using namespace std; using namespace marcelb; -log mylog("../example", false); +log mylog("../example", Level::FATAL, false); int main() { - -// log mylog("../example", false); - - mylog.put("[EVENT] Start loging"); + mylog.debug("Start debug loging"); + mylog.info("Start info loging"); + mylog.warning("Start warning loging"); + mylog.error("Start error loging"); + mylog.fatal("Start fatal loging"); return 0; } diff --git a/test/test.o b/test/test.o index 5f88533..f09cdcb 100755 Binary files a/test/test.o and b/test/test.o differ