-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.cpp
144 lines (116 loc) · 3.38 KB
/
logger.cpp
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
144
#include "logger.h"
#include "utility.h"
using namespace std;
/********************************************************************/
LoggerString::LoggerString(const std::string& type, const std::string& string) : type_(type), string_(string) {
time_left_ = LoggerMgr::timeLeft();
}
LoggerString::LoggerString(const LoggerString& logger_string) {
if( &logger_string != this ) {
type_ = logger_string.type_;
string_ = logger_string.string_;
time_left_ = logger_string.time_left_;
}
}
void LoggerString::animate(float time_passed) {
if( time_left_ > 0.f ) {
time_left_ -= time_passed;
}
}
std::string LoggerString::full_string() const {
std::string full;
full.append(type_);
full.append(": ");
full.append(string_);
return full;
}
/********************************************************************/
Logger::Logger(LoggerType type) : type_(type) {
}
Logger::Logger(const Logger& logger) {
if( &logger != this ) {
type_ = logger.type_;
string_ = logger.string_;
}
}
Logger::~Logger() {
}
string Logger::endl("endl");
Logger& Logger::operator<<(const string& str) {
if( str != Logger::endl ) {
this->appendString(str);
} else {
// display string in terminal
cout << getType() << ": " << string_ << std::endl;
if( type_ != LoggerType::DEBUG ) {
LoggerMgr::instance()->addString( LoggerString(getType(), string_) );
}
}
return *this;
}
Logger& Logger::operator<<(const int& value) {
this->appendString(Utility::itos(value));
return *this;
}
void Logger::appendString(const string& str) {
string_.append(str);
}
Logger Logger::info() {
return Logger(LoggerType::INFO);
}
Logger Logger::warning() {
return Logger(LoggerType::WARNING);
}
Logger Logger::error() {
return Logger(LoggerType::ERROR);
}
Logger Logger::debug() {
return Logger(LoggerType::DEBUG);
}
const std::string& Logger::getType() const {
if( type_ == LoggerType::INFO ) {
static std::string info_str("Info");
return info_str;
}
if( type_ == LoggerType::WARNING ) {
static std::string warning_str("Warning");
return warning_str;
}
if( type_ == LoggerType::ERROR ) {
static std::string error_str("Error");
return error_str;
}
static std::string debug_str("Debug");
return debug_str;
}
/********************************************************************/
// Initialize singleton_ to nullptr
LoggerMgr* LoggerMgr::singleton_ = nullptr;
LoggerMgr* LoggerMgr::instance() {
if( singleton_ == nullptr ) {
singleton_ = new LoggerMgr();
Logger::debug() << "creating LoggerMgr singleton" << Logger::endl;
}
return singleton_;
}
void LoggerMgr::kill() {
if( singleton_ != nullptr ) {
Logger::debug() << "destroying LoggerMgr singleton" << Logger::endl;
delete singleton_;
singleton_ = nullptr;
}
}
void LoggerMgr::addString(const LoggerString& str) {
terminal_.push_back( str );
}
void LoggerMgr::animate(float time_passed) {
for( auto& log_str : terminal_ ) {
log_str.animate(time_passed);
}
}
void LoggerMgr::moveInJournal(int index) {
LoggerString str = terminal_[index];
journal_.push_back(str);
terminal_.erase( terminal_.begin() + index);
}
/********************************************************************/