Compare commits

..

4 Commits

Author SHA1 Message Date
mbandic
108e42ce1f Support grouped writing delayed by time 2025-04-04 12:37:39 +02:00
marcelb
cef9c64e86 Switch to CMake 2025-01-31 19:14:55 +01:00
marcelb
f6f7853062 Merge, test, fix, and release 2024-06-18 20:04:37 +02:00
marcelb
b06129f345 Edit namespaces 2024-03-25 20:58:52 +01:00
12 changed files with 151 additions and 48 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
.vscode
example/*
example
test/*.o
build

View File

@ -5,6 +5,9 @@
"fstream": "cpp",
"*.tcc": "cpp",
"ostream": "cpp",
"mutex": "cpp"
"mutex": "cpp",
"queue": "cpp",
"array": "cpp",
"string_view": "cpp"
}
}

30
CMakeLists.txt Normal file
View File

@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.10)
project(log)
# Postavi verziju projekta
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Pronađi Boost biblioteku (ako nije uobičajeni direktorijum, postavi put)
# find_package(Boost REQUIRED COMPONENTS system)
# Dodaj direktorijume sa zaglavljima
include_directories(lib)
# Dodaj biblioteku
add_library(log STATIC
src/log.cpp
)
# # Linkaj log biblioteku sa Boost-om
# target_link_libraries(log Boost::system)
# Dodaj testove
add_subdirectory(test)
# Instaliraj biblioteku
# install(TARGETS log DESTINATION lib)
# install(FILES lib/log.hpp lib/define.hpp lib/engine.hpp lib/filesystem.hpp lib/timers.hpp lib/trigger.hpp DESTINATION include/log)
#

View File

@ -1 +0,0 @@
23:08:58 [EVENT] Start loging

View File

@ -1,3 +0,0 @@
18:29:30 [EVENT] Start loging
18:29:44 [EVENT] Start loging
18:29:47 [EVENT] Start loging

View File

@ -1,5 +0,0 @@
[EVENT] Start loging
[EVENT] Start loging
10:58:12 [EVENT] Start loging
10:59:19 [EVENT] Start loging
11:00:17 [EVENT] Start loging

View File

@ -1,2 +0,0 @@
11:00:58 [EVENT] Start loging
13:02:17 [EVENT] Start loging

View File

@ -9,11 +9,13 @@
#include <time.h>
#include <sys/stat.h>
#include <mutex>
#include <queue>
#if _WIN32
typedef unsigned int uint;
#endif
namespace marcelb {
namespace logging {
using namespace std;
@ -38,6 +40,9 @@ class log {
uint day;
string path;
mutex io;
uint32_t groupedWriting;
time_t lastWriting;
queue<string> toWrite;
/**
* Checking if the path is in the string dir directory
@ -74,6 +79,18 @@ class log {
*/
void put(string logline, Level _level);
/**
* Write to log file
*/
void write(string logline);
void midnight();
void writeQueue();
bool writableQueue();
public:
/**
@ -82,7 +99,7 @@ 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, uint32_t groupedWriting = 0);
/**
@ -116,6 +133,7 @@ class log {
~log();
};
}
}
#endif

View File

@ -1,10 +1,14 @@
#include "../lib/log.hpp"
marcelb::log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) {
namespace marcelb {
namespace logging {
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, uint32_t _groupedWriting) {
dir = _dir;
loglevel = _loglevel;
isKeepOpen = _isKeepOpen;
printInConsole = _printInConsole;
groupedWriting = _groupedWriting;
if (!isdir()) {
throw string("[ERROR] Log dir path invalid ");
@ -12,6 +16,9 @@ marcelb::log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInC
setMoment();
day = moment->tm_mday;
if (groupedWriting) {
lastWriting = timelocal(moment);
}
setPath();
if (isKeepOpen) {
@ -22,28 +29,28 @@ marcelb::log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInC
}
bool marcelb::log::isdir() {
bool log::isdir() {
struct stat sb;
return stat(dir.c_str(), &sb) == 0;
}
bool marcelb::log::open() {
bool log::open() {
logfile = ofstream (path, ios_base::app);
return logfile.is_open();
}
void marcelb::log::loose() {
void log::loose() {
logfile.close();
}
void marcelb::log::setMoment() {
void log::setMoment() {
time_t rawtime;
time (&rawtime);
moment = localtime (&rawtime);
}
void marcelb::log::put(string logline, Level _level) {
void log::put(string logline, Level _level) {
if (_level < loglevel) {
return;
}
@ -54,20 +61,21 @@ void marcelb::log::put(string logline, Level _level) {
setMoment();
setPrefix(logline, _level);
midnight();
if (day != moment->tm_mday) {
if (isKeepOpen && logfile.is_open()) {
loose();
}
day = moment->tm_mday;
setPath();
if (isKeepOpen) {
if (!open()) {
throw string("[ERROR] Opening log file! ");
}
if (groupedWriting) {
toWrite.push(logline);
if (writableQueue()) {
writeQueue();
}
} else {
write(logline);
}
io.unlock();
}
void log::write(string logline) {
if (!isKeepOpen || !logfile.is_open()) {
if (!open()) {
throw string("[ERROR] Opening log file! ");
@ -79,11 +87,52 @@ void marcelb::log::put(string logline, Level _level) {
if (!isKeepOpen && logfile.is_open()) {
loose();
}
io.unlock();
}
void marcelb::log::setPath() {
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() {
if (dir[dir.length()-1] != '/') {
dir.push_back('/');
}
@ -94,7 +143,7 @@ void marcelb::log::setPath() {
path = dir + to_string(moment->tm_year+1900) + '-' + mon.str() + '-' + _day.str() + ".log";
}
void marcelb::log::setPrefix(string &logline, Level &_level) {
void log::setPrefix(string &logline, Level &_level) {
stringstream hour, min, sec;
hour << setw(2) << setfill('0') << moment->tm_hour;
min << setw(2) << setfill('0') << moment->tm_min;
@ -125,28 +174,31 @@ void marcelb::log::setPrefix(string &logline, Level &_level) {
logline = _logline + logline;
}
void marcelb::log::debug(string logline) {
void log::debug(string logline) {
put(logline, DEBUG);
}
void marcelb::log::info(string logline) {
void log::info(string logline) {
put(logline, INFO);
}
void marcelb::log::warning(string logline) {
void log::warning(string logline) {
put(logline, WARNING);
}
void marcelb::log::error(string logline) {
void log::error(string logline) {
put(logline, ERROR);
}
void marcelb::log::fatal(string logline) {
void log::fatal(string logline) {
put(logline, FATAL);
}
marcelb::log::~log() {
log::~log() {
loose();
}
}
}

3
test/CMakeLists.txt Normal file
View File

@ -0,0 +1,3 @@
add_executable(log_test test.cpp)
target_link_libraries(log_test log)

View File

@ -1,18 +1,25 @@
#include <iostream>
#include <unistd.h>
#include "../lib/log.hpp"
using namespace std;
using namespace marcelb;
using namespace marcelb::logging;
log mylog("../example", Level::FATAL, false);
log mylog("../example", Level::DEBUG, true, false, 5);
int main() {
mylog.debug("Start debug loging");
mylog.info("Start info loging");
mylog.warning("Start warning loging");
mylog.error("Start error loging");
mylog.fatal("Start fatal loging");
// mylog.debug("Start debug loging");
// mylog.info("Start info loging");
// mylog.warning("Start warning loging");
// mylog.error("Start error loging");
// mylog.fatal("Start fatal loging");
int i = 0;
while(true) {
mylog.fatal("Test fatal error: "+ to_string(i++));
sleep(2);
}
return 0;
}

Binary file not shown.