-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
144 lines (124 loc) · 4.89 KB
/
library.cpp
File metadata and controls
144 lines (124 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "library.h"
#include <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <chrono>
#include <ctime>
cpplogger::cpplogger(bool console_enabled, std::string log_file) {
this->console_enabled = console_enabled;
this->log_file = log_file;
std::fstream logfile;
logfile.open(this->log_file, std::ios::in | std::ios::out | std::ios::app); // Dodajemy std::ios::app, żeby dodawać na końcu
if (!logfile.is_open()) {
logfile.clear();
logfile.open(this->log_file, std::ios::out);
logfile << "CPPLOGER STARTED\n";
} else {
logfile.write("CPPLOGER STARTED \n", strlen("CPPLOGER STARTED \n"));
}
this->log_file_stream = std::move(logfile);
if (console_enabled) {
this->info("Initalized cpplogger",0);
}
}
cpplogger::~cpplogger() {
this->log_file_stream.close();
}
void cpplogger::log(std::string msg) {
auto now = std::chrono::system_clock::now();
std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&time_t_now);
std::ostringstream timestamp_stream;
timestamp_stream << std::put_time(&tm_now, "%d:%m:%y %H:%M:%S");
std::string message = "[ " + timestamp_stream.str() + " ] [ LOG ] " + msg + " \n";
if (this->console_enabled) {
std::cout << message << std::endl;
}
this->log_file_stream.write(message.c_str(),strlen(message.c_str()));
}
void cpplogger::info(std::string msg, int level) {
auto now = std::chrono::system_clock::now();
std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&time_t_now);
std::ostringstream timestamp_stream;
timestamp_stream << std::put_time(&tm_now, "%d:%m:%y %H:%M:%S");
std::string message = "[ " + timestamp_stream.str() + " ] [ INFO LEVEL " + std::to_string(level) + " ] " + msg + " \n";
if (this->console_enabled) {
std::cout << message << std::endl;
}
this->log_file_stream.write(message.c_str(),strlen(message.c_str()));
}
void cpplogger::warning(std::string msg, int level) {
auto now = std::chrono::system_clock::now();
std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&time_t_now);
std::ostringstream timestamp_stream;
timestamp_stream << std::put_time(&tm_now, "%d:%m:%y %H:%M:%S");
std::string message = "[ " + timestamp_stream.str() + " ] [ WARN LEVEL " + std::to_string(level) + " ] " + msg + "\n";
if (this->console_enabled) {
std::cout << message << std::endl;
}
this->log_file_stream.write(message.c_str(),strlen(message.c_str()));
}
void cpplogger::error(std::string msg, int level) {
auto now = std::chrono::system_clock::now();
std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
std::tm tm_now = *std::localtime(&time_t_now);
std::ostringstream timestamp_stream;
timestamp_stream << std::put_time(&tm_now, "%d:%m:%y %H:%M:%S");
std::string message = "[ " + timestamp_stream.str() + " ] [ ERROR LEVEL " + std::to_string(level) + " ] " + msg + "\n";
if (this->console_enabled) {
std::cout << message << std::endl;
}
this->log_file_stream.write(message.c_str(),strlen(message.c_str()));
}
bool cpplogger::getCoutLogEnabled() const {
return this->console_enabled;
}
void cpplogger::setCoutLogEnabled(bool new_state) {
this->console_enabled = new_state;
}
extern "C" {
cpplogger_handle* cpplogger_create(bool console_enabled, const char* log_file) {
cpplogger_handle* handle = new cpplogger_handle;
handle->ptr = new cpplogger(console_enabled, std::string(log_file));
return handle;
}
void cpplogger_destroy(cpplogger_handle* handle) {
if (handle) {
delete static_cast<cpplogger*>(handle->ptr);
delete handle;
}
}
void cpplogger_log(const char* msg, cpplogger_handle* handle) {
if (handle && handle->ptr) {
static_cast<cpplogger*>(handle->ptr)->log(std::string(msg));
}
}
void cpplogger_info(const char* msg, int level, cpplogger_handle* handle) {
if (handle && handle->ptr) {
static_cast<cpplogger*>(handle->ptr)->info(std::string(msg), level);
}
}
void cpplogger_warning(const char* msg, int level, cpplogger_handle* handle) {
if (handle && handle->ptr) {
static_cast<cpplogger*>(handle->ptr)->warning(std::string(msg), level);
}
}
void cpplogger_error(const char* msg, int level, cpplogger_handle* handle) {
if (handle && handle->ptr) {
static_cast<cpplogger*>(handle->ptr)->error(std::string(msg), level);
}
}
bool cpplogger_getCoutLogEnabled(cpplogger_handle* handle) {
if (handle && handle->ptr) {
return static_cast<cpplogger*>(handle->ptr)->getCoutLogEnabled();
}
}
void cpplogger_setCoutLogEnabled(cpplogger_handle* handle, bool new_state) {
if (handle && handle->ptr) {
static_cast<cpplogger*>(handle->ptr)->setCoutLogEnabled(new_state);
}
}
}