| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include "Languages.h" |
| 26 |
#include "Logger/LoggerServer.h" |
| 27 |
#include "Network/TcpServer.h" |
| 28 |
|
| 29 |
namespace Logger |
| 30 |
{ |
| 31 |
class Logger |
| 32 |
{ |
| 33 |
public: |
| 34 |
enum Level : unsigned char |
| 35 |
{ |
| 36 |
LevelOff = 0, |
| 37 |
LevelError, |
| 38 |
LevelWarning, |
| 39 |
LevelInfo, |
| 40 |
LevelDebug |
| 41 |
}; |
| 42 |
|
| 43 |
Logger(LoggerServer& server, const std::string& component) |
| 44 |
: server(server), |
| 45 |
component(component) |
| 46 |
{} |
| 47 |
|
| 48 |
~Logger() {}; |
| 49 |
|
| 50 |
static Logger* GetLogger(const std::string& component) |
| 51 |
{ |
| 52 |
return LoggerServer::Instance().GetLogger(component); |
| 53 |
} |
| 54 |
|
| 55 |
void AddFileLogger(const std::string& fileName) |
| 56 |
{ |
| 57 |
LoggerServer::Instance().AddFileLogger(fileName); |
| 58 |
} |
| 59 |
|
| 60 |
void AddConsoleLogger() |
| 61 |
{ |
| 62 |
LoggerServer::Instance().AddConsoleLogger(); |
| 63 |
} |
| 64 |
|
| 65 |
static void SetLogLevel(Level level) { logLevel = level; } |
| 66 |
static Level GetLogLevel() { return logLevel; } |
| 67 |
|
| 68 |
bool IsComponent(const std::string& component) { return component.compare(this->component) == 0; } |
| 69 |
|
| 70 |
static std::string Format(const std::string& input) { return input; } |
| 71 |
static std::string Format(char* input) { const char* constInput = input; return Format(constInput); } |
| 72 |
static std::string Format(const char* input) |
| 73 |
{ |
| 74 |
if (input == nullptr) |
| 75 |
{ |
| 76 |
return std::string(""); |
| 77 |
} |
| 78 |
return std::string(input); |
| 79 |
} |
| 80 |
|
| 81 |
template<typename... Args> |
| 82 |
static std::string Format(const std::string& input, Args... args) |
| 83 |
{ |
| 84 |
std::string output = input; |
| 85 |
FormatInternal(output, 0, args...); |
| 86 |
return output; |
| 87 |
} |
| 88 |
|
| 89 |
template<typename... Args> |
| 90 |
static std::string Format(char* input, Args... args) |
| 91 |
{ |
| 92 |
const char* constInput = input; |
| 93 |
return Format(constInput, args...); |
| 94 |
} |
| 95 |
|
| 96 |
template<typename... Args> |
| 97 |
static std::string Format(const char* input, Args... args) |
| 98 |
{ |
| 99 |
if (input == nullptr) |
| 100 |
{ |
| 101 |
return std::string(""); |
| 102 |
} |
| 103 |
return Format(std::string(input), args...); |
| 104 |
} |
| 105 |
|
| 106 |
template<typename... Args> void Error(const Languages::TextSelector text, Args... args) |
| 107 |
{ |
| 108 |
if (logLevel < LevelError) |
| 109 |
{ |
| 110 |
return; |
| 111 |
} |
| 112 |
Log("Error", Languages::GetText(text), args...); |
| 113 |
} |
| 114 |
|
| 115 |
template<typename... Args> void Warning(const Languages::TextSelector text, Args... args) |
| 116 |
{ |
| 117 |
if (logLevel < LevelWarning) |
| 118 |
{ |
| 119 |
return; |
| 120 |
} |
| 121 |
Log("Warning", Languages::GetText(text), args...); |
| 122 |
} |
| 123 |
|
| 124 |
template<typename... Args> void Info(const Languages::TextSelector text, Args... args) |
| 125 |
{ |
| 126 |
if (logLevel < LevelInfo) |
| 127 |
{ |
| 128 |
return; |
| 129 |
} |
| 130 |
Log("Info", Languages::GetText(text), args...); |
| 131 |
} |
| 132 |
|
| 133 |
template<typename... Args> void Debug(const Languages::TextSelector text, Args... args) |
| 134 |
{ |
| 135 |
Debug(Languages::GetText(text), args...); |
| 136 |
} |
| 137 |
|
| 138 |
template<typename... Args> void Debug(const std::string& text, Args... args) |
| 139 |
{ |
| 140 |
if (logLevel < LevelDebug) |
| 141 |
{ |
| 142 |
return; |
| 143 |
} |
| 144 |
Log("Debug", text, args...); |
| 145 |
} |
| 146 |
|
| 147 |
inline void HexIn(const std::string& input) |
| 148 |
{ |
| 149 |
Hex(1, reinterpret_cast<const unsigned char*>(input.c_str()), input.size()); |
| 150 |
} |
| 151 |
|
| 152 |
inline void HexOut(const std::string& input) |
| 153 |
{ |
| 154 |
Hex(2, reinterpret_cast<const unsigned char*>(input.c_str()), input.size()); |
| 155 |
} |
| 156 |
|
| 157 |
void Hex(const unsigned char direction, const unsigned char* input, const size_t size); |
| 158 |
|
| 159 |
inline void HexIn(const unsigned char* input, const size_t size) |
| 160 |
{ |
| 161 |
Hex(1, input, size); |
| 162 |
} |
| 163 |
|
| 164 |
inline void HexOut(const unsigned char* input, const size_t size) |
| 165 |
{ |
| 166 |
Hex(2, input, size); |
| 167 |
} |
| 168 |
|
| 169 |
private: |
| 170 |
static Level logLevel; |
| 171 |
LoggerServer& server; |
| 172 |
const std::string component; |
| 173 |
|
| 174 |
static void AsciiPart(std::stringstream& output, const unsigned char* input, const size_t size); |
| 175 |
static std::string DateTime(); |
| 176 |
|
| 177 |
static void Replace(std::string& workString, const unsigned char argument, const std::string& value); |
| 178 |
static void Replace(std::string& workString, const unsigned char argument, char* value) |
| 179 |
{ |
| 180 |
const char* constValue = value; |
| 181 |
Replace(workString, argument, std::string(constValue)); |
| 182 |
} |
| 183 |
static void Replace(std::string& workString, const unsigned char argument, const char* value) |
| 184 |
{ |
| 185 |
Replace(workString, argument, std::string(value == nullptr ? "" : value)); |
| 186 |
} |
| 187 |
template<typename T> |
| 188 |
static void Replace(std::string& workString, const unsigned char argument, T value) |
| 189 |
{ |
| 190 |
Replace(workString, argument, std::to_string(value)); |
| 191 |
} |
| 192 |
|
| 193 |
template<typename T> |
| 194 |
static void FormatInternal(std::string& workString, |
| 195 |
const unsigned char argument, |
| 196 |
T value) |
| 197 |
{ |
| 198 |
Replace(workString, argument, value); |
| 199 |
} |
| 200 |
|
| 201 |
template<typename T, typename... Args> |
| 202 |
static void FormatInternal(std::string& workString, |
| 203 |
const unsigned char argument, |
| 204 |
T value, |
| 205 |
Args... args) |
| 206 |
{ |
| 207 |
Replace(workString, argument, value); |
| 208 |
FormatInternal(workString, argument + 1, args...); |
| 209 |
} |
| 210 |
|
| 211 |
template<typename... Args> void Log(const std::string& type, const std::string& text, Args... args) |
| 212 |
{ |
| 213 |
server.Send(DateTime() + ": " + type + ": " + component + ": " + Format(text, args...) + "\n"); |
| 214 |
} |
| 215 |
}; |
| 216 |
} |