-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cpp
170 lines (147 loc) · 4.67 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "main.hpp"
#include "Logger.hpp"
#include "Exceptions.hpp"
#include <iostream>
#include <iomanip>
#include <ctime>
#include "utils.hpp"
/**
* @brief The shared Logger instance;
*/
util::Logger util::Logger::instance;
/**
* Synbols for printing an image to the console.
*
* █
* std::string_format("%c", 219)
* 219
*/
const std::string util::Logger::FILL = "#";
const std::string util::Logger::EMPTY = " ";
/**
* @brief Create the Logger instance from a filename.
* If the file does not exist, it will be created, text will be appended otherwise.
*
* Disable logging if fileName is an empty string or on error.
*
* Writes version and author info to the log on start.
*
* @param fileName
* The log file to use.
*/
void util::Logger::Create(const std::string &fileName) {
if (fileName.length() > 0) {
try {
util::Logger::instance.log_file.open(fileName, ios_base::app | ios_base::out);
util::Logger::instance.enabled = true;
#ifdef ENCODER
#define ENC_TXT "Encoder"
#else
#define ENC_TXT
#endif
#ifdef DECODER
#ifdef ENCODER
#define DEC_TXT "/Decoder"
#else
#define DEC_TXT "Decoder"
#endif
#else
#define DEC_TXT
#endif
util::Logger::WriteLn("Simplified JPEG/Video " ENC_TXT DEC_TXT " by " AUTHOR " v" VERSION "\n", false);
return;
} catch (std::exception const& e) {
std::cerr << "[Logger] " << e.what() << std::endl;
}
}
util::Logger::instance.enabled = false;
}
/**
* @brief Detroy the Logging instance by closing the output file.
*/
void util::Logger::Destroy() {
if (util::Logger::instance.enabled) {
util::Logger::instance.log_file
<< std::endl
<< "----------------------------------------------------------------------"
<< std::endl << std::endl;
util::Logger::instance.log_file.close();
util::Logger::instance.enabled = false;
}
}
/**
* @brief Write the text <text> to the console and the log file.
*
* @param text
* The text to write.
* @param timestamp
* Whether to include a timestamp (in the file only).
*/
void util::Logger::Write(const std::string &text, bool timestamp) {
if (util::Logger::instance.canLog()) {
try {
std::cout << text;
if (timestamp) {
auto t = std::time(nullptr);
#ifdef _MSC_VER
tm tm_l;
localtime_s(&tm_l, &t);
tm *tm = &tm_l;
#else
auto tm = std::localtime(&t);
#endif
util::Logger::instance.log_file << "[" << std::put_time(tm, "%Y-%m-%d %H:%M:%S")
<< "] ";
}
util::Logger::instance.log_file << text;
} catch (std::exception const& e) {
std::cerr << "[Logger] " << e.what() << std::endl;
util::Logger::Destroy();
}
}
}
/**
* @brief Write the text <text> to the console and the log file and append a new line.
*
* @param text
* The text to write.
* @param timestamp
* Whether to include a timestamp (in the file only).
*/
void util::Logger::WriteLn(const std::string &text, bool timestamp) {
util::Logger::Write(text + "\n", timestamp);
}
/**
* @brief Write a progress bar to the console.
* @param iteration
* @param total
*/
void util::Logger::WriteProgress(const size_t& iteration, const size_t& total) {
static constexpr size_t LEN = 55u;
static size_t stepu = 0u;
#ifndef ENABLE_OPENMP
if (!util::Logger::instance.canLog()) return;
#endif
const bool done = (iteration == total);
if (iteration == 0) {
stepu = size_t(float(total) / std::min(LEN, total));
} else if (done || iteration % stepu == 0) {
const float progress = float(iteration) / total;
const size_t filled_len = std::min(LEN, size_t(LEN * progress));
std::cout << "\rProgress |"
<< std::string(filled_len, util::Logger::FILL[0])
<< std::string(LEN - filled_len, '-')
<< "| "
<< std::string_format("%6.2f%%", progress * 100.0f)
<< std::flush;
if (done) {
std::cout << std::endl;
}
}
}
void util::Logger::Pause(void) {
util::Logger::instance.paused = true;
}
void util::Logger::Resume(void) {
util::Logger::instance.paused = false;
}