LkEngine 0.1.2
 
Loading...
Searching...
No Matches
ConsoleSink.h
1#pragma once
2
3#include <mutex>
4
5#include <spdlog/sinks/base_sink.h>
6
7#include "LkEngine/Core/Assert.h"
8#include "LkEngine/Core/Log/Log.h"
9
10#include "LkEngine/Editor/EditorConsolePanel.h"
11
12namespace LkEngine {
13
14 class LEditorConsoleSink : public spdlog::sinks::base_sink<std::mutex>
15 {
16 public:
17 explicit LEditorConsoleSink(const uint32_t InBufferCapacity)
18 : MessageBufferCapacity(InBufferCapacity)
19 , MessageBuffer(InBufferCapacity)
20 {
21 }
22
23 virtual ~LEditorConsoleSink() = default;
24
25 LEditorConsoleSink(const LEditorConsoleSink& Other) = delete;
26 LEditorConsoleSink& operator=(const LEditorConsoleSink& Other) = delete;
27
28 protected:
29 virtual void sink_it_(const spdlog::details::log_msg& InLogMessage) override
30 {
31 spdlog::memory_buf_t Formatted;
32 spdlog::sinks::base_sink<std::mutex>::formatter_->format(InLogMessage, Formatted);
33
34 #if defined(LK_ENGINE_MSVC)
35 const std::string LongMessage = LK_FMT_LIB::format("{}", Formatted);
36 #elif defined(LK_ENGINE_GCC) || defined(LK_ENGINE_CLANG)
37 const std::string LongMessage = LK_FMT_LIB::to_string(Formatted);
38 #endif
39 std::string ShortMessage = LongMessage;
40
41 static constexpr int MAX_LENGTH_MESSAGE = 100;
42 if (ShortMessage.length() > MAX_LENGTH_MESSAGE)
43 {
44 const std::size_t SpacePos = ShortMessage.find_first_of(' ', MAX_LENGTH_MESSAGE);
45 if (SpacePos != std::string::npos)
46 {
47 ShortMessage.replace(SpacePos, ShortMessage.length() - 1, "...");
48 }
49 }
50
51 MessageBuffer[MessageCount++] = FConsoleMessage(
52 ShortMessage,
53 LongMessage,
54 GetMessageFlags(InLogMessage.level),
55 std::chrono::system_clock::to_time_t(InLogMessage.time)
56 );
57
58 if (MessageCount == MessageBufferCapacity)
59 {
60 flush_();
61 }
62 }
63
64 virtual void flush_() override
65 {
66 for (const FConsoleMessage& Message : MessageBuffer)
67 {
68 LEditorConsolePanel::PushMessage(Message);
69 }
70
71 MessageCount = 0;
72 }
73
74 private:
75 static int16_t GetMessageFlags(spdlog::level::level_enum level)
76 {
77 int16_t Flags = 0;
78
79 switch (level)
80 {
81 case spdlog::level::trace:
82 case spdlog::level::debug:
83 {
84 Flags |= EConsoleVerbosity::Debug;
85 break;
86 }
87 case spdlog::level::info:
88 {
89 Flags |= EConsoleVerbosity::Info;
90 break;
91 }
92 case spdlog::level::warn:
93 {
94 Flags |= EConsoleVerbosity::Warning;
95 break;
96 }
97 case spdlog::level::err:
98 case spdlog::level::critical:
99 {
100 Flags |= EConsoleVerbosity::Error;
101 break;
102 }
103 }
104
105 return Flags;
106 }
107
108 private:
109 uint32_t MessageCount = 0;
110 std::vector<FConsoleMessage> MessageBuffer;
111 uint32_t MessageBufferCapacity;
112 };
113
114}
Definition ConsoleSink.h:15
Definition Asset.h:11
Definition ConsoleMessage.h:29