-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.hpp
77 lines (63 loc) · 1.71 KB
/
Logger.hpp
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
#pragma once
#include <format>
#include <iostream>
#include <source_location>
#include <string>
namespace Logger {
enum class Priority {
Debug,
Info,
Warning,
Error,
Critical,
Fatal
};
class ILogger {
public:
ILogger( ) : mVerbosity( Priority::Warning ) { }
ILogger( Priority verbosity ) : mVerbosity( verbosity ) { }
virtual ~ILogger( ) = default;
void SetVerbosity( Priority verbosity ) { mVerbosity = verbosity; }
virtual void Log(
Priority prio, const std::string& msg, const std::source_location& sl = std::source_location::current( )
) const = 0;
protected:
Priority mVerbosity;
};
class CoutLogger final : public ILogger {
public:
CoutLogger( ) : ILogger( Priority::Warning ) { }
CoutLogger( Priority verbosity ) : ILogger( verbosity ) { }
void Log( Priority prio, const std::string& msg, const std::source_location& sl = std::source_location::current( ) )
const override
{
if ( prio < mVerbosity )
return;
std::string prioString;
using enum Priority;
switch ( prio ) {
case Debug:
prioString = "[DEBUG]";
break;
case Info:
prioString = "[INFO]";
break;
case Warning:
prioString = "[WARNING]";
break;
case Error:
prioString = "[ERROR]";
break;
case Critical:
prioString = "[CRITICAL]";
break;
case Fatal:
prioString = "[FATAL]";
break;
default:
break;
}
std::cout << std::format( "{}:{} {} {}\n", sl.file_name( ), sl.line( ), prioString, msg );
}
};
} // namespace Logger