PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
log_internal.h
1#pragma once
2
3#include "pixelbullet/logging/log.h"
4#include "pixelbullet/logging/log_paths.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 std::filesystem::path path_;
40 bool failed_ = false;
41};
42
43class NdjsonSink
44{
45public:
46 explicit NdjsonSink(const std::filesystem::path& path);
47
48 void write(const log::LogRecord& record);
49
50private:
51 std::mutex mutex_;
52 std::ofstream stream_;
53 std::filesystem::path path_;
54 bool failed_ = false;
55};
56
58{
59 std::string started_at;
60 std::string ended_at;
61 uint32_t process_id = 0;
62 bool used_environment_override = false;
63 std::string binary_stem;
64 std::filesystem::path binary_path;
65 std::filesystem::path current_working_directory;
66 std::filesystem::path log_root;
67 std::filesystem::path session_directory;
68 std::filesystem::path latest_manifest_path;
69 std::filesystem::path session_log_path;
70 std::filesystem::path core_log_path;
71 std::filesystem::path client_log_path;
72 std::filesystem::path validation_log_path;
73 std::filesystem::path events_ndjson_path;
74 std::string client_name;
75 std::string client_version;
76 std::string engine_name;
77 std::string engine_version;
78};
79
81{
82 std::mutex mutex;
83 bool initialized = false;
84 log::LogConfig config;
86 SessionMetadata metadata;
87 SteadyClock::time_point start_steady = SteadyClock::now();
88 std::shared_ptr<ConsoleSink> console;
89 std::shared_ptr<TextFileSink> session_sink;
90 std::shared_ptr<TextFileSink> core_sink;
91 std::shared_ptr<TextFileSink> client_sink;
92 std::shared_ptr<TextFileSink> validation_sink;
93 std::shared_ptr<NdjsonSink> ndjson;
94 std::unordered_map<std::string, std::shared_ptr<log::Logger>> loggers;
95};
96
98{
99 SessionMetadata metadata;
100 std::filesystem::path session_metadata_path;
101 std::filesystem::path latest_manifest_path;
102};
103
104enum class LatestManifestWrite
105{
106 Always,
107 IfCurrent,
108};
109
110[[nodiscard]] LoggingState& GetState();
111[[nodiscard]] std::shared_ptr<log::Logger> GetOrCreateLogger(LoggingState& state, std::string_view name);
112[[nodiscard]] MetadataSnapshot MakeMetadataSnapshot(const LoggingState& state);
113
114[[nodiscard]] std::string EscapeJson(std::string_view value);
115[[nodiscard]] std::string JsonString(std::string_view value);
116[[nodiscard]] std::string JsonPath(const std::filesystem::path& path);
117[[nodiscard]] std::string JsonOptionalString(const std::string& value);
118[[nodiscard]] std::string FormatIso8601(SystemClock::time_point time_point);
119[[nodiscard]] std::string FormatElapsed(SteadyClock::duration duration);
120[[nodiscard]] std::string ThreadIdToString();
121[[nodiscard]] bool logger_matches_namespace(std::string_view value, std::string_view logger_namespace);
122[[nodiscard]] std::string FormatTextRecord(const log::LogRecord& record);
123[[nodiscard]] std::string FormatNdjsonRecord(const log::LogRecord& record);
124
125void PersistMetadata(const MetadataSnapshot& snapshot, LatestManifestWrite latest_manifest_write);
126[[nodiscard]] bool EnsureDirectoryExists(const std::filesystem::path& path);
127[[nodiscard]] log::LogRecord BuildRecord(const LoggingState& state, std::string_view logger_name, log::Level level, std::string message);
128
129void EmitRecord(const log::LogRecord& record, const std::shared_ptr<ConsoleSink>& console_sink,
130 const std::shared_ptr<TextFileSink>& session_sink, const std::shared_ptr<TextFileSink>& core_sink,
131 const std::shared_ptr<TextFileSink>& client_sink, const std::shared_ptr<TextFileSink>& validation_sink,
132 const std::shared_ptr<NdjsonSink>& ndjson);
133} // namespace pixelbullet::logging_internal
Definition log_internal.h:21
Definition log.h:36
Definition log.h:43