-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
109 lines (89 loc) · 2.93 KB
/
Copy pathLogger.cpp
File metadata and controls
109 lines (89 loc) · 2.93 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
//
// Created by dPons on 3/20/19.
//
#include "Logger.h"
#include <chrono>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace std;
using namespace chrono;
/**
* Logger class that stores in file all prints
* REMEMBER: use this class as a pointer and delete manually when no more used. Otherwise log file is not saved on disk.
* @param print True if text has to be printed in console
*/
Logger::Logger(bool print) {
LogFile = new fstream();
// Get current time
auto CurrentTime = system_clock::to_time_t(system_clock::now());
// Format current time as Day/Month/DayNr_hh:dd:ss:Yr.Log
auto FormattedTime = std::ctime(&CurrentTime);
FileName.assign("Logs/").append(FormattedTime).erase(FileName.end()-1, FileName.end());
FileName += ".log";
// Replace spaces with underscores
for (char &c : FileName) {
c = c == ' ' ? '_' : c;
}
// Try to open Logs directory
DIR * LogDir = opendir("Logs/");
if (LogDir == nullptr) {
// Create new dir
mkdir("Logs/", 0777);
} else closedir(LogDir);
// Open log file
LogFile->open(FileName.data(), ios::out);
this->print = print;
}
/**
* Logger class that stores in file all prints
* REMEMBER: use this class as a pointer and delete manually when no more used. Otherwise log file is not saved on disk.
* @param fileName custom name of log file
* @param print True if text has to be printed in console
*/
Logger::Logger(const string &fileName, bool print){
LogFile = new fstream();
this->FileName.assign("Logs/");
this->FileName.append(fileName).append(".log");
// Replace spaces with underscores
for (char &c : this->FileName) {
c = c == ' ' ? '_' : c;
}
// Try to open Logs directory
DIR * LogDir = opendir("Logs/");
if (LogDir == nullptr) {
// Create new dir
mkdir("Logs/", 0777);
} else closedir(LogDir);
// Open log file
LogFile->open(this->FileName.data(), ios::out);
this->print = print;
}
/**
* Retreive this logger last message
* @return last message that was written by this logger
*/
string Logger::getLastMessage() const {
return string(LastMessage);
}
/**
* Write stuff to log/console
* @param ToWrite string to write
*/
void Logger::writeLog(const string &ToWrite) {
LastMessage.assign(ToWrite);
// Get current time
auto CurrentTime = system_clock::to_time_t(system_clock::now());
// Format current time as Day/Month/DayNr_hh:dd:ss:Yr.Log
auto FormattedTime = string(std::ctime(&CurrentTime));
FormattedTime.erase(FormattedTime.end()-1, FormattedTime.end());
string toWrite;
toWrite.assign("[").append(FormattedTime).append("]: ").append(ToWrite).append("\n");
LogFile->write(toWrite.data(), toWrite.length());
if (print) printf("[%s]: %s\n", FormattedTime.data(), ToWrite.data());
}
Logger::~Logger() {
if (LogFile->is_open())
LogFile->close();
}