PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
log_internal.h
1#pragma once
2
3#include "log_paths.h"
4#include "pixelbullet/application/log.h"
5
6#include <chrono>
7#include <filesystem>
8#include <fstream>
9#include <memory>
10#include <mutex>
11#include <string>
12#include <string_view>
13#include <unordered_map>
14
15namespace pixelbullet::logging_internal
16{
17using SystemClock = std::chrono::system_clock;
18using SteadyClock = std::chrono::steady_clock;
19
21{
22public:
23 void Write(const log::LogRecord& record);
24
25private:
26 std::mutex mutex_;
27};
28
29class TextFileSink
30{
31public:
32 explicit TextFileSink(const std::filesystem::path& path);
33
34 void Write(const log::LogRecord& record);
35
36private:
37 std::mutex mutex_;
38 std::ofstream stream_;
39};
40
41class NdjsonSink
42{
43public:
44 explicit NdjsonSink(const std::filesystem::path& path);
45
46 void Write(const log::LogRecord& record);
47
48private:
49 std::mutex mutex_;
50 std::ofstream stream_;
51};
52
54{
55 std::string started_at;
56 std::string ended_at;
57 uint32_t process_id = 0;
58 bool used_environment_override = false;
59 std::string binary_stem;
60 std::filesystem::path binary_path;
61 std::filesystem::path current_working_directory;
62 std::filesystem::path log_root;
63 std::filesystem::path session_directory;
64 std::filesystem::path latest_manifest_path;
65 std::filesystem::path session_log_path;
66 std::filesystem::path core_log_path;
67 std::filesystem::path client_log_path;
68 std::filesystem::path validation_log_path;
69 std::filesystem::path events_ndjson_path;
70 std::string client_name;
71 std::string client_version;
72 std::string engine_name;
73 std::string engine_version;
74};
75
77{
78 std::mutex mutex;
79 bool initialized = false;
80 log::LogConfig config;
82 SessionMetadata metadata;
83 SteadyClock::time_point start_steady = SteadyClock::now();
84 std::shared_ptr<ConsoleSink> console;
85 std::shared_ptr<TextFileSink> session_sink;
86 std::shared_ptr<TextFileSink> core_sink;
87 std::shared_ptr<TextFileSink> client_sink;
88 std::shared_ptr<TextFileSink> validation_sink;
89 std::shared_ptr<NdjsonSink> ndjson;
90 std::unordered_map<std::string, std::shared_ptr<log::Logger>> loggers;
91};
92
93[[nodiscard]] LoggingState& GetState();
94[[nodiscard]] std::shared_ptr<log::Logger> GetOrCreateLogger(LoggingState& state, std::string_view name);
95
96[[nodiscard]] std::string EscapeJson(std::string_view value);
97[[nodiscard]] std::string JsonString(std::string_view value);
98[[nodiscard]] std::string JsonPath(const std::filesystem::path& path);
99[[nodiscard]] std::string JsonOptionalString(const std::string& value);
100[[nodiscard]] std::string FormatIso8601(SystemClock::time_point time_point);
101[[nodiscard]] std::string FormatElapsed(SteadyClock::duration duration);
102[[nodiscard]] std::string ThreadIdToString();
103[[nodiscard]] bool StartsWith(std::string_view value, std::string_view prefix);
104[[nodiscard]] std::string FormatTextRecord(const log::LogRecord& record);
105[[nodiscard]] std::string FormatNdjsonRecord(const log::LogRecord& record);
106
107void PersistMetadata(const LoggingState& state);
108[[nodiscard]] bool EnsureDirectoryExists(const std::filesystem::path& path);
109[[nodiscard]] log::LogRecord BuildRecord(const LoggingState& state, std::string_view logger_name, log::Level level, std::string message);
110
111void EmitRecord(const log::LogRecord& record, const std::shared_ptr<ConsoleSink>& console_sink,
112 const std::shared_ptr<TextFileSink>& session_sink, const std::shared_ptr<TextFileSink>& core_sink,
113 const std::shared_ptr<TextFileSink>& client_sink, const std::shared_ptr<TextFileSink>& validation_sink,
114 const std::shared_ptr<NdjsonSink>& ndjson);
115} // namespace pixelbullet::logging_internal
Definition log_internal.h:21
Definition log.h:35
Definition log.h:42