-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.cpp
69 lines (57 loc) · 1.74 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
#include <iostream>
#include "logger.h"
#include <string>
#include <string_view>
Logger::Logger()
: m_log{false}
{}
Logger &Logger::get()
{
static Logger instance;
return instance;
}
bool Logger::setup(std::filesystem::path& filePath)
{
m_stream.open(filePath, std::ofstream::out | std::ofstream::trunc);
m_log = !m_stream.fail();
return m_log;
}
void Logger::showProgress(const std::string& message, const float &ratio)
{
constexpr int32_t barSize = 50;
int32_t completeSize = ratio * barSize;
int32_t remainingSize = barSize - completeSize;
std::cerr << "\rProgress [";
for (int32_t i = 0; i < completeSize; i++)
std::cerr << "#";
for (int32_t i = 0; i < remainingSize; i++)
std::cerr << "-";
std::cerr << "] - "
<< static_cast<int>(ratio * 100)
<< "% - "
<< message;
}
void Logger::close()
{
get() << NewLine;
if (m_log)
m_stream.close();
}
template<typename T>
Logger& operator<<(Logger& logger, T text)
{
std::cerr << text;
if (logger.m_log) logger.m_stream << text;
return logger;
}
template Logger& operator<<(Logger&, size_t);
template Logger& operator<<(Logger&, int32_t);
template Logger& operator<<(Logger&, bool);
template Logger& operator<<(Logger&, double);
template Logger& operator<<(Logger&, char);
template Logger& operator<<(Logger&, char*);
template Logger& operator<<(Logger&, char const*);
template Logger& operator<<(Logger&, std::basic_string<char, std::char_traits<char>, std::allocator<char>>);
template Logger& operator<<(Logger&, std::basic_string_view<char, std::char_traits<char>>);
template Logger& operator<<(Logger&, std::filesystem::path);
template Logger& operator<<(Logger&, unsigned short);