-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYLog.h
More file actions
109 lines (94 loc) · 3.23 KB
/
Copy pathYLog.h
File metadata and controls
109 lines (94 loc) · 3.23 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// \|/ YLog
// /|\ 2018.07.28 Vladimir Zvezda
//
// The interface class for logging
#pragma once
#include "fmt/format.h"
namespace ylog {
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
struct Sink
{
// Submits the log record to the log destination
virtual void Write(const char* formattedString, const void* source = nullptr) noexcept = 0;
};
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
struct FmtLog: public Sink
{
// Max size of the log record. App can issue several log records if it has to provide a really
// long one.
enum { kMaxRecordSize = 300 };
// Write log record using fmt library
template <typename ...Args>
void Fmt(const char* fmtSyntax, Args... args) noexcept
{
FmtS(nullptr, fmtSyntax, std::forward<Args>(args)...);
}
// Write log record using fmt library
template <typename ...Args>
void FmtS(const void* source, const char* fmtSyntax, Args... args) noexcept
{
try
{
char buffer[kMaxRecordSize + 1];
auto res = fmt::format_to_n(buffer,
kMaxRecordSize,
fmtSyntax,
std::forward<Args>(args)...);
*res.out = 0;
Write(buffer, source);
}
catch (...)
{
Write("YLog: failed to format log message", source);
}
}
};
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
class Defaults final
{
// YLog that writes to nowhere, used when
class NowhereLogImpl final: public FmtLog
{
protected:
void Write(const char* formattedString, const void* source) noexcept final {}
};
public:
static FmtLog* Nowhere() noexcept
{
static NowhereLogImpl s_nowhereLog;
return &s_nowhereLog;
}
static FmtLog* Stdout() noexcept;
};
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
enum class TraceMode { On, Off };
//-------------------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
template <class Owner, TraceMode TraceModeP = TraceMode::Off>
class Loggable
{
public:
Loggable(FmtLog* pLog = nullptr) noexcept
: m_pLog(pLog == nullptr ? Defaults::Nowhere() : pLog)
{}
void SetLogger(FmtLog* pLog) noexcept { m_pLog = pLog; }
FmtLog* GetLogger() noexcept { return m_pLog; }
template <typename ...Args>
void Log(const char* fmtSyntax, Args... args)
{
m_pLog->FmtS(static_cast<const Owner*>(this), fmtSyntax, std::forward<Args>(args)...);
};
private:
FmtLog* m_pLog;
};
}