commit d12ef77ba5901051cd8dcd9e7e5094bc5ef0672e Author: marcelb Date: Sun May 21 11:03:18 2023 +0200 Tested diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..46602ed --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.associations": { + "iostream": "cpp", + "iosfwd": "cpp", + "fstream": "cpp", + "*.tcc": "cpp" + } +} \ No newline at end of file diff --git a/example/2023-5-20.log b/example/2023-5-20.log new file mode 100644 index 0000000..67c05ea --- /dev/null +++ b/example/2023-5-20.log @@ -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 diff --git a/example/2023-5-21.log b/example/2023-5-21.log new file mode 100644 index 0000000..9999c2f --- /dev/null +++ b/example/2023-5-21.log @@ -0,0 +1 @@ +11:00:58 [EVENT] Start loging diff --git a/lib/log.hpp b/lib/log.hpp new file mode 100644 index 0000000..59fe485 --- /dev/null +++ b/lib/log.hpp @@ -0,0 +1,36 @@ +#ifndef _LOG_ +#define _LOG_ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +class log { + public: + string dir; + bool isKeepOpen; + ofstream logfile; + struct tm * moment; + uint day; + string path; + + log (string _dir, bool _isKeepOpen = true); + + bool isdir(); + bool open(); + void loose(); + void setMoment(); + void setPath(); + void put(string logline); + void setPrefix(string &logline); + + ~log(); +}; + + +#endif \ No newline at end of file diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..498aa5e --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,101 @@ +#include "../lib/log.hpp" + +log::log(string _dir, bool _isKeepOpen) { + dir = _dir; + isKeepOpen = _isKeepOpen; + + if (!isdir()) { + cout << "Eror log dir path invalid!" << endl; + exit(1); + } + + setMoment(); + day = moment->tm_mday; + setPath(); + + if (isKeepOpen) { + if (!open()) { + cout << "Error opening log file!" << endl; + exit(2); + } + } + +} + +bool log::isdir() { + struct stat sb; + return stat(dir.c_str(), &sb) == 0; +} + +bool log::open() { + logfile = ofstream (path, ios_base::app); + return logfile.is_open(); +} + + +void log::loose() { + logfile.close(); +} + +void log::setMoment() { + time_t rawtime; + time (&rawtime); + moment = localtime (&rawtime); +} + +void log::put(string logline) { + setMoment(); + setPrefix(logline); + + if (day != moment->tm_mday) { + if (isKeepOpen && logfile.is_open()) { + loose(); + } + day = moment->tm_mday; + setPath(); + if (isKeepOpen) { + if (!open()) { + cout << "Error opening log file!" << endl; + exit(3); + } + } + } + + if (!isKeepOpen || !logfile.is_open()) { + if (!open()) { + cout << "Error opening log file!" << endl; + exit(4); + } + } + + cout << logline << endl; + logfile << logline << endl; + + + if (!isKeepOpen && logfile.is_open()) { + loose(); + } + +} + +void log::setPath() { + if (dir[dir.length()-1] != '/') { + dir.push_back('/'); + } + path = dir + to_string(moment->tm_year+1900) + '-' + to_string(moment->tm_mon+1) + '-' + to_string(moment->tm_mday) + ".log"; +} + +void log::setPrefix(string &logline) { + 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; +} + +log::~log() { + loose(); +} + diff --git a/test/compile.sh b/test/compile.sh new file mode 100644 index 0000000..d78b604 --- /dev/null +++ b/test/compile.sh @@ -0,0 +1 @@ +g++ test.cpp ../src/* -o test.o -l sqlite3 \ No newline at end of file diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..a955d42 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,14 @@ +#include + +#include "../lib/log.hpp" + +using namespace std; + +int main() { + + log mylog("../example", false); + + mylog.put("[EVENT] Start loging"); + + return 0; +} \ No newline at end of file diff --git a/test/test.o b/test/test.o new file mode 100755 index 0000000..a824953 Binary files /dev/null and b/test/test.o differ