-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks_example.cpp
More file actions
244 lines (190 loc) · 8.27 KB
/
Copy pathhooks_example.cpp
File metadata and controls
244 lines (190 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// -----------------------------------------------------------------------------
// Copyright (c) 2026 @DMsuDev. Licensed under the MIT License.
// See LICENSE file in the project root for full license text.
// -----------------------------------------------------------------------------
#include "vigil/vigil.h"
#include <iostream>
#include <vector>
#include <string>
// Demo of LogSystem hooks usage
// Shows how to register and use various hooks in the LogSystem.
// ============================================================================
// Example Initialization Macro
// ============================================================================
#define VIGIL_EXAMPLE_INIT(name) \
vigil::LogSystem::Init({ \
.Name = name, \
.LogDir = "logs/hooks", \
.ConsoleLevel = vigil::LogLevel::Trace \
});
// ============================================================================
// Helpers
// ============================================================================
static const char* LevelToString(vigil::LogLevel level)
{
switch (level)
{
case vigil::LogLevel::Trace: return "Trace";
case vigil::LogLevel::Debug: return "Debug";
case vigil::LogLevel::Info: return "Info";
case vigil::LogLevel::Warn: return "Warn";
case vigil::LogLevel::Error: return "Error";
case vigil::LogLevel::Critical: return "Critical";
default: return "Unknown";
}
}
// Simulates an in-app console that receives log messages (e.g. a game editor).
struct AppConsole
{
struct Entry {
vigil::LogLevel Level;
std::string Message;
std::string LoggerName;
};
std::vector<Entry> Entries;
void Push(const vigil::LogMessageEvent& e)
{
Entries.push_back({ e.Level, std::string(e.Message), std::string(e.LoggerName) });
std::cout << "[AppConsole] [" << LevelToString(e.Level) << "] "
<< "[" << e.LoggerName << "] " << e.Message << "\n";
}
void Print() const
{
std::cout << "\n--- AppConsole captured " << Entries.size() << " entries ---\n";
for (const auto& entry : Entries)
{
std::cout << " [" << LevelToString(entry.Level) << "] "
<< entry.Message << "\n";
}
}
};
// ============================================================================
// Demo 1 — SetHooks (all at once)
// ============================================================================
static void Demo_SetHooks()
{
std::cout << "\n========== Demo 1: SetHooks ==========\n";
if (!vigil::EnableUTF8Console())
std::cerr << "Failed to enable UTF-8 console\n";
AppConsole console;
VIGIL_EXAMPLE_INIT("HooksDemo");
vigil::LogSystem::SetHooks({
.OnMessage = [&console](const vigil::LogMessageEvent& e) {
console.Push(e);
},
.OnLevelChange = [](const vigil::LevelChangeEvent& e) {
std::cout << "[Hook] Level changed"
<< (e.LoggerName.empty() ? " globally" : " on '" + std::string(e.LoggerName) + "'")
<< ": " << LevelToString(e.OldLevel)
<< " -> " << LevelToString(e.NewLevel) << "\n";
},
.OnFlush = [](const vigil::FlushEvent& e) {
std::cout << "[Hook] Flush triggered"
<< (e.LoggerName.empty() ? " (all loggers)" : " on '" + std::string(e.LoggerName) + "'")
<< "\n";
},
.OnShutdown = [] {
std::cout << "\n[Hook] LogSystem shutting down\n";
},
});
// Basic logging — OnMessage should fire for each
VIGIL_INFO("Application started");
VIGIL_WARN("Low memory warning: {} MB remaining", 128);
VIGIL_ERROR("Failed to load asset: {}", "texture_diffuse.png");
// Named logger — OnMessage should still fire
auto& net = vigil::LogSystem::Create("Network");
net.Info("Connected to server");
net.Warn("Packet loss detected: {}%", 12);
VIGIL_LOG_NAMED("Network", vigil::LogLevel::Info, "Named logger message");
// Level change — OnLevelChange should fire
vigil::LogSystem::SetGlobalLevel(vigil::LogLevel::Warn);
// These should be filtered out (below Warn)
VIGIL_INFO("This should NOT appear");
VIGIL_DEBUG("This should NOT appear either");
// These should pass through
VIGIL_WARN("This should appear");
VIGIL_ERROR("This should appear too");
// Flush — OnFlush should fire
vigil::LogSystem::FlushAll();
console.Print();
vigil::LogSystem::Shutdown();
}
// ============================================================================
// Demo 2 — Individual setters
// ============================================================================
static void Demo_IndividualSetters()
{
std::cout << "\n========== Demo 2: Individual setters ==========\n";
VIGIL_EXAMPLE_INIT("IndividualDemo");
// Register only what you need
vigil::LogSystem::SetOnMessage([](const vigil::LogMessageEvent& e) {
if (e.Level >= vigil::LogLevel::Error)
std::cout << "[ALERT] Critical message from '" << e.LoggerName << "': " << e.Message << "\n";
});
VIGIL_INFO("This is info — no alert");
VIGIL_WARN("This is a warning — no alert");
VIGIL_ERROR("This is an error — ALERT should fire");
VIGIL_CRITICAL("This is critical — ALERT should fire");
// Replace OnMessage mid-run — now capture everything
vigil::LogSystem::SetOnMessage([](const vigil::LogMessageEvent& e) {
std::cout << "[NewCallback] " << LevelToString(e.Level) << ": " << e.Message << "\n";
});
VIGIL_INFO("Now captured by the new callback");
VIGIL_WARN("This too");
// Clear only OnMessage — other hooks remain (none registered here, but pattern is valid)
vigil::LogSystem::SetOnMessage(nullptr);
VIGIL_INFO("This fires no callback — silently logged to file only");
vigil::LogSystem::Shutdown();
}
// ============================================================================
// Demo 3 — ClearHooks
// ============================================================================
static void Demo_ClearHooks()
{
std::cout << "\n========== Demo 3: ClearHooks ==========\n";
VIGIL_EXAMPLE_INIT("ClearDemo");
vigil::LogSystem::SetOnMessage([](const vigil::LogMessageEvent& e) {
std::cout << "[Before clear] " << e.Message << "\n";
});
VIGIL_INFO("Hook active — this should print");
VIGIL_WARN("Hook active — this should print too");
vigil::LogSystem::ClearHooks();
VIGIL_INFO("Hook cleared — no callback output");
VIGIL_ERROR("Hook cleared — no callback output");
vigil::LogSystem::Shutdown();
}
// ============================================================================
// Demo 4 — Named logger level change hook
// ============================================================================
static void Demo_NamedLoggerLevelChange()
{
std::cout << "\n========== Demo 4: Named logger level change ==========\n";
VIGIL_EXAMPLE_INIT("NamedLevelDemo");
vigil::LogSystem::SetOnLevelChange([](const vigil::LevelChangeEvent& e) {
if (e.LoggerName.empty())
std::cout << "[Hook] Global level: "
<< LevelToString(e.OldLevel) << " -> " << LevelToString(e.NewLevel) << "\n";
else
std::cout << "[Hook] Logger '" << e.LoggerName << "' level: "
<< LevelToString(e.OldLevel) << " -> " << LevelToString(e.NewLevel) << "\n";
});
vigil::LogSystem::Create("Physics");
vigil::LogSystem::Create("Renderer");
// Per-logger change
vigil::LogSystem::SetLevel("Physics", vigil::LogLevel::Debug);
vigil::LogSystem::SetLevel("Renderer", vigil::LogLevel::Error);
// Global change — fires once with empty LoggerName
vigil::LogSystem::SetGlobalLevel(vigil::LogLevel::Warn);
vigil::LogSystem::Shutdown();
}
// ============================================================================
// Entry point
// ============================================================================
int main()
{
Demo_SetHooks();
Demo_IndividualSetters();
Demo_ClearHooks();
Demo_NamedLoggerLevelChange();
return 0;
}