testing
marcelb 2 years ago
commit d12ef77ba5
  1. 8
      .vscode/settings.json
  2. 5
      example/2023-5-20.log
  3. 1
      example/2023-5-21.log
  4. 36
      lib/log.hpp
  5. 101
      src/log.cpp
  6. 1
      test/compile.sh
  7. 14
      test/test.cpp
  8. BIN
      test/test.o

@ -0,0 +1,8 @@
{
"files.associations": {
"iostream": "cpp",
"iosfwd": "cpp",
"fstream": "cpp",
"*.tcc": "cpp"
}
}

@ -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

@ -0,0 +1 @@
11:00:58 [EVENT] Start loging

@ -0,0 +1,36 @@
#ifndef _LOG_
#define _LOG_
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <time.h>
#include <sys/stat.h>
using namespace std;
class log {
public:
string dir;
bool isKeepOpen;
ofstream logfile;
struct tm * moment;
uint day;
string path;
log (string _dir, bool _isKeepOpen = true);
bool isdir();
bool open();
void loose();
void setMoment();
void setPath();
void put(string logline);
void setPrefix(string &logline);
~log();
};
#endif

@ -0,0 +1,101 @@
#include "../lib/log.hpp"
log::log(string _dir, bool _isKeepOpen) {
dir = _dir;
isKeepOpen = _isKeepOpen;
if (!isdir()) {
cout << "Eror log dir path invalid!" << endl;
exit(1);
}
setMoment();
day = moment->tm_mday;
setPath();
if (isKeepOpen) {
if (!open()) {
cout << "Error opening log file!" << endl;
exit(2);
}
}
}
bool log::isdir() {
struct stat sb;
return stat(dir.c_str(), &sb) == 0;
}
bool log::open() {
logfile = ofstream (path, ios_base::app);
return logfile.is_open();
}
void log::loose() {
logfile.close();
}
void log::setMoment() {
time_t rawtime;
time (&rawtime);
moment = localtime (&rawtime);
}
void log::put(string logline) {
setMoment();
setPrefix(logline);
if (day != moment->tm_mday) {
if (isKeepOpen && logfile.is_open()) {
loose();
}
day = moment->tm_mday;
setPath();
if (isKeepOpen) {
if (!open()) {
cout << "Error opening log file!" << endl;
exit(3);
}
}
}
if (!isKeepOpen || !logfile.is_open()) {
if (!open()) {
cout << "Error opening log file!" << endl;
exit(4);
}
}
cout << logline << endl;
logfile << logline << endl;
if (!isKeepOpen && logfile.is_open()) {
loose();
}
}
void log::setPath() {
if (dir[dir.length()-1] != '/') {
dir.push_back('/');
}
path = dir + to_string(moment->tm_year+1900) + '-' + to_string(moment->tm_mon+1) + '-' + to_string(moment->tm_mday) + ".log";
}
void 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;
logline = _logline;
}
log::~log() {
loose();
}

@ -0,0 +1 @@
g++ test.cpp ../src/* -o test.o -l sqlite3

@ -0,0 +1,14 @@
#include <iostream>
#include "../lib/log.hpp"
using namespace std;
int main() {
log mylog("../example", false);
mylog.put("[EVENT] Start loging");
return 0;
}

Binary file not shown.
Loading…
Cancel
Save