VAE - Virtual Audio Engine 1
Small Data Driven Audio Engine
tklb::Logger Class Reference

#include <TLog.hpp>

Collaboration diagram for tklb::Logger:

Public Types

enum  Level {
  DEBUG = 0 , INFO , WARN , ERROR ,
  CRITICAL
}
 

Static Public Member Functions

static void setLevel (Level loglevel)
 
static void debug (const std::string &msg)
 
static void info (const std::string &msg)
 
static void warn (const std::string &msg)
 
static void error (const std::string &msg)
 
static void critical (const std::string &msg)
 

Private Member Functions

 Logger ()
 
 ~Logger ()
 
 Logger (const Logger &)=delete
 
Loggeroperator= (const Logger &)=delete
 

Static Private Member Functions

static Loggerinstance ()
 
static void print (const char *msg, const Level level=Level::INFO)
 

Private Attributes

std::ofstream mLogFile
 
std::mutex mWriteLock
 
Level mLevel = Level::DEBUG
 

Detailed Description

Definition at line 21 of file TLog.hpp.

Member Enumeration Documentation

◆ Level

Enumerator
DEBUG 
INFO 
WARN 
ERROR 
CRITICAL 

Definition at line 23 of file TLog.hpp.

23 {
24 DEBUG = 0,
25 INFO,
26 WARN,
27 ERROR,
29 };
@ CRITICAL
Definition: TLog.hpp:28

Constructor & Destructor Documentation

◆ Logger() [1/2]

tklb::Logger::Logger ( )
inlineprivate

Definition at line 65 of file TLog.hpp.

65 {
66 mLogFile.open("log.txt", std::ofstream::trunc);
67 if (!mLogFile.is_open()) {
68 std::cerr << "ERROR:\tCould not open log file!" << std::endl;
69 }
70 }
std::ofstream mLogFile
Definition: TLog.hpp:32

◆ ~Logger()

tklb::Logger::~Logger ( )
inlineprivate

Definition at line 72 of file TLog.hpp.

72 {
73 if (mLogFile.is_open()) {
74 print("Shutting down...");
75 mLogFile.close();
76 }
77 }
static void print(const char *msg, const Level level=Level::INFO)
Definition: TLog.hpp:41
Here is the call graph for this function:

◆ Logger() [2/2]

tklb::Logger::Logger ( const Logger )
privatedelete

Member Function Documentation

◆ critical()

static void tklb::Logger::critical ( const std::string &  msg)
inlinestatic

Definition at line 107 of file TLog.hpp.

107 {
108 print(msg.c_str(), Level::CRITICAL);
109 };
Here is the call graph for this function:

◆ debug()

static void tklb::Logger::debug ( const std::string &  msg)
inlinestatic

Definition at line 89 of file TLog.hpp.

89 {
90 #ifndef NDEBUG
91 print(msg.c_str(), Level::DEBUG);
92 #endif
93 };
Here is the call graph for this function:

◆ error()

static void tklb::Logger::error ( const std::string &  msg)
inlinestatic

Definition at line 103 of file TLog.hpp.

103 {
104 print(msg.c_str(), Level::ERROR);
105 };
Here is the call graph for this function:

◆ info()

static void tklb::Logger::info ( const std::string &  msg)
inlinestatic

Definition at line 95 of file TLog.hpp.

95 {
96 print(msg.c_str(), Level::INFO);
97 };
Here is the call graph for this function:

◆ instance()

static Logger & tklb::Logger::instance ( )
inlinestaticprivate

Definition at line 36 of file TLog.hpp.

36 {
37 static Logger _instance;
38 return _instance;
39 }
Here is the caller graph for this function:

◆ operator=()

Logger & tklb::Logger::operator= ( const Logger )
privatedelete

◆ print()

static void tklb::Logger::print ( const char *  msg,
const Level  level = Level::INFO 
)
inlinestaticprivate

Definition at line 41 of file TLog.hpp.

41 {
42 auto& inst = instance();
43 if (level < inst.mLevel) { return; }
44
45 const char* prefix = nullptr;
46 if (level == Level::DEBUG) { prefix = "DEBUG:"; }
47 if (level == Level::INFO) { prefix = "INFO:"; }
48 if (level == Level::WARN) { prefix = "WARN:"; }
49 if (level == Level::ERROR) { prefix = "ERROR:"; }
50 if (level == Level::CRITICAL) { prefix = "CRITICAL:"; }
51
52 if (level > Level::WARN) {
53 std::cerr << prefix << "\t" << msg << std::endl;
54 } else {
55 std::cout << prefix << "\t" << msg << std::endl;
56 }
57
58 if (!inst.mLogFile.is_open()) { return; }
59 inst.mWriteLock.lock();
60 inst.mLogFile << prefix << "\t" << msg << std::endl;
61 inst.mLogFile.flush();
62 inst.mWriteLock.unlock();
63 }
static Logger & instance()
Definition: TLog.hpp:36
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setLevel()

static void tklb::Logger::setLevel ( Level  loglevel)
inlinestatic

Definition at line 84 of file TLog.hpp.

84 {
85 instance().mLevel = loglevel;
86 }
Level mLevel
Definition: TLog.hpp:34
Here is the call graph for this function:

◆ warn()

static void tklb::Logger::warn ( const std::string &  msg)
inlinestatic

Definition at line 99 of file TLog.hpp.

99 {
100 print(msg.c_str(), Level::WARN);
101 };
Here is the call graph for this function:

Member Data Documentation

◆ mLevel

Level tklb::Logger::mLevel = Level::DEBUG
private

Definition at line 34 of file TLog.hpp.

◆ mLogFile

std::ofstream tklb::Logger::mLogFile
private

Definition at line 32 of file TLog.hpp.

◆ mWriteLock

std::mutex tklb::Logger::mWriteLock
private

Definition at line 33 of file TLog.hpp.


The documentation for this class was generated from the following file: