-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.h
68 lines (54 loc) · 2.3 KB
/
logger.h
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
/********************************************************************************************************************************************************
* @file logger.h
*
* @Copyright (C) 2022 i-trace.org
*
* This file is part of iTrace Infrastructure http://www.i-trace.org/.
* iTrace Infrastructure is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* iTrace Infrastructure is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with iTrace Infrastructure. If not, see <https://www.gnu.org/licenses/>.
********************************************************************************************************************************************************/
#ifndef LOGGER_H
#define LOGGER_H
#include <fstream>
#include <chrono>
#include <ctime>
#include <cstdarg>
#include <QDateTime>
#include <iostream>
class Logger {
public:
static Logger* instance() {
if(internal_inst == nullptr) { internal_inst = new Logger; }
return internal_inst;
}
/*template<typename T>
void write(const T& t) {
if(lineEnded) { writeTime(); }
log << t;
lineEnded = false;
}*/
template<typename T>
void writeLine(const char* type, const T& text) {
if(lineEnded) { writeTime(); }
log << "[" << type << "] " << text << std::endl;
lineEnded = true;
}
void close() {
log.close();
}
private:
// Privatize the Default Constructor for Singleton
Logger() { log = std::ofstream("output_log.txt"); writeTime(); writeLine("INFO","Log file created"); };
// Function that writes time at the beginning of each log entry
void writeTime() {
log << QDateTime::currentDateTime().toString().toUtf8().constData() << ' ';
}
// Disabled for Singleton
Logger(const Logger&) {}
Logger& operator=(const Logger&) {}
static Logger* internal_inst;
std::ofstream log;
bool lineEnded = false;
};
#endif // LOGGER_H