Support external writer

This commit is contained in:
mbandic 2025-04-04 11:35:32 +02:00
parent cef9c64e86
commit 39a2f1a740
5 changed files with 43 additions and 8 deletions

View File

@ -5,6 +5,8 @@
"fstream": "cpp",
"*.tcc": "cpp",
"ostream": "cpp",
"mutex": "cpp"
"mutex": "cpp",
"functional": "cpp",
"iomanip": "cpp"
}
}

View File

@ -34,6 +34,12 @@ using namespace marcelb;
*/
log mylog("../log", Level::INFO);
// or use external writer callback (for occasional writing)
log mylog("../example", [](string log, string path) {
cout << log << " " << path << endl;
}, Level::INFO)
/**
* Put log in file
*/

View File

@ -9,6 +9,7 @@
#include <time.h>
#include <sys/stat.h>
#include <mutex>
#include <functional>
#if _WIN32
typedef unsigned int uint;
@ -39,6 +40,7 @@ class log {
uint day;
string path;
mutex io;
function<void(string, string)> extWriter;
/**
* Checking if the path is in the string dir directory
@ -83,9 +85,11 @@ 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, Level loglevel = WARNING, bool _isKeepOpen = true, bool _printInConsole = false);
log (string _dir, Level _loglevel = WARNING, bool _isKeepOpen = true, bool _printInConsole = false);
log (string _dir, function<void(string, string)> _extWriter, Level _loglevel = WARNING, bool _printInConsole = false);
/**
* Debug log line
*/

View File

@ -25,19 +25,35 @@ log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) {
}
log::log (string _dir, function<void(string, string)> _extWriter, Level _loglevel, bool _printInConsole) {
dir = _dir;
loglevel = _loglevel;
extWriter = _extWriter;
printInConsole = _printInConsole;
setMoment();
day = moment->tm_mday;
setPath();
}
bool log::isdir() {
struct stat sb;
return stat(dir.c_str(), &sb) == 0;
}
bool log::open() {
if (extWriter) {
return false;
}
logfile = ofstream (path, ios_base::app);
return logfile.is_open();
}
void log::loose() {
if (!extWriter) {
logfile.close();
}
}
void log::setMoment() {
@ -59,27 +75,31 @@ void log::put(string logline, Level _level) {
setPrefix(logline, _level);
if (day != moment->tm_mday) {
if (isKeepOpen && logfile.is_open()) {
if (!extWriter && isKeepOpen && logfile.is_open()) {
loose();
}
day = moment->tm_mday;
setPath();
if (isKeepOpen) {
if (!extWriter && isKeepOpen) {
if (!open()) {
throw string("[ERROR] Opening log file! ");
}
}
}
if (!isKeepOpen || !logfile.is_open()) {
if (!extWriter && (!isKeepOpen || !logfile.is_open())) {
if (!open()) {
throw string("[ERROR] Opening log file! ");
}
}
if (!extWriter) {
logfile << logline << endl;
} else {
extWriter(logline, path);
}
if (!isKeepOpen && logfile.is_open()) {
if (!extWriter && !isKeepOpen && logfile.is_open()) {
loose();
}
io.unlock();

View File

@ -6,6 +6,9 @@ using namespace std;
using namespace marcelb::logging;
log mylog("../example", Level::INFO, false);
// log mylog("../example", [](string log, string path) {
// cout << log << " " << path << endl;
// }, Level::INFO);
int main() {
mylog.debug("Start debug loging");