Compare commits

..

No commits in common. 'dev' and 'testing' have entirely different histories.
dev ... testing

  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 .vscode
example example/*
test/*.o test/*.o

@ -10,7 +10,6 @@ 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
@ -32,16 +31,12 @@ using namespace marcelb;
/** /**
* Initialization and declaration * Initialization and declaration
*/ */
log mylog("../log", Level::INFO); log mylog("../log");
/** /**
* Put log in file * Put log in file
*/ */
mylog.debug("Debug loging"); mylog.put("[EVENT] Start loging");
mylog.info("Info loging");
mylog.warning("Warning loging");
mylog.error("Error loging");
mylog.fatal("Fatal loging");
``` ```
## License ## License

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

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

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

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

@ -4,34 +4,20 @@
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
#include <string> #include <string>
#include <sstream>
#include <fstream> #include <fstream>
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <mutex> #include <mutex>
#if _WIN32
typedef unsigned int uint;
#endif
namespace marcelb { namespace marcelb {
namespace logging {
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;
@ -68,12 +54,7 @@ class log {
/** /**
* Set log line time prefix * Set log line time prefix
*/ */
void setPrefix(string &logline, Level &_level); void setPrefix(string &logline);
/**
* Put string log in file
*/
void put(string logline, Level _level);
public: public:
@ -83,33 +64,12 @@ 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, Level loglevel = WARNING, bool _isKeepOpen = true, bool _printInConsole = false); log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false);
/** /**
* Debug log line * Put string log in file
*/
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); void put(string logline);
/** /**
* Destruktor, close log files * Destruktor, close log files
@ -117,7 +77,6 @@ class log {
~log(); ~log();
}; };
}
} }
#endif #endif

@ -1,11 +1,7 @@
#include "../lib/log.hpp" #include "../lib/log.hpp"
namespace marcelb { marcelb::log::log(string _dir, bool _isKeepOpen, bool _printInConsole) {
namespace logging {
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) {
dir = _dir; dir = _dir;
loglevel = _loglevel;
isKeepOpen = _isKeepOpen; isKeepOpen = _isKeepOpen;
printInConsole = _printInConsole; 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; struct stat sb;
return stat(dir.c_str(), &sb) == 0; return stat(dir.c_str(), &sb) == 0;
} }
bool log::open() { bool marcelb::log::open() {
logfile = ofstream (path, ios_base::app); logfile = ofstream (path, ios_base::app);
return logfile.is_open(); return logfile.is_open();
} }
void log::loose() { void marcelb::log::loose() {
logfile.close(); logfile.close();
} }
void log::setMoment() { void marcelb::log::setMoment() {
time_t rawtime; time_t rawtime;
time (&rawtime); time (&rawtime);
moment = localtime (&rawtime); moment = localtime (&rawtime);
} }
void log::put(string logline, Level _level) { void marcelb::log::put(string logline) {
if (_level < loglevel) {
return;
}
io.lock(); io.lock();
if (printInConsole) { if (printInConsole) {
cout << logline << endl; cout << logline << endl;
} }
setMoment(); setMoment();
setPrefix(logline, _level); setPrefix(logline);
if (day != moment->tm_mday) { if (day != moment->tm_mday) {
if (isKeepOpen && logfile.is_open()) { 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] != '/') { if (dir[dir.length()-1] != '/') {
dir.push_back('/'); dir.push_back('/');
} }
@ -97,62 +90,17 @@ void 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 log::setPrefix(string &logline, Level &_level) { void marcelb::log::setPrefix(string &logline) {
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;
switch (_level) { logline = _logline;
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(); loose();
} }
}
}

@ -3,16 +3,15 @@
#include "../lib/log.hpp" #include "../lib/log.hpp"
using namespace std; using namespace std;
using namespace marcelb::logging; using namespace marcelb;
log mylog("../example", Level::INFO, false); log mylog("../example", false);
int main() { int main() {
mylog.debug("Start debug loging");
mylog.info("Start info loging"); // log mylog("../example", false);
mylog.warning("Start warning loging");
mylog.error("Start error loging"); mylog.put("[EVENT] Start loging");
mylog.fatal("Start fatal loging");
return 0; return 0;
} }

Binary file not shown.
Loading…
Cancel
Save