Compare commits
1 Commits
external-w
...
dev
Author | SHA1 | Date | |
---|---|---|---|
|
108e42ce1f |
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -6,7 +6,8 @@
|
|||||||
"*.tcc": "cpp",
|
"*.tcc": "cpp",
|
||||||
"ostream": "cpp",
|
"ostream": "cpp",
|
||||||
"mutex": "cpp",
|
"mutex": "cpp",
|
||||||
"functional": "cpp",
|
"queue": "cpp",
|
||||||
"iomanip": "cpp"
|
"array": "cpp",
|
||||||
|
"string_view": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -34,12 +34,6 @@ using namespace marcelb;
|
|||||||
*/
|
*/
|
||||||
log mylog("../log", Level::INFO);
|
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
|
* Put log in file
|
||||||
*/
|
*/
|
||||||
|
22
lib/log.hpp
22
lib/log.hpp
@ -9,7 +9,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <functional>
|
#include <queue>
|
||||||
|
|
||||||
#if _WIN32
|
#if _WIN32
|
||||||
typedef unsigned int uint;
|
typedef unsigned int uint;
|
||||||
@ -40,7 +40,9 @@ class log {
|
|||||||
uint day;
|
uint day;
|
||||||
string path;
|
string path;
|
||||||
mutex io;
|
mutex io;
|
||||||
function<void(string, string)> extWriter;
|
uint32_t groupedWriting;
|
||||||
|
time_t lastWriting;
|
||||||
|
queue<string> toWrite;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checking if the path is in the string dir directory
|
* Checking if the path is in the string dir directory
|
||||||
@ -77,6 +79,18 @@ class log {
|
|||||||
*/
|
*/
|
||||||
void put(string logline, Level _level);
|
void put(string logline, Level _level);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write to log file
|
||||||
|
*/
|
||||||
|
|
||||||
|
void write(string logline);
|
||||||
|
|
||||||
|
void midnight();
|
||||||
|
|
||||||
|
void writeQueue();
|
||||||
|
|
||||||
|
bool writableQueue();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,10 +99,8 @@ 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, Level loglevel = WARNING, bool _isKeepOpen = true, bool _printInConsole = false, uint32_t groupedWriting = 0);
|
||||||
|
|
||||||
|
|
||||||
log (string _dir, function<void(string, string)> _extWriter, Level _loglevel = WARNING, bool _printInConsole = false);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Debug log line
|
* Debug log line
|
||||||
|
98
src/log.cpp
98
src/log.cpp
@ -3,11 +3,12 @@
|
|||||||
namespace marcelb {
|
namespace marcelb {
|
||||||
namespace logging {
|
namespace logging {
|
||||||
|
|
||||||
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) {
|
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, uint32_t _groupedWriting) {
|
||||||
dir = _dir;
|
dir = _dir;
|
||||||
loglevel = _loglevel;
|
loglevel = _loglevel;
|
||||||
isKeepOpen = _isKeepOpen;
|
isKeepOpen = _isKeepOpen;
|
||||||
printInConsole = _printInConsole;
|
printInConsole = _printInConsole;
|
||||||
|
groupedWriting = _groupedWriting;
|
||||||
|
|
||||||
if (!isdir()) {
|
if (!isdir()) {
|
||||||
throw string("[ERROR] Log dir path invalid ");
|
throw string("[ERROR] Log dir path invalid ");
|
||||||
@ -15,6 +16,9 @@ log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) {
|
|||||||
|
|
||||||
setMoment();
|
setMoment();
|
||||||
day = moment->tm_mday;
|
day = moment->tm_mday;
|
||||||
|
if (groupedWriting) {
|
||||||
|
lastWriting = timelocal(moment);
|
||||||
|
}
|
||||||
setPath();
|
setPath();
|
||||||
|
|
||||||
if (isKeepOpen) {
|
if (isKeepOpen) {
|
||||||
@ -25,35 +29,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() {
|
bool 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 log::open() {
|
||||||
if (extWriter) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
logfile = ofstream (path, ios_base::app);
|
logfile = ofstream (path, ios_base::app);
|
||||||
return logfile.is_open();
|
return logfile.is_open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void log::loose() {
|
void log::loose() {
|
||||||
if (!extWriter) {
|
logfile.close();
|
||||||
logfile.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void log::setMoment() {
|
void log::setMoment() {
|
||||||
@ -73,37 +61,75 @@ void log::put(string logline, Level _level) {
|
|||||||
|
|
||||||
setMoment();
|
setMoment();
|
||||||
setPrefix(logline, _level);
|
setPrefix(logline, _level);
|
||||||
|
midnight();
|
||||||
|
|
||||||
if (day != moment->tm_mday) {
|
if (groupedWriting) {
|
||||||
if (!extWriter && isKeepOpen && logfile.is_open()) {
|
toWrite.push(logline);
|
||||||
loose();
|
if (writableQueue()) {
|
||||||
}
|
writeQueue();
|
||||||
day = moment->tm_mday;
|
|
||||||
setPath();
|
|
||||||
if (!extWriter && isKeepOpen) {
|
|
||||||
if (!open()) {
|
|
||||||
throw string("[ERROR] Opening log file! ");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
write(logline);
|
||||||
}
|
}
|
||||||
|
io.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
if (!extWriter && (!isKeepOpen || !logfile.is_open())) {
|
|
||||||
|
void log::write(string logline) {
|
||||||
|
if (!isKeepOpen || !logfile.is_open()) {
|
||||||
if (!open()) {
|
if (!open()) {
|
||||||
throw string("[ERROR] Opening log file! ");
|
throw string("[ERROR] Opening log file! ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!extWriter) {
|
logfile << logline << endl;
|
||||||
logfile << logline << endl;
|
|
||||||
} else {
|
|
||||||
extWriter(logline, path);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!extWriter && !isKeepOpen && logfile.is_open()) {
|
if (!isKeepOpen && logfile.is_open()) {
|
||||||
loose();
|
loose();
|
||||||
}
|
}
|
||||||
io.unlock();
|
}
|
||||||
|
|
||||||
|
void log::midnight() {
|
||||||
|
if (day != moment->tm_mday) {
|
||||||
|
if (groupedWriting && !toWrite.empty()) {
|
||||||
|
writeQueue();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isKeepOpen && logfile.is_open()) {
|
||||||
|
loose();
|
||||||
|
}
|
||||||
|
day = moment->tm_mday;
|
||||||
|
setPath();
|
||||||
|
if (isKeepOpen) {
|
||||||
|
if (!open()) {
|
||||||
|
throw string("[ERROR] Opening log file! ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void log::writeQueue() {
|
||||||
|
string lines;
|
||||||
|
bool notEmpty = !toWrite.empty();
|
||||||
|
|
||||||
|
while (notEmpty) {
|
||||||
|
lines += toWrite.front();
|
||||||
|
toWrite.pop();
|
||||||
|
notEmpty = !toWrite.empty();
|
||||||
|
if (notEmpty) lines += "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
write(lines);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool log::writableQueue() {
|
||||||
|
bool _writable = false;
|
||||||
|
auto _time = timelocal(moment);
|
||||||
|
if (_time > lastWriting + groupedWriting) {
|
||||||
|
_writable = true;
|
||||||
|
lastWriting = _time;
|
||||||
|
}
|
||||||
|
return _writable;
|
||||||
}
|
}
|
||||||
|
|
||||||
void log::setPath() {
|
void log::setPath() {
|
||||||
|
@ -1,21 +1,25 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "../lib/log.hpp"
|
#include "../lib/log.hpp"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace marcelb::logging;
|
using namespace marcelb::logging;
|
||||||
|
|
||||||
log mylog("../example", Level::INFO, false);
|
log mylog("../example", Level::DEBUG, true, false, 5);
|
||||||
// log mylog("../example", [](string log, string path) {
|
|
||||||
// cout << log << " " << path << endl;
|
|
||||||
// }, Level::INFO);
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
mylog.debug("Start debug loging");
|
// mylog.debug("Start debug loging");
|
||||||
mylog.info("Start info loging");
|
// mylog.info("Start info loging");
|
||||||
mylog.warning("Start warning loging");
|
// mylog.warning("Start warning loging");
|
||||||
mylog.error("Start error loging");
|
// mylog.error("Start error loging");
|
||||||
mylog.fatal("Start fatal loging");
|
// mylog.fatal("Start fatal loging");
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while(true) {
|
||||||
|
mylog.fatal("Test fatal error: "+ to_string(i++));
|
||||||
|
sleep(2);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user