OpenShaderDesigner 0.0.1
Loading...
Searching...
No Matches
Console.h
1// =====================================================================================================================
2// OpenShaderDesigner, an open source software utility to create materials and shaders.
3// Copyright (C) 2024 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef CONSOLE_H
20#define CONSOLE_H
21
22#include <imgui-docking/imgui.h>
23#include <filesystem>
24#include <format>
25#include <iostream>
26#include <sstream>
27#include <thread>
28#include <list>
29#include <mutex>
30
31namespace OpenShaderDesigner
32{
33
35{
36public:
41 : uint8_t
42 {
43 Settings_ShowTimeStamp = 0b00000001
44 , Settings_ShowThread = 0b00000010
45 , Settings_Severity = 0b00000100
46 , Settings_ShowFileInfo = 0b00001000
47 , Settings_WrapText = 0b00010000
48
49 , Settings_ALL = 0xFF
50 , Settings_Default = Settings_ALL ^ Settings_WrapText
51 };
52
56 inline static const std::string SettingNames[] =
57 {
58 "Timestamps", "Thread IDs", "Severity", "File Info", "Wrapping"
59 };
60
65 : int
66 {
67 Message = 0,
68 Warning,
69 Error,
70 Fatal,
71 Alert,
72 Command,
73 COUNT,
74 DEFAULT = Warning
75 };
76
80 static inline const std::string Severities[] =
81 {
82 "Message", "Warning", "Error", "Fatal", "Alert", "Command"
83 };
84
90 inline static constexpr ImVec4 ImGuiColor(unsigned int RGB)
91 {
92 return {
93 static_cast<float>((RGB >> 24) & 255) / 255.0f, static_cast<float>((RGB >> 16) & 255) / 255.0f,
94 static_cast<float>((RGB >> 8) & 255) / 255.0f, static_cast<float>((RGB >> 0) & 255) / 255.0f
95 };
96 }
97
101 inline static const ImVec4 SeverityColors[] = {
102 ImGuiColor(0xA4B9C4FF), ImGuiColor(0xF2C554FF), ImGuiColor(0xE57327FF), ImGuiColor(0xCC211EFF),
103 ImGuiColor(0x9CDCFEFF),
104 };
105
106 static std::string ThreadID()
107 {
108 std::stringstream ss;
109 ss << std::this_thread::get_id();
110 return ss.str();
111 }
112
122 template <typename... Args>
123 static void Log(const std::string& file
124 , const int line
125 , Severity severity = Severity::DEFAULT
126 , const std::format_string<Args...>& message = ""
127 , Args&&... vargs);
128
129 static void DrawMenu();
130 static void DrawWindow();
131
132 static inline bool Open = true;
133
134private:
135 struct LogEntry
136 {
137 const std::string Message;
138 const Severity Severity;
139 const std::string File, Timestamp, Thread;
140 const int Line;
141 };
142
148 static std::string Format(const LogEntry& entry, uint8_t settings);
149
154 static void ProcessCommand(const std::string& command);
155
156 inline static std::list<LogEntry> EntryLog_;
157 inline static std::mutex Lock_;
158 inline static int Filter_ = static_cast<int>(0xFFFFFFFF);
159 inline static uint8_t Settings_ = Settings_Default;
160 inline static std::string CommandBuffer_;
161};
162
163template <typename... Args>
165 const std::string& file
166 , const int line
167 , Severity severity
168 , const std::format_string<Args...>& fmt
169 , Args&&... vargs)
170{
171 auto t = std::time(nullptr);
172#ifdef _MSC_VER
173#pragma warning(disable:4996)
174#endif
175 auto tm = *std::localtime(&t);
176 const auto rel = std::filesystem::relative(file, PROJECT_DIR).string();
177
178 std::lock_guard guard(Lock_);
179 LogEntry entry{
180 std::vformat(fmt.get(), std::make_format_args(vargs...)), severity, rel, std::format(
181 "{:0>2}:{:0>2}:{:0>2}", tm.tm_hour, tm.tm_min, tm.tm_sec),
182 ThreadID(), line
183 };
184 EntryLog_.push_back(entry);
185 std::cout << Format(entry, Settings_ALL) << std::endl;
186}
187
188}
189
190#define Log(...) Log(__FILE__, __LINE__, __VA_ARGS__)
191
192#endif //CONSOLE_H
Definition Console.h:35
static const ImVec4 SeverityColors[]
Color for rendering each Severity level text in editor.
Definition Console.h:101
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:164
static const std::string SettingNames[]
String representations of the settings.
Definition Console.h:56
Severity
Severity levels for log entries.
Definition Console.h:66
static const std::string Severities[]
String representations of the Severity levels.
Definition Console.h:80
static constexpr ImVec4 ImGuiColor(unsigned int RGB)
Integer to floating point color. (ImGui APIVersion)
Definition Console.h:90
Settings
Setting for displaying log entries.
Definition Console.h:42