Toast Engine
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1
10
11#pragma once
12
13#include "generated/logging.pb.h"
14
15#include <asio.hpp>
16#include <atomic>
17#include <cassert>
18#include <cstdint>
19#include <deque>
20#include <memory>
21#include <mutex>
22#include <string_view>
23#include <vector>
24
25namespace logging {
26
27class Logger {
28 inline static Logger* instance = nullptr;
29
30 struct {
31 asio::io_context io_ctx;
32 asio::ip::tcp::socket socket {io_ctx};
33
34 std::deque<proto::logging::LogData> log_queue;
35 std::mutex queue_mutex;
36
43 std::atomic<bool> drain_pending = false;
44
48 std::atomic<bool> connected = false;
49
54 std::atomic<bool> unavailable = false;
55 } m;
56
57 static constexpr uint16_t port = 12800;
58 static constexpr bool auto_spawn_log_server = true;
59 static constexpr bool show_server_logs = false;
60
61public:
67 static auto create() noexcept -> std::unique_ptr<Logger>;
68
72 ~Logger() noexcept;
73
74 // Copying a global logger doesn't make sense
75 Logger(const Logger&) = delete;
76 Logger(Logger&&) = delete;
77 auto operator=(const Logger&) -> Logger& = delete;
78 auto operator=(Logger&&) -> Logger& = delete;
79
86 static void log(std::string_view file, unsigned line, char severity, std::string_view sink, std::string_view message);
87
88private:
89 Logger() = default;
90
91 void initNetworkRetry();
92 void stop();
93 void drain();
94 auto collectQueue() -> std::vector<uint8_t>;
95 void flushSync();
96};
97
98}
Definition logger.hpp:27
std::atomic< bool > unavailable
Set once initNetworkRetry has given up.
Definition logger.hpp:54
static void log(std::string_view file, unsigned line, char severity, std::string_view sink, std::string_view message)
Entry point for all engine logs.
Definition logger.cpp:196
std::atomic< bool > connected
True once initNetworkRetry has established the connection.
Definition logger.hpp:48
static auto create() noexcept -> std::unique_ptr< Logger >
Set up the global logger instance, gives ownership to the caller.
Definition logger.cpp:73
std::atomic< bool > drain_pending
Prevents multiple background jobs from fighting over the same socket.
Definition logger.hpp:43