Compare commits

..

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

13 changed files with 49 additions and 243 deletions

3
.gitignore vendored
View File

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

View File

@ -5,9 +5,6 @@
"fstream": "cpp",
"*.tcc": "cpp",
"ostream": "cpp",
"mutex": "cpp",
"queue": "cpp",
"array": "cpp",
"string_view": "cpp"
"mutex": "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

@ -10,7 +10,6 @@ Logging errors to a file, daily file rotation, date and time stamps. Configurabl
- Customizable directory for files
- Daily file rotation
- Protection of recording consecutive errors
- Log levels
- Thread safe
- Exceptions
- The possibility of printing logs in the console
@ -32,16 +31,12 @@ using namespace marcelb;
/**
* Initialization and declaration
*/
log mylog("../log", Level::INFO);
log mylog("../log");
/**
* Put log in file
*/
mylog.debug("Debug loging");
mylog.info("Info loging");
mylog.warning("Warning loging");
mylog.error("Error loging");
mylog.fatal("Fatal loging");
mylog.put("[EVENT] Start loging");
```
## License

1
example/2023-05-21.log Normal file
View File

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

3
example/2023-07-27.log Normal file
View File

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

5
example/2023-5-20.log Normal file
View File

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

2
example/2023-5-21.log Normal file
View File

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

View File

@ -4,35 +4,20 @@
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#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;
typedef enum {
DEBUG = 0,
INFO,
WARNING,
ERROR,
FATAL,
} Level;
/**
* Log class - used at the level of one log directory
*/
class log {
string dir;
Level loglevel;
bool isKeepOpen;
bool printInConsole;
ofstream logfile;
@ -40,9 +25,6 @@ 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
@ -72,24 +54,7 @@ class log {
/**
* Set log line time prefix
*/
void setPrefix(string &logline, Level &_level);
/**
* Put string log in file
*/
void put(string logline, Level _level);
/**
* Write to log file
*/
void write(string logline);
void midnight();
void writeQueue();
bool writableQueue();
void setPrefix(string &logline);
public:
@ -99,33 +64,12 @@ 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, uint32_t groupedWriting = 0);
log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false);
/**
* Debug log line
* Put string log in file
*/
void debug(string logline);
/**
* Info log line
*/
void info(string logline);
/**
* Warning log line
*/
void warning(string logline);
/**
* Error log line
*/
void error(string logline);
/**
* Fatal log line
*/
void fatal(string logline);
void put(string logline);
/**
* Destruktor, close log files
@ -133,7 +77,6 @@ class log {
~log();
};
}
}
#endif

View File

@ -1,14 +1,9 @@
#include "../lib/log.hpp"
namespace marcelb {
namespace logging {
log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, uint32_t _groupedWriting) {
marcelb::log::log(string _dir, bool _isKeepOpen, bool _printInConsole) {
dir = _dir;
loglevel = _loglevel;
isKeepOpen = _isKeepOpen;
printInConsole = _printInConsole;
groupedWriting = _groupedWriting;
if (!isdir()) {
throw string("[ERROR] Log dir path invalid ");
@ -16,9 +11,6 @@ log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, u
setMoment();
day = moment->tm_mday;
if (groupedWriting) {
lastWriting = timelocal(moment);
}
setPath();
if (isKeepOpen) {
@ -29,72 +21,37 @@ log::log(string _dir, Level _loglevel, bool _isKeepOpen, bool _printInConsole, u
}
bool log::isdir() {
bool marcelb::log::isdir() {
struct stat sb;
return stat(dir.c_str(), &sb) == 0;
}
bool log::open() {
bool marcelb::log::open() {
logfile = ofstream (path, ios_base::app);
return logfile.is_open();
}
void log::loose() {
void marcelb::log::loose() {
logfile.close();
}
void log::setMoment() {
void marcelb::log::setMoment() {
time_t rawtime;
time (&rawtime);
moment = localtime (&rawtime);
}
void log::put(string logline, Level _level) {
if (_level < loglevel) {
return;
}
void marcelb::log::put(string logline) {
io.lock();
if (printInConsole) {
cout << logline << endl;
}
setMoment();
setPrefix(logline, _level);
midnight();
setPrefix(logline);
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 (groupedWriting && !toWrite.empty()) {
writeQueue();
}
if (isKeepOpen && logfile.is_open()) {
loose();
}
@ -106,33 +63,23 @@ void log::midnight() {
}
}
}
}
void log::writeQueue() {
string lines;
bool notEmpty = !toWrite.empty();
while (notEmpty) {
lines += toWrite.front();
toWrite.pop();
notEmpty = !toWrite.empty();
if (notEmpty) lines += "\n";
if (!isKeepOpen || !logfile.is_open()) {
if (!open()) {
throw string("[ERROR] Opening log file! ");
}
}
write(lines);
}
logfile << logline << endl;
bool log::writableQueue() {
bool _writable = false;
auto _time = timelocal(moment);
if (_time > lastWriting + groupedWriting) {
_writable = true;
lastWriting = _time;
if (!isKeepOpen && logfile.is_open()) {
loose();
}
return _writable;
io.unlock();
}
void log::setPath() {
void marcelb::log::setPath() {
if (dir[dir.length()-1] != '/') {
dir.push_back('/');
}
@ -143,62 +90,17 @@ void log::setPath() {
path = dir + to_string(moment->tm_year+1900) + '-' + mon.str() + '-' + _day.str() + ".log";
}
void log::setPrefix(string &logline, Level &_level) {
void marcelb::log::setPrefix(string &logline) {
stringstream hour, min, sec;
hour << setw(2) << setfill('0') << moment->tm_hour;
min << setw(2) << setfill('0') << moment->tm_min;
sec << setw(2) << setfill('0') << moment->tm_sec;
string _logline = hour.str() + ':' + min.str() + ':' + sec.str();// + logline;
switch (_level) {
case DEBUG:
_logline += " [DEBUG] ";
break;
case INFO:
_logline += " [INFO] ";
break;
case WARNING:
_logline += " [WARNING] ";
break;
case ERROR:
_logline += " [ERROR] ";
break;
case FATAL:
_logline += " [FATAL] ";
break;
default:
_logline += " [UNAKOWN] ";
break;
}
logline = _logline + logline;
string _logline = hour.str() + ':' + min.str() + ':' + sec.str() + ' ' + logline;
logline = _logline;
}
void log::debug(string logline) {
put(logline, DEBUG);
}
void log::info(string logline) {
put(logline, INFO);
}
void log::warning(string logline) {
put(logline, WARNING);
}
void log::error(string logline) {
put(logline, ERROR);
}
void log::fatal(string logline) {
put(logline, FATAL);
}
log::~log() {
marcelb::log::~log() {
loose();
}
}
}

View File

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

View File

@ -1,25 +1,17 @@
#include <iostream>
#include <unistd.h>
#include "../lib/log.hpp"
using namespace std;
using namespace marcelb::logging;
using namespace marcelb;
log mylog("../example", Level::DEBUG, true, false, 5);
log mylog("../example", false);
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");
int i = 0;
while(true) {
mylog.fatal("Test fatal error: "+ to_string(i++));
sleep(2);
}
// log mylog("../example", false);
mylog.put("[EVENT] Start loging");
return 0;
}

BIN
test/test.o Executable file

Binary file not shown.