OpenShaderDesigner 0.0.1
Loading...
Searching...
No Matches
Console.h
1// =====================================================================================================================
2// Copyright 2024 Medusa Slockbower
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// =====================================================================================================================
15
16
17#ifndef CONSOLE_H
18#define CONSOLE_H
19
20#include <imgui-docking/imgui.h>
21#include <format>
22#include <iostream>
23#include <sstream>
24#include <thread>
25#include <list>
26#include <mutex>
27
28namespace OpenShaderDesigner
29{
30 class Console
31 {
32 public:
37 : uint8_t
38 {
39 SHOW_TIMESTAMP = 0b00000001
40 , SHOW_THREAD = 0b00000010
41 , SHOW_SEVERITY = 0b00000100
42 , SHOW_FILE_INFO = 0b00001000
43 , WRAP_TEXT = 0b00010000
44
45 , ALL_SETTINGS = 0xFF
46 , DEFAULT_SETTINGS = ALL_SETTINGS ^ WRAP_TEXT
47 };
48
52 inline static const std::string SettingNames[] =
53 {
54 "Timestamps", "Thread IDs", "Severity", "File Info", "Wrapping"
55 };
56
60 enum class Severity
61 : int
62 {
63 MESSAGE = 0,
64 WARNING,
65 ERROR,
66 FATAL,
67 ALERT,
68 COMMAND,
69 COUNT,
70 DEFAULT = WARNING
71 };
72
76 static inline const std::string Severities[] =
77 {
78 "Message", "Warning", "Error", "Fatal", "Alert", "Command"
79 };
80
86 inline static constexpr ImVec4 ImGuiColor(unsigned int RGB)
87 {
88 return {
89 static_cast<float>((RGB >> 24) & 255) / 255.0f, static_cast<float>((RGB >> 16) & 255) / 255.0f,
90 static_cast<float>((RGB >> 8) & 255) / 255.0f, static_cast<float>((RGB >> 0) & 255) / 255.0f
91 };
92 }
93
97 inline static const ImVec4 SeverityColors[] = {
98 ImGuiColor(0xA4B9C4FF), ImGuiColor(0xF2C554FF), ImGuiColor(0xE57327FF), ImGuiColor(0xCC211EFF),
99 ImGuiColor(0x9CDCFEFF),
100 };
101
102 static std::string ThreadID()
103 {
104 std::stringstream ss;
105 ss << std::this_thread::get_id();
106 return ss.str();
107 }
108
118 template <typename... Args>
119 static void Log(const std::string& file
120 , const int line
121 , Severity severity = Severity::DEFAULT
122 , const std::format_string<Args...>& message = ""
123 , Args&&... vargs);
124
125 static void DrawMenu();
126 static void DrawWindow();
127
128 static inline bool Open = true;
129
130 private:
131 struct LogEntry
132 {
133 const std::string Message;
134 const Severity Severity;
135 const std::string File, Timestamp, Thread;
136 const int Line;
137 };
138
144 static std::string Format(const LogEntry& entry, Setting settings);
145
150 static void ProcessCommand(const std::string& command);
151
152 inline static std::list<LogEntry> EntryLog;
153 inline static std::mutex Lock;
154 inline static int Filter = static_cast<int>(0xFFFFFFFF);
155 inline static Setting Settings = DEFAULT_SETTINGS;
156 inline static std::string Command;
157 };
158
159 template <typename... Args>
161 const std::string& file
162 , const int line
163 , Severity severity
164 , const std::format_string<Args...>& fmt
165 , Args&&... vargs)
166 {
167 auto t = std::time(nullptr);
168#ifdef _MSC_VER
169#pragma warning(disable:4996)
170#endif
171 auto tm = *std::localtime(&t);
172
173 std::lock_guard guard(Lock);
174 LogEntry entry{
175 std::vformat(fmt.get(), std::make_format_args(vargs...)), severity, file, std::format(
176 "{:0>2}:{:0>2}:{:0>2}", tm.tm_hour, tm.tm_min, tm.tm_sec),
177 ThreadID(), line
178 };
179 EntryLog.push_back(entry);
180 std::cout << Format(entry, ALL_SETTINGS) << std::endl;
181 }
182}
183
184#define Log(...) Log(__FILE__, __LINE__, __VA_ARGS__)
185
186#endif //CONSOLE_H
Definition Console.h:31
static const ImVec4 SeverityColors[]
Color for rendering each Severity level text in editor.
Definition Console.h:97
static void Log(const std::string &file, const int line, Severity severity=Severity::DEFAULT, const std::format_string< Args... > &message="", Args &&... vargs)
Thread-Safe Log function for debugging.
Definition Console.h:160
Setting
Setting for displaying log entries.
Definition Console.h:38
static const std::string SettingNames[]
String representations of the settings.
Definition Console.h:52
Severity
Severity levels for log entries.
Definition Console.h:62
static const std::string Severities[]
String representations of the Severity levels.
Definition Console.h:76
static constexpr ImVec4 ImGuiColor(unsigned int RGB)
Integer to floating point color. (ImGui APIVersion)
Definition Console.h:86