-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.h
More file actions
242 lines (202 loc) · 5.82 KB
/
Logger.h
File metadata and controls
242 lines (202 loc) · 5.82 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#ifndef LOGGER_H
#define LOGGER_H
#include <filesystem>
#include <fstream>
#include <memory>
#include <mutex>
#include <sstream>
#include <thread>
// enable POSIX syslog support if available
#if __has_include(<syslog.h>)
#define RLS_SYSLOG
#endif
namespace rustLaunchSite
{
/// @brief Logging levels
enum class LogLevel { INF = 0, WRN = 1, ERR = 2 };
/// @brief Pure virtual interface class for a logging sink
class LogSinkI
{
public:
virtual bool CanWrite() const = 0;
virtual void Write(
std::string_view message,
std::string_view file,
const std::size_t line,
const LogLevel level = LogLevel::INF
) = 0;
virtual void Flush() {};
};
/// @brief Pure virtual interface class for a flushable log object
class LogFlushI
{
public:
virtual void Flush() = 0;
};
/// @brief Implementation of LogSink that logs to @c std::out (stdout)
class LogSinkStdout : public LogSinkI, public LogFlushI
{
public:
bool CanWrite() const override;
void Write(
std::string_view message,
std::string_view file,
const std::size_t line,
const LogLevel level = LogLevel::INF
) override;
void Flush() override;
};
/// @brief Implementation of LogSink that logs to a file
class LogSinkFile : public LogSinkI, public LogFlushI
{
public:
/// @brief Create a log sink that writes to the specified file
/// @details Output file will be truncated on open.
/// @param outputFile File/path to which this sink should write
/// @throws @c std::runtime_error if specified @c outputFile cannot be opened
/// for writing
explicit LogSinkFile(const std::filesystem::path& outputFile);
bool CanWrite() const override;
void Write(
std::string_view message,
std::string_view file,
const std::size_t line,
const LogLevel level = LogLevel::INF
) override;
void Flush() override;
private:
std::ofstream fileStream_;
};
#ifdef RLS_SYSLOG
/// @brief Implementation of LogSink that logs to a POSIX syslog
class LogSinkSyslog : public LogSinkI
{
public:
bool CanWrite() const override;
void Write(
std::string_view message,
std::string_view file,
const std::size_t line,
const LogLevel level = LogLevel::INF
) override;
};
#endif
/// @brief rustLaunchSite logging facility
///
/// @details Implements the ability to log messages to a file, stdout, or (POSIX
/// only) syslog.
///
/// Logging sink must be provided at time of construction. Using the same
/// logging sink with two @c Logger instances is unsupported, and may result in
/// undefined behavior.
///
/// If the chosen sink supports I/O flushing, this will be performed once every
/// 5 seconds as a compromise between performance and usability/reliability.
class Logger
{
public:
/// @brief Constructor
///
/// @param logSink Endpoint to which log messages should be written
explicit Logger(std::shared_ptr<LogSinkI> logSink);
/// @brief Destructor
~Logger();
/// @brief Log an error message
///
/// @details @c level values greater than @c LOGERR will be treated as
/// @c LOGERR.
///
/// This method is thread safe.
///
/// @param message Log message
/// @param file Originating source file
/// @param line Originating source line
/// @param level Log level
void Log(
std::string_view message,
std::string_view file,
const std::size_t line,
const LogLevel level = LogLevel::INF
);
/// @brief Trim the given path, leaving only the filename
/// @details Finds and returns start of filename. Constexpr for macro use.
/// @param path Path to trim
/// @return Pointer to start of filename
static constexpr auto* TrimFile(const char* const path)
{
const auto* startPosition(path);
for (const auto* currentCharacter(path)
; *currentCharacter != '\0'
; ++currentCharacter)
{
if (*currentCharacter == '\\' || *currentCharacter == '/')
{
startPosition = currentCharacter;
}
}
if (startPosition != path) ++startPosition;
return startPosition;
}
private:
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
void FlushLoop();
std::shared_ptr<LogSinkI> logSink_ = nullptr;
std::recursive_mutex mutex_;
bool hasFlush_ = false;
bool doFlush_ = false;
bool stopFlush_ = false;
std::thread flushThread_;
static std::stringstream* shutUpLintersTheMacroNeedsSstreamHeader_;
};
/// @brief Logging macro for use by @c LOGINF / @c LOGWRN / @c LOGERR
#define LOG(logger, level, message) \
{ \
std::stringstream __temp_log_stream__; __temp_log_stream__ << message; \
(logger).Log( \
__temp_log_stream__.str() \
, rustLaunchSite::Logger::TrimFile(__FILE__) \
, __LINE__ \
, level); \
}
/// @brief Log an INFO-level message to the specified logger
/// @details Must be terminated in a semicolon.
///
/// Example usage:
/// @code
/// LOGINF(myLogger, "The answer is " << x << " or something");
/// @endcode
///
/// @param logger Destination logger
/// @param message Message to log
#define LOGINF(logger, message) do { \
LOG(logger, rustLaunchSite::LogLevel::INF, message) \
} while (false)
/// @brief Log a WARNING-level message to the specified logger
/// @details Must be terminated in a semicolon.
///
/// Example usage:
/// @code
/// LOGWRN(myLogger, "The answer is " << x << " or something");
/// @endcode
///
/// @param logger Destination logger
/// @param message Message to log
#define LOGWRN(logger, message) do { \
LOG(logger, rustLaunchSite::LogLevel::WRN, message) \
} while (false)
/// @brief Log an ERROR-level message to the specified logger
/// @details Must be terminated in a semicolon.
///
/// Example usage:
/// @code
/// LOGERR(myLogger, "The answer is " << x << " or something");
/// @endcode
///
/// @param logger Destination logger
/// @param message Message to log
#define LOGERR(logger, message) do { \
LOG(logger, rustLaunchSite::LogLevel::ERR, message) \
} while (false)
}
#endif