Index

zydeco / a4d3636

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

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
703 Sep 2023 16:015f524c5OpenGL render abstractionsJosh Stockin1129G

Blob @ zydeco / src / util / Logger.cpp

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