Skip to content

Commit 1a71f18

Browse files
committed
add: FileLogFormatter for file sink
1 parent 3ab3d89 commit 1a71f18

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/common/FileLogFormatter.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include "pch.h"
3+
#include "spdlog/pattern_formatter.h"
4+
#include "utils.h"
5+
6+
/**
7+
* Custom formatter for spdlog. Same as default but using the getTimeStamp() function
8+
*/
9+
class FileLogFormatter : public spdlog::formatter
10+
{
11+
private:
12+
spdlog::pattern_formatter f; // Separate formatter for rest of the log messages
13+
public:
14+
FileLogFormatter()
15+
{
16+
auto format = "[%n] [%^%l%$] [%s:%#] %v";
17+
f.set_pattern(format);
18+
}
19+
void format(const spdlog::details::log_msg &msg, spdlog::memory_buf_t &dest) override
20+
{
21+
auto timeStamp = utils::getMonotonicTimeStamp();
22+
dest.append("[" + std::to_string(timeStamp) + "] "); // The only thing we need to change is the time stamp
23+
f.format(msg, dest);
24+
}
25+
26+
std::unique_ptr<formatter> clone() const override
27+
{
28+
return std::make_unique<FileLogFormatter>();
29+
}
30+
};

src/init/MainLoop.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
#include <spdlog/sinks/systemd_sink.h>
44
#include <thread>
55

6-
#include "helpers/Helper.h"
6+
#include "common/FileLogFormatter.h"
77

88
#include "IO/InterfaceImpl.h"
99
#include "chrono"
10+
#include "common/FileLogFormatter.h"
1011
#include "data/UOSMData.h"
1112
#include "spdlog/sinks/basic_file_sink.h"
1213
#include "spdlog/sinks/dup_filter_sink.h"
@@ -38,9 +39,10 @@ void setup_logging()
3839
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
3940
"logs/global_" + std::to_string(unix_timestamp_x_1000) + ".log");
4041
file_sink->set_level(FILE_LOGGING_LEVEL);
42+
file_sink->set_formatter(std::make_unique<FileLogFormatter>());
4143
dup_filter->add_sink(file_sink);
4244

43-
if (helper::getEnvOrDefault<std::string>("INSIDE_SERVICE", "0") == "0")
45+
if (utils::getEnvOrDefault<std::string>("INSIDE_SERVICE", "0") == "0")
4446
{
4547
// Log to the console
4648
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
@@ -89,7 +91,7 @@ int main()
8991
UOSMData data = UOSMData();
9092

9193
const auto targetUpdateDuration =
92-
helper::getEnvOrDefault("TARGET_UPDATE_DURATION_NS", DEFAULT_TARGET_UPDATE_DURATION_NS);
94+
utils::getEnvOrDefault("TARGET_UPDATE_DURATION_NS", DEFAULT_TARGET_UPDATE_DURATION_NS);
9395
uint64_t count = 1;
9496

9597
while (true)

0 commit comments

Comments
 (0)