Compare commits

...

5 Commits
v0.4 ... dev

Author SHA1 Message Date
marcelb f6f7853062 Merge, test, fix, and release 3 months ago
marcelb b06129f345 Edit namespaces 6 months ago
mbandic ffc53a7ad5 Add log level 6 months ago
marcelb db7f8ebd92 Windows sstream missing 8 months ago
marcelb 3a2f9fa12e Windows uint datatype def 8 months ago
  1. 2
      .gitignore
  2. 9
      README.md
  3. 1
      example/2023-05-21.log
  4. 3
      example/2023-07-27.log
  5. 5
      example/2023-5-20.log
  6. 2
      example/2023-5-21.log
  7. 49
      lib/log.hpp
  8. 76
      src/log.cpp
  9. 13
      test/test.cpp
  10. BIN
      test/test.o

2
.gitignore vendored

@ -1,3 +1,3 @@
.vscode
example/*
example
test/*.o

@ -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

@ -1 +0,0 @@
23:08:58 [EVENT] Start loging

@ -1,3 +0,0 @@
18:29:30 [EVENT] Start loging
18:29:44 [EVENT] Start loging
18:29:47 [EVENT] Start loging

@ -1,5 +0,0 @@
[EVENT] Start loging
[EVENT] Start loging
10:58:12 [EVENT] Start loging
10:59:19 [EVENT] Start loging
11:00:17 [EVENT] Start loging

@ -1,2 +0,0 @@
11:00:58 [EVENT] Start loging
13:02:17 [EVENT] Start loging

@ -4,20 +4,34 @@
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <time.h>
#include <sys/stat.h>
#include <mutex>
#if _WIN32
typedef unsigned int uint;
#endif
namespace marcelb {
namespace logging {
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;
@ -54,7 +68,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:
@ -64,12 +83,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 put(string logline);
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
@ -77,6 +117,7 @@ class log {
~log();
};
}
}
#endif

@ -1,7 +1,11 @@
#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;
loglevel = _loglevel;
isKeepOpen = _isKeepOpen;
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;
return stat(dir.c_str(), &sb) == 0;
}
bool marcelb::log::open() {
bool log::open() {
logfile = ofstream (path, ios_base::app);
return logfile.is_open();
}
void marcelb::log::loose() {
void log::loose() {
logfile.close();
}
void marcelb::log::setMoment() {
void log::setMoment() {
time_t rawtime;
time (&rawtime);
moment = localtime (&rawtime);
}
void marcelb::log::put(string logline) {
void 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()) {
@ -79,7 +86,7 @@ void marcelb::log::put(string logline) {
}
void marcelb::log::setPath() {
void log::setPath() {
if (dir[dir.length()-1] != '/') {
dir.push_back('/');
}
@ -90,17 +97,62 @@ void marcelb::log::setPath() {
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;
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 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();
}
}
}

@ -3,15 +3,16 @@
#include "../lib/log.hpp"
using namespace std;
using namespace marcelb;
using namespace marcelb::logging;
log mylog("../example", false);
log mylog("../example", Level::INFO, 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;
}

Binary file not shown.
Loading…
Cancel
Save