-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStaticLogger.h
173 lines (142 loc) · 3.57 KB
/
StaticLogger.h
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2017 - 2024, Samsonov Andrey. All Rights Reserved.
#pragma once
#include "Containers/Deque.h"
#include "Evospace/Vector.h"
#include "Logging/StructuredLog.h"
#include <string>
#ifndef LOG_LEVEL
#define LOG(level) \
FLogHelper(FSimpleLogger::static_logger, level)
#endif
#ifndef LOG_NOTHING
#define LOG_NOTHING(level) \
true ? (void)0 : cpplog::helpers::VoidStreamClass() & LOG(level)
#endif
enum ELogLevel {
ERROR_LL,
WARN_LL,
INFO_LL,
ELogLevel_Count
};
class FSimpleLogger {
public:
void Log(ELogLevel LogLevel, const FString &Message) {
FString LogEntry = GetLogLevelString(LogLevel) + TEXT(" : ") + Message;
if (LogLevel == WARN_LL) {
UE_LOGFMT(LogTemp, Warning, "{0}", *Message);
} else if (LogLevel == ERROR_LL) {
UE_LOGFMT(LogTemp, Error, "{0}", *Message);
} else {
UE_LOGFMT(LogTemp, Log, "{0}", *Message);
}
if (LogLevel == ERROR_LL) {
ErrorEntries.PushLast(Message);
}
LogEntries.PushLast(MoveTemp(LogEntry));
++perLevelCount[LogLevel];
}
const TDeque<FString> &GetLogEntries() const {
return LogEntries;
}
void Clear() {
perLevelCount.SetNumZeroed(ELogLevel_Count);
LogEntries.Empty();
ErrorEntries.Empty();
}
static FSimpleLogger static_logger;
FSimpleLogger() {
Clear();
}
bool HasErrors() const {
return perLevelCount[ERROR_LL] > 0;
}
const FString &GetLastMessage() const {
static FString dummy = "";
if (LogEntries.IsEmpty()) {
return dummy;
}
return LogEntries.Last();
}
TArray<FString> GetErrors() const {
TArray<FString> arr;
for (auto &s : ErrorEntries) {
arr.Add(s);
}
return arr;
}
const TArray<int32> &GetLevels() const {
return perLevelCount;
}
private:
TArray<int32> perLevelCount;
TDeque<FString> LogEntries;
TDeque<FString> ErrorEntries;
FString GetLogLevelString(ELogLevel LogLevel) const {
switch (LogLevel) {
case ERROR_LL:
return TEXT("ERROR");
case WARN_LL:
return TEXT("WARN");
case INFO_LL:
return TEXT("INFO");
default:
return TEXT("UNKNOWN");
}
}
};
class FLogHelper {
public:
FLogHelper(FSimpleLogger &InLogger, ELogLevel InLogLevel)
: Logger(InLogger), LogLevel(InLogLevel) {
}
FLogHelper &operator<<(const FString &Message) {
Buffer += Message;
return *this;
}
FLogHelper &operator<<(const TCHAR *Message) {
Buffer += Message;
return *this;
}
FLogHelper &operator<<(const std::string &Message) {
Buffer += UTF8_TO_TCHAR(Message.data());
return *this;
}
FLogHelper &operator<<(const char *Message) {
Buffer += UTF8_TO_TCHAR(Message);
return *this;
}
template <size_t N>
FLogHelper &operator<<(const char (&Message)[N]) {
Buffer += UTF8_TO_TCHAR(Message);
return *this;
}
FLogHelper &operator<<(TCHAR *const Message) {
Buffer += Message;
return *this;
}
template <typename T>
FLogHelper &operator<<(const T &Value) {
Buffer += FString::Printf(TEXT("%s"), *TTypeToString(Value));
return *this;
}
~FLogHelper() {
Logger.Log(LogLevel, Buffer);
}
private:
FSimpleLogger &Logger;
ELogLevel LogLevel;
FString Buffer;
template <typename T>
FString TTypeToString(const T &Value) const {
return FString::Printf(TEXT("%s"), *FString::FromInt(Value));
}
FString TTypeToString(const FName &Value) const {
return Value.ToString();
}
FString TTypeToString(const bool &Value) const {
return Value ? "true" : "false";
}
FString TTypeToString(const FVector3i &Value) const {
return Value.ToString();
}
};