Index

zydeco / 3195a10

Experiment in graphics programming, C++, OpenGL, simulation techniques.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
631 Aug 2023 21:183195a10Update program flowJosh Stockin1259G

Blob @ zydeco / src / util / Logger.cpp

text/plain1892 bytesdownload raw
1#include <fmt/core.h>
2
3#include "ZydecoCommon.hpp"
4#include "Logger.hpp"
5
6static Logger LOGGER("LOGGER");
7
8// static initialize members
9std::ostream *Logger::s_ostream = nullptr;
10Logger::Verbosity Logger::s_maxVerbosity = Logger::DEBUG;
11int Logger::s_loggerNameLongestLen = 0;
12
13
14static const char *VERBOSITY_STRINGS[] = {
15 "", // DISABLED
16 "\e[1;031m[ ERROR ]\e[0m",
17 "\e[1;033m[ WARNING ]\e[0m",
18 "\e[1;034m[ INFO ]\e[0m",
19 "\e[1;035m[ DEBUG ]\e[0m",
20 "\e[1;036m[ VERBOSE ]\e[0m",
21 "\e[1;037m[ TRACE ]\e[0m",
22};
23
24// ctor
25Logger::Logger(std::string logger_name):
26 m_loggerName(logger_name),
27 m_loggerNameLen(logger_name.length())
28{
29 if (m_loggerNameLen > s_loggerNameLongestLen) { s_loggerNameLongestLen = m_loggerNameLen; }
30}
31
32// should be called once
33void Logger::InitializeLogging(Verbosity max_verbosity, std::ostream *stream)
34{
35 if (stream != nullptr)
36 {
37 s_maxVerbosity = max_verbosity;
38 s_ostream = stream;
39
40 LOGGER.Log(INFO, "Initialized logger with max verbosity {0}", VERBOSITY_STRINGS[max_verbosity]);
41 }
42}
43
44// output debug message
45void Logger::Log(Verbosity verbosity, std::string message)
46{
47 if (s_ostream != nullptr && verbosity <= s_maxVerbosity)
48 {
49 if (s_loggerNameLongestLen != m_loggerNameLongestLen)
50 {
51 m_loggerNameLongestLen = s_loggerNameLongestLen;
52
53 int left_padding_count = m_loggerNameLongestLen - m_loggerNameLen;
54 char left_padding[left_padding_count + 1];
55
56 for (int i = 0; i < left_padding_count; i++)
57 {
58 left_padding[i] = ' ';
59 }
60 left_padding[left_padding_count] = '\0';
61 m_leftPadding = std::string(left_padding);
62 }
63 *s_ostream << VERBOSITY_STRINGS[verbosity] << m_leftPadding << " \e[090m" << m_loggerName << "\e[0m :: " << message << "\n";
64 }
65}
66