Add log level
This commit is contained in:
parent
db7f8ebd92
commit
ffc53a7ad5
@ -10,6 +10,7 @@ Logging errors to a file, daily file rotation, date and time stamps. Configurabl
|
|||||||
- Customizable directory for files
|
- Customizable directory for files
|
||||||
- Daily file rotation
|
- Daily file rotation
|
||||||
- Protection of recording consecutive errors
|
- Protection of recording consecutive errors
|
||||||
|
- Log levels
|
||||||
- Thread safe
|
- Thread safe
|
||||||
- Exceptions
|
- Exceptions
|
||||||
- The possibility of printing logs in the console
|
- The possibility of printing logs in the console
|
||||||
@ -31,12 +32,16 @@ using namespace marcelb;
|
|||||||
/**
|
/**
|
||||||
* Initialization and declaration
|
* Initialization and declaration
|
||||||
*/
|
*/
|
||||||
log mylog("../log");
|
log mylog("../log", Level::INFO);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Put log in file
|
* 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
|
## License
|
||||||
|
|
||||||
|
43
lib/log.hpp
43
lib/log.hpp
@ -17,11 +17,20 @@ namespace marcelb {
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
DEBUG = 0,
|
||||||
|
INFO,
|
||||||
|
WARNING,
|
||||||
|
ERROR,
|
||||||
|
FATAL,
|
||||||
|
} Level;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Log class - used at the level of one log directory
|
* Log class - used at the level of one log directory
|
||||||
*/
|
*/
|
||||||
class log {
|
class log {
|
||||||
string dir;
|
string dir;
|
||||||
|
Level loglevel;
|
||||||
bool isKeepOpen;
|
bool isKeepOpen;
|
||||||
bool printInConsole;
|
bool printInConsole;
|
||||||
ofstream logfile;
|
ofstream logfile;
|
||||||
@ -58,7 +67,12 @@ class log {
|
|||||||
/**
|
/**
|
||||||
* Set log line time prefix
|
* 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:
|
public:
|
||||||
|
|
||||||
@ -68,12 +82,33 @@ class log {
|
|||||||
* optional: a bool variable if it keeps the file open,
|
* optional: a bool variable if it keeps the file open,
|
||||||
* and a bool variable if it prints log lines to the console
|
* 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 put(string logline);
|
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 fatal(string logline);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destruktor, close log files
|
* Destruktor, close log files
|
||||||
|
58
src/log.cpp
58
src/log.cpp
@ -1,7 +1,8 @@
|
|||||||
#include "../lib/log.hpp"
|
#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;
|
dir = _dir;
|
||||||
|
loglevel = _loglevel;
|
||||||
isKeepOpen = _isKeepOpen;
|
isKeepOpen = _isKeepOpen;
|
||||||
printInConsole = _printInConsole;
|
printInConsole = _printInConsole;
|
||||||
|
|
||||||
@ -42,14 +43,17 @@ void marcelb::log::setMoment() {
|
|||||||
moment = localtime (&rawtime);
|
moment = localtime (&rawtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
void marcelb::log::put(string logline) {
|
void marcelb::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()) {
|
||||||
@ -90,16 +94,58 @@ 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 marcelb::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 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() {
|
marcelb::log::~log() {
|
||||||
loose();
|
loose();
|
||||||
}
|
}
|
||||||
|
@ -5,13 +5,14 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace marcelb;
|
using namespace marcelb;
|
||||||
|
|
||||||
log mylog("../example", false);
|
log mylog("../example", Level::FATAL, false);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
mylog.debug("Start debug loging");
|
||||||
// log mylog("../example", false);
|
mylog.info("Start info loging");
|
||||||
|
mylog.warning("Start warning loging");
|
||||||
mylog.put("[EVENT] Start loging");
|
mylog.error("Start error loging");
|
||||||
|
mylog.fatal("Start fatal loging");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
BIN
test/test.o
BIN
test/test.o
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user