From 8ac79b5a5d4b133459c4aea1bafc695d1bc3e8a4 Mon Sep 17 00:00:00 2001 From: marcelb Date: Mon, 22 May 2023 23:11:20 +0200 Subject: [PATCH] Add print in console option --- .vscode/settings.json | 3 ++- README.md | 5 +++++ lib/log.hpp | 3 ++- src/log.cpp | 9 ++++++--- 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 README.md diff --git a/.vscode/settings.json b/.vscode/settings.json index 46602ed..1439fff 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "iostream": "cpp", "iosfwd": "cpp", "fstream": "cpp", - "*.tcc": "cpp" + "*.tcc": "cpp", + "ostream": "cpp" } } \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..774a097 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ + +# A simple Logging library for C++ + +Logging errors to a file, daily file rotation, date and time stamps. Configurable record structure etc. + diff --git a/lib/log.hpp b/lib/log.hpp index 59fe485..176e936 100644 --- a/lib/log.hpp +++ b/lib/log.hpp @@ -14,12 +14,13 @@ class log { public: string dir; bool isKeepOpen; + bool printInConsole; ofstream logfile; struct tm * moment; uint day; string path; - log (string _dir, bool _isKeepOpen = true); + log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false); bool isdir(); bool open(); diff --git a/src/log.cpp b/src/log.cpp index 717fcb2..796862c 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,8 +1,9 @@ #include "../lib/log.hpp" -log::log(string _dir, bool _isKeepOpen) { +log::log(string _dir, bool _isKeepOpen, bool _printInConsole = false) { dir = _dir; isKeepOpen = _isKeepOpen; + printInConsole = _printInConsole; if (!isdir()) { cout << "Eror log dir path invalid!" << endl; @@ -44,6 +45,10 @@ void log::setMoment() { } void log::put(string logline) { + if (printInConsole) { + cout << logline << endl; + } + setMoment(); setPrefix(logline); @@ -68,10 +73,8 @@ void log::put(string logline) { } } - cout << logline << endl; logfile << logline << endl; - if (!isKeepOpen && logfile.is_open()) { loose(); }