A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
INYLogger.h
Go to the documentation of this file.
1// Copyright Csaba Molnar, Daniel Butum. All Rights Reserved.
2#pragma once
3
4#include "CoreMinimal.h"
5#include "Logging/TokenizedMessage.h"
6#include "Logging/LogCategory.h"
7// #include "INYLogger.generated.h"
8
9class FOutputDevice;
11class FMessageLogModule;
12class IMessageLogListing;
13
14
19{
21
22 // Whether to show the filters menu
23 bool bShowFilters = true;
24
25 // Whether to initially show the pages widget. Setting this to false will allow the user to manually clear the log.
26 // If this is not set & NewPage() is called on the log, the pages widget will show itself
27 bool bShowPages = false;
28
29 // Whether to allow the user to clear this log.
30 bool bAllowClear = true;
31
33 bool bDiscardDuplicates = false;
34
35 // The maximum number of pages this log can hold. Pages are managed in a first in, last out manner
36 uint32 MaxPageCount = 20;
37
38 // Whether to show this log in the main log window
39 bool bShowInLogWindow = true;
40};
41
42
43UENUM()
44enum class ENYLoggerLogLevel : uint8
45{
47
48 // Kills the program
49 // TODO use
50 // Fatal,
51
52 Error,
53 Warning,
54
55 // Log
56 Info,
57 //Log = Info,
58
59 // Verbose
60 Debug,
61 //Verbose = Debug,
62
63 // VeryVerbose
64 // Used for
65 Trace,
66 //VeryVerbose = Trace
67};
68
69
77class DLGSYSTEM_API INYLogger
78{
79 typedef INYLogger Self;
80protected:
82
83public:
84 virtual ~INYLogger() {}
85
86 // Create a new logger
87 static INYLogger New() { return Self{}; }
88 static INYLogger& Get()
89 {
90 static INYLogger Instance;
91 return Instance;
92 }
93
94 // Exclusive enables
95 Self& OnlyEnableClientConsole(APlayerController* PC)
96 {
97 EnableClientConsole(PC);
98 DisableOnScreen();
99 DisableMessageLog();
100 DisableOutputLog();
101 return *this;
102 }
103 Self& OnlyEnableOnScreen(bool bInForceEnableScreenMessages = false)
104 {
105 EnableOnScreen(bInForceEnableScreenMessages);
106 DisableClientConsole();
107 DisableMessageLog();
108 DisableOutputLog();
109 return *this;
110 }
111 Self& OnlyEnableOutputLog()
112 {
113 EnableOutputLog();
114 DisableOnScreen();
115 DisableClientConsole();
116 DisableMessageLog();
117 return *this;
118 }
119
120 Self& OnlyEnableMessageLog(bool bSuppressLoggingToOutputLog = false)
121 {
122 EnableMessageLog(bSuppressLoggingToOutputLog);
123 DisableOnScreen();
124 DisableClientConsole();
125 DisableOutputLog();
126 return *this;
127 }
128
129
130 //
131 // Client console
132 //
133
134 Self& EnableClientConsole(APlayerController* PC)
135 {
136 UseClientConsole(true);
137 SetClientConsolePlayerController(PC);
138 return *this;
139 }
140 Self& DisableClientConsole() { return UseClientConsole(false); }
141 Self& UseClientConsole(bool bValue)
143 bClientConsole = bValue;
144 return *this;
145 }
146 Self& SetClientConsolePlayerController(APlayerController* PC);
147
148 //
149 // On screen
150 //
151
152 // bInForceEnableScreenMessages - if true, even if the screen messages are disabled we will force display it
153 Self& EnableOnScreen(bool bInForceEnableScreenMessages = false) { return UseOnScreen(true, bInForceEnableScreenMessages); }
154 Self& DisableOnScreen() { return UseOnScreen(false); }
155 Self& UseOnScreen(bool bValue, bool bInForceEnableScreenMessages = false)
157 bOnScreen = bValue;
158 bForceEnableScreenMessages = bInForceEnableScreenMessages;
159 return *this;
160 }
161
162 // How long to display the on screen log messages
163 Self& SetOnScreenTimeToDisplay(float Seconds)
164 {
165 ScreenLogDisplayTimeSeconds = Seconds;
166 return *this;
167 }
168
169 // Should newer messages appear on top
170 Self& SetOnScreenNewerOnTop(bool bValue)
171 {
172 bScreenNewerOnTop = bValue;
173 return *this;
174 }
175
176 // Clears all the on screen messages
177 static void ClearAllOnScreenLogs();
178 FORCEINLINE static bool AreAllOnScreenMessagesEnabled() { return GAreScreenMessagesEnabled; }
179 FORCEINLINE static void SetAreAllOnScreenMessagesEnabled(bool bValue)
181 GAreScreenMessagesEnabled = bValue;
182 }
183 FORCEINLINE static void DisableAllOnScreenMessages() { SetAreAllOnScreenMessagesEnabled(false); }
184 FORCEINLINE static void EnableAllOnScreenMessages() { SetAreAllOnScreenMessagesEnabled(true); }
187 //
188 // Output log
189 //
190
191 Self& EnableOutputLog() { return UseOutputLog(true); }
192 Self& DisableOutputLog() { return UseOutputLog(false); }
193 Self& UseOutputLog(bool bValue)
195 bOutputLog = bValue;
196 return *this;
197 }
198
199 // The log category must exist
200 Self& SetNoOutputLogCategory() { return SetOutputLogCategory(NAME_None); }
201 Self& SetOutputLogCategory(const FLogCategoryBase& NewCategory) { return SetOutputLogCategory(NewCategory.GetCategoryName()); }
202 Self& SetOutputLogCategory(FName NewCategory)
204 PreviousOutputLogCategory = OutputLogCategory;
205 OutputLogCategory = NewCategory;
206 return *this;
207 }
208
209 // Special case for no logging, aka shipping build
210#if NO_LOGGING
211 Self& SetOutputLogCategory(FNoLoggingCategory NoLogging)
212 {
213 OutputLogCategory = NAME_None;
214 bOutputLog = false;
215 return *this;
216 }
217#endif // NO_LOGGING
218
219 //
220 // Message log
221 //
222
223 Self& EnableMessageLog(bool bSuppressLoggingToOutputLog = false) { return UseMessageLog(true, bSuppressLoggingToOutputLog); }
224 Self& DisableMessageLog() { return UseMessageLog(true); }
225 Self& UseMessageLog(bool bValue, bool bInMessageLogMirrorToOutputLog = true)
227 bMessageLog = bValue;
228 return SetMessageLogMirrorToOutputLog(bInMessageLogMirrorToOutputLog);
229 }
230
231 // Opens the log for display to the user given certain conditions.
232 // Set filter with SetOpenMessageLogLevelsHigherThan
233 Self& SetMessageLogOpenOnNewMessage(bool bValue)
234 {
235 bMessageLogOpen = bValue;
236 return *this;
237 }
238
239 // Should we mirror message log messages from this instance to the output log during flush?
240 Self& SetMessageLogMirrorToOutputLog(bool bValue)
241 {
242 bMessageLogMirrorToOutputLog = bValue;
243 return *this;
244 }
245
246 Self& DisableRedirectMessageLogLevels() { return SetRedirectMessageLogLevelsHigherThan(ENYLoggerLogLevel::NoLogging); }
247 Self& SetRedirectMessageLogLevelsHigherThan(ENYLoggerLogLevel AfterOrEqualLevel)
249 RedirectMessageLogLevelsHigherThan = AfterOrEqualLevel;
250 return *this;
251 }
252
253 // Only useful if bMessageLogOpen is set to true
254 Self& SetOpenMessageLogLevelsHigherThan(ENYLoggerLogLevel AfterOrEqualLevel)
255 {
256 OpenMessageLogLevelsHigherThan = AfterOrEqualLevel;
257 return *this;
258 }
259
260 static bool IsMessageLogNameRegistered(FName LogName);
261 static bool MessageLogUnregisterLogName(FName LogName);
262 static void MessageLogRegisterLogName(FName LogName, const FText& LogLabel, const FNYMessageLogInitializationOptions& InitOptions = {});
263#if WITH_UNREAL_DEVELOPER_TOOLS
264 static TSharedPtr<IMessageLogListing> MessageLogGetLogNameListing(FName LogName);
265#endif // WITH_UNREAL_DEVELOPER_TOOLS
266 static void MessageLogOpenLogName(FName LogName);
267
268
269 // Registers the new Message log name
270 // NOTE: Call MessageLogRegisterLogName before calling this
271 Self& SetMessageLogName(FName LogName, bool bVerify = true)
272 {
273#if WITH_UNREAL_DEVELOPER_TOOLS
274 if (bVerify && !IsMessageLogNameRegistered(LogName))
275 {
276 Warning(TEXT("SetMessageLogName: Failed to register the message log name"));
277 }
278#endif // WITH_UNREAL_DEVELOPER_TOOLS
279
280 MessageLogName = LogName;
281 return *this;
282 }
283
284 //
285 // Public accessors
286 //
287
288 FORCEINLINE FName GetOutputLogCategory() const { return OutputLogCategory; }
289 FORCEINLINE bool IsClientConsoleEnabled() const { return bClientConsole; }
290 FORCEINLINE bool IsOnScreenEnabled() const { return bOnScreen; }
291 FORCEINLINE bool IsOutputLogEnabled() const { return bOutputLog; }
292 FORCEINLINE bool IsMessageLogEnabled() const { return bMessageLog; }
294 template <typename FmtType, typename... Types>
295 void Logf(ENYLoggerLogLevel Level, const FmtType& Fmt, Types... Args)
296 {
297 static_assert(TIsArrayOrRefOfType<FmtType, TCHAR>::Value, "Formatting string must be a TCHAR array.");
298 static_assert(TAnd<TIsValidVariadicFunctionArg<Types>...>::Value, "Invalid argument(s) passed to INYLogger::Logf");
299 LogfImplementation(Level, Fmt, Args...);
300 }
301
302 template <typename FmtType, typename... Types>
303 void Errorf(const FmtType& Fmt, Types... Args) { Logf(ENYLoggerLogLevel::Error, Fmt, Args...); }
304
305 template <typename FmtType, typename... Types>
306 void Warningf(const FmtType& Fmt, Types... Args) { Logf(ENYLoggerLogLevel::Warning, Fmt, Args...); }
307
308 template <typename FmtType, typename... Types>
309 void Infof(const FmtType& Fmt, Types... Args) { Logf(ENYLoggerLogLevel::Info, Fmt, Args...); }
310
311 template <typename FmtType, typename... Types>
312 void Debugf(const FmtType& Fmt, Types... Args) { Logf(ENYLoggerLogLevel::Debug, Fmt, Args...); }
313
314 template <typename FmtType, typename... Types>
315 void Tracef(const FmtType& Fmt, Types... Args) { Logf(ENYLoggerLogLevel::Trace, Fmt, Args...); }
316
318 // void Fatal(const ANSICHAR* File, int32 Line, const FString& Message);
319 void Log(ENYLoggerLogLevel Level, const FString& Message);
320
321 // TODO implement
322 // void Fatal(const FString& Message) { Log(ENYLoggerLogLevel::Fatal, Message); }
323 FORCEINLINE void Error(const FString& Message) { Log(ENYLoggerLogLevel::Error, Message); }
324 FORCEINLINE void Warning(const FString& Message) { Log(ENYLoggerLogLevel::Warning, Message); }
325 FORCEINLINE void Info(const FString& Message) { Log(ENYLoggerLogLevel::Info, Message); }
326 FORCEINLINE void Debug(const FString& Message) { Log(ENYLoggerLogLevel::Debug, Message); }
327 FORCEINLINE void Trace(const FString& Message) { Log(ENYLoggerLogLevel::Trace, Message); }
329protected:
330 void VARARGS LogfImplementation(ENYLoggerLogLevel Level, const TCHAR* Fmt, ...);
331
332#if WITH_UNREAL_DEVELOPER_TOOLS
333 static FMessageLogModule* GetMessageLogModule();
334#endif // WITH_UNREAL_DEVELOPER_TOOLS
335
336 virtual void LogScreen(ENYLoggerLogLevel Level, const FString& Message);
337 virtual void LogOutputLog(ENYLoggerLogLevel Level, const FString& Message);
338 virtual void LogMessageLog(ENYLoggerLogLevel Level, const FString& Message);
339 virtual void LogClientConsole(ENYLoggerLogLevel Level, const FString& Message);
340
341 static ELogVerbosity::Type GetUnrealLogTypeForLogLevel(ENYLoggerLogLevel Level)
342 {
343 switch (Level)
344 {
345 // case ENYLoggerLogLevel::Fatal:
346 // return ELogVerbosity::Fatal;
347
349 return ELogVerbosity::Error;
350
352 return ELogVerbosity::Warning;
353
355 return ELogVerbosity::Verbose;
356
358 return ELogVerbosity::VeryVerbose;
359
360 default:
361 return ELogVerbosity::Log;
362 }
363 }
364 static EMessageSeverity::Type GetMessageSeverityForLogLevel(ENYLoggerLogLevel Level)
365 {
366 switch (Level)
367 {
368 // case ENYLoggerLogLevel::Fatal:
369 // return EMessageSeverity::CriticalError;
370
372 return EMessageSeverity::Error;
373
375 return EMessageSeverity::Warning;
376
377 default:
378 return EMessageSeverity::Info;
379 }
380 }
381
382 FColor GetColorForLogLevel(ENYLoggerLogLevel Level) const
383 {
384 switch (Level)
385 {
386 // case ENYLoggerLogLevel::Fatal:
387 // return ColorFatal;
388
390 return ColorError;
391
393 return ColorWarning;
394
396 return ColorDebug;
397
399 return ColorTrace;
400
401 default:
402 return ColorInfo;
403 }
404 }
405 static FOutputDevice* GetOutputDeviceFromLogLevel(ENYLoggerLogLevel Level);
406
407protected:
408 //
409 // On screen
410 //
411
412 // Output to the screen
413 bool bOnScreen = false;
414
415 // Time to stay on screen
416 float ScreenLogDisplayTimeSeconds = 5.f;
417
418 // How to scale the text for the on screen messages
419 FVector2D ScreenTextScale = FVector2D::UnitVector;
420
421 // Newer screen messages appear on top
422 bool bScreenNewerOnTop = true;
423
424 // If bScreen == true and this is true, we force enable the screen messages
425 bool bForceEnableScreenMessages = false;
426
427 //
428 // Output log
429 //
430
431 // Output to the output log and log file
432 bool bOutputLog = false;
433
434 // Category for output log
435 FName OutputLogCategory = NAME_None;
436
437 // Useful when temporarily using another category
438 FName PreviousOutputLogCategory = NAME_None;
439
440 //
441 // Message log
442 //
443
444 // Output to the message log
445 bool bMessageLog = true;
446
447 // Category for message log
448 FName MessageLogName = TEXT("PIE");
449
450 // Should we mirror message log messages from this instance to the output log during flush?
451 bool bMessageLogMirrorToOutputLog = true;
452
453 // By default the message log does not support debug output, latest is info.
454 // For the sake of sanity we redirect all levels higher than RedirectMessageLogLevelsHigherThan to the output log
455 // even if the output log is disabled.
456 // So that not to output for example debug output to the message log only to the output log.
457 // NOTE: A value of ENYLoggerLogLevel::NoLogging means no log level will get redirected
458 ENYLoggerLogLevel RedirectMessageLogLevelsHigherThan = ENYLoggerLogLevel::Warning;
459
460 // Opens the log for display to the user given certain conditions.
461 // See OpenMessageLogLevelsHigherThan for the filter
462 bool bMessageLogOpen = true;
463
464 // All the log levels messages that will open the message log window if bMessageLogOpen is true
465 // NOTE: A value of ENYLoggerLogLevel::NoLogging means all log levels will be opened if bMessageLogOpen is true
466 ENYLoggerLogLevel OpenMessageLogLevelsHigherThan = ENYLoggerLogLevel::NoLogging;
467
468 //
469 // Client console
470 //
471
472 // Output to the dropdown in game console, requires the PlayerController to be set
473 bool bClientConsole = false;
474
475 // Required to print to client console
476 APlayerController* PlayerController = nullptr;
477
478 //
479 // Colors
480 //
481 //
482 FColor ColorFatal = FColor::Red;
483 FColor ColorError = FColor::Red;
484 FColor ColorWarning = FColor::Yellow;
485 FColor ColorInfo = FColor::White;
486 FColor ColorDebug = FColor::Blue;
487 FColor ColorTrace = FColor::Cyan;
ENYLoggerLogLevel
UENUM()
Definition INYLogger.h:47
Self & SetOnScreenTimeToDisplay(float Seconds)
Definition INYLogger.h:165
Self & SetMessageLogOpenOnNewMessage(bool bValue)
Definition INYLogger.h:235
Self & EnableMessageLog(bool bSuppressLoggingToOutputLog=false)
Definition INYLogger.h:225
Self & SetOutputLogCategory(const FLogCategoryBase &NewCategory)
Definition INYLogger.h:203
FORCEINLINE bool IsOnScreenEnabled() const
Definition INYLogger.h:292
FORCEINLINE void Warning(const FString &Message)
Definition INYLogger.h:326
FORCEINLINE void Trace(const FString &Message)
Definition INYLogger.h:329
FORCEINLINE FName GetOutputLogCategory() const
Definition INYLogger.h:290
Self & OnlyEnableMessageLog(bool bSuppressLoggingToOutputLog=false)
Definition INYLogger.h:122
virtual ~INYLogger()
Definition INYLogger.h:86
Self & OnlyEnableOutputLog()
Definition INYLogger.h:113
Self & SetMessageLogMirrorToOutputLog(bool bValue)
Definition INYLogger.h:242
FORCEINLINE bool IsMessageLogEnabled() const
Definition INYLogger.h:294
Self & DisableOnScreen()
Definition INYLogger.h:156
Self & SetOpenMessageLogLevelsHigherThan(ENYLoggerLogLevel AfterOrEqualLevel)
Definition INYLogger.h:256
static INYLogger New()
Definition INYLogger.h:89
Self & SetNoOutputLogCategory()
Definition INYLogger.h:202
FORCEINLINE bool IsClientConsoleEnabled() const
Definition INYLogger.h:291
FColor GetColorForLogLevel(ENYLoggerLogLevel Level) const
Definition INYLogger.h:384
static FORCEINLINE void EnableAllOnScreenMessages()
Definition INYLogger.h:186
Self & UseOutputLog(bool bValue)
Definition INYLogger.h:195
static INYLogger & Get()
Definition INYLogger.h:90
static EMessageSeverity::Type GetMessageSeverityForLogLevel(ENYLoggerLogLevel Level)
Definition INYLogger.h:366
Self & EnableOnScreen(bool bInForceEnableScreenMessages=false)
Definition INYLogger.h:155
Self & EnableClientConsole(APlayerController *PC)
Definition INYLogger.h:136
Self & UseMessageLog(bool bValue, bool bInMessageLogMirrorToOutputLog=true)
Definition INYLogger.h:227
FORCEINLINE void Error(const FString &Message)
Definition INYLogger.h:325
Self & DisableClientConsole()
Definition INYLogger.h:142
void Logf(ENYLoggerLogLevel Level, const FmtType &Fmt, Types... Args)
Definition INYLogger.h:297
static FORCEINLINE void SetAreAllOnScreenMessagesEnabled(bool bValue)
Definition INYLogger.h:181
void Warningf(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:308
static FORCEINLINE void DisableAllOnScreenMessages()
Definition INYLogger.h:185
Self & OnlyEnableOnScreen(bool bInForceEnableScreenMessages=false)
Definition INYLogger.h:105
Self & DisableOutputLog()
Definition INYLogger.h:194
Self & SetOnScreenNewerOnTop(bool bValue)
Definition INYLogger.h:172
void Tracef(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:317
void Infof(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:311
Self & DisableRedirectMessageLogLevels()
Definition INYLogger.h:248
void Errorf(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:305
FORCEINLINE void Info(const FString &Message)
Definition INYLogger.h:327
Self & SetOutputLogCategory(FName NewCategory)
Definition INYLogger.h:204
static FORCEINLINE bool AreAllOnScreenMessagesEnabled()
Definition INYLogger.h:180
Self & SetMessageLogName(FName LogName, bool bVerify=true)
Definition INYLogger.h:273
void Debugf(const FmtType &Fmt, Types... Args)
Definition INYLogger.h:314
INYLogger Self
Definition INYLogger.h:81
Self & OnlyEnableClientConsole(APlayerController *PC)
Definition INYLogger.h:97
FORCEINLINE void Debug(const FString &Message)
Definition INYLogger.h:328
FORCEINLINE bool IsOutputLogEnabled() const
Definition INYLogger.h:293
Self & DisableMessageLog()
Definition INYLogger.h:226
Self & UseClientConsole(bool bValue)
Definition INYLogger.h:143
Self & EnableOutputLog()
Definition INYLogger.h:193
Self & UseOnScreen(bool bValue, bool bInForceEnableScreenMessages=false)
Definition INYLogger.h:157
Self & SetRedirectMessageLogLevelsHigherThan(ENYLoggerLogLevel AfterOrEqualLevel)
Definition INYLogger.h:249
static ELogVerbosity::Type GetUnrealLogTypeForLogLevel(ENYLoggerLogLevel Level)
Definition INYLogger.h:343
bool bDiscardDuplicates
Whether to check for duplicate messages & discard them.
Definition INYLogger.h:33