Compare commits
No commits in common. "external-writer" and "dev" have entirely different histories.
external-w
...
dev
4
.vscode/settings.json
vendored
4
.vscode/settings.json
vendored
@ -5,8 +5,6 @@
|
||||
"fstream": "cpp",
|
||||
"*.tcc": "cpp",
|
||||
"ostream": "cpp",
|
||||
"mutex": "cpp",
|
||||
"functional": "cpp",
|
||||
"iomanip": "cpp"
|
||||
"mutex": "cpp"
|
||||
}
|
||||
}
|
@ -34,12 +34,6 @@ 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
|
||||
*/
|
||||
|
@ -9,7 +9,6 @@
|
||||
#include <time.h>
|
||||
#include <sys/stat.h>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
#if _WIN32
|
||||
typedef unsigned int uint;
|
||||
@ -40,7 +39,6 @@ class log {
|
||||
uint day;
|
||||
string path;
|
||||
mutex io;
|
||||
function<void(string, string)> extWriter;
|
||||
|
||||
/**
|
||||
* Checking if the path is in the string dir directory
|
||||
@ -85,10 +83,8 @@ 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
|
||||
|
32
src/log.cpp
32
src/log.cpp
@ -25,35 +25,19 @@ 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();
|
||||
}
|
||||
logfile.close();
|
||||
}
|
||||
|
||||
void log::setMoment() {
|
||||
@ -75,31 +59,27 @@ void log::put(string logline, Level _level) {
|
||||
setPrefix(logline, _level);
|
||||
|
||||
if (day != moment->tm_mday) {
|
||||
if (!extWriter && isKeepOpen && logfile.is_open()) {
|
||||
if (isKeepOpen && logfile.is_open()) {
|
||||
loose();
|
||||
}
|
||||
day = moment->tm_mday;
|
||||
setPath();
|
||||
if (!extWriter && isKeepOpen) {
|
||||
if (isKeepOpen) {
|
||||
if (!open()) {
|
||||
throw string("[ERROR] Opening log file! ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!extWriter && (!isKeepOpen || !logfile.is_open())) {
|
||||
if (!isKeepOpen || !logfile.is_open()) {
|
||||
if (!open()) {
|
||||
throw string("[ERROR] Opening log file! ");
|
||||
}
|
||||
}
|
||||
|
||||
if (!extWriter) {
|
||||
logfile << logline << endl;
|
||||
} else {
|
||||
extWriter(logline, path);
|
||||
}
|
||||
logfile << logline << endl;
|
||||
|
||||
if (!extWriter && !isKeepOpen && logfile.is_open()) {
|
||||
if (!isKeepOpen && logfile.is_open()) {
|
||||
loose();
|
||||
}
|
||||
io.unlock();
|
||||
|
@ -6,9 +6,6 @@ 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");
|
||||
|
Loading…
x
Reference in New Issue
Block a user