Compare commits

..

No commits in common. "dev" and "v0.6" have entirely different histories.
dev ... v0.6

7 changed files with 22 additions and 128 deletions

1
.gitignore vendored
View File

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

View File

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

View File

@ -1,30 +0,0 @@
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

@ -9,7 +9,6 @@
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <mutex> #include <mutex>
#include <queue>
#if _WIN32 #if _WIN32
typedef unsigned int uint; typedef unsigned int uint;
@ -40,9 +39,6 @@ class log {
uint day; uint day;
string path; string path;
mutex io; mutex io;
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
@ -79,18 +75,6 @@ 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:
/** /**
@ -99,7 +83,7 @@ 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, uint32_t groupedWriting = 0); log (string _dir, Level loglevel = WARNING, bool _isKeepOpen = true, bool _printInConsole = false);
/** /**

View File

@ -3,12 +3,11 @@
namespace marcelb { namespace marcelb {
namespace logging { namespace logging {
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, uint32_t _groupedWriting) { log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole) {
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 ");
@ -16,9 +15,6 @@ log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, u
setMoment(); setMoment();
day = moment->tm_mday; day = moment->tm_mday;
if (groupedWriting) {
lastWriting = timelocal(moment);
}
setPath(); setPath();
if (isKeepOpen) { if (isKeepOpen) {
@ -61,40 +57,8 @@ void log::put(string logline, Level _level) {
setMoment(); setMoment();
setPrefix(logline, _level); setPrefix(logline, _level);
midnight();
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! ");
}
}
logfile << logline << endl;
if (!isKeepOpen && logfile.is_open()) {
loose();
}
}
void log::midnight() {
if (day != moment->tm_mday) { if (day != moment->tm_mday) {
if (groupedWriting && !toWrite.empty()) {
writeQueue();
}
if (isKeepOpen && logfile.is_open()) { if (isKeepOpen && logfile.is_open()) {
loose(); loose();
} }
@ -106,30 +70,20 @@ void log::midnight() {
} }
} }
} }
if (!isKeepOpen || !logfile.is_open()) {
if (!open()) {
throw string("[ERROR] Opening log file! ");
}
} }
void log::writeQueue() { logfile << logline << endl;
string lines;
bool notEmpty = !toWrite.empty();
while (notEmpty) { if (!isKeepOpen && logfile.is_open()) {
lines += toWrite.front(); loose();
toWrite.pop();
notEmpty = !toWrite.empty();
if (notEmpty) lines += "\n";
} }
io.unlock();
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() {

View File

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

View File

@ -1,25 +1,18 @@
#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::DEBUG, true, false, 5); log mylog("../example", Level::INFO, false);
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;
} }