Cogs.Core
LogManager.h
1#pragma once
2
3#include "../StringView.h"
4
5#include <cstdarg>
6
7#if defined( _MSC_VER )
8 #define VALIDATE_ARGS(a)
9#else
10 #define _Printf_format_string_
11 #define VALIDATE_ARGS(a) __attribute__((format(printf, a, a+1)))
12#endif
13
14namespace Cogs
15{
19 namespace Logging
20 {
22 using LoggerCallback = void(const char* message, const char* source, int category);
23
25 using FileLineLoggerCallback = void(const char* file, int line, const char* message, const char* source, int category);
26
30 enum class Category
31 {
32 Trace = 0,
33 Debug,
34 Info,
35 Warning,
36 Error,
37 Fatal
38 };
39
48 enum ErrorGroup : uint32_t {
50
51 // Cogs.Foundation systems should add new values to this first section...
52 FirstFoundation = 0x00100000,
53 FoundationIO = 0x00110000,
54 FoundationModule = 0x00120000,
55 FoundationNetwork = 0x00130000,
56
57 // Cogs.Rendering modules belong in this section...
58 FirstRendering = 0x01000000,
59
60 // Cogs.Core should define additional IDs in this section...
61 FirstCore = 0x10000000,
62
63 // Cogs.Desktop can define group IDs here...
64 FirstDesktop = 0x20000000,
65
66 // Cogs.Terrain related systems...
67 FirstTerrain = 0x30000000,
68
69 // Known Cogs.Core extensions should use this section...
70 FirstCoreExtension = 0x40000000,
71
72 // External Cogs.Core extensions should define their own system IDs above this value...
73 // (But lower than FirstThirdParty, obviously.)
74 FirstExternalExtension = 0x90000000,
75
76 // Third party libraries (or users of them) can use group IDs from this section...
77 FirstThirdParty = 0xB0000000,
78 NvidiaCUDA = 0xB0010000,
79 NvidiaNvEnc = 0xB0020000,
80 LibSRT = 0xB0030000,
81
82 // Application specific systems should define group IDs above this value...
83 FirstApplication = 0xC0000000,
84 };
85
86 class Consumer;
87
89 void COGSFOUNDATION_API setLoggerCategory(Category category);
90
93 void COGSFOUNDATION_API updateLoggerCategory(Category category);
94
97
99 void COGSFOUNDATION_API enableUnhandledExceptionLogging();
100
107 void COGSFOUNDATION_API registerConsumer(Consumer* consumer);
108
113 void COGSFOUNDATION_API unregisterConsumer(Consumer* consumer);
114
116 void COGSFOUNDATION_API log(const char* message, const char* source, const Category category, uint32_t errorNumber);
117
119 void COGSFOUNDATION_API log(const char* source, const Category category, uint32_t errorNumber, _Printf_format_string_ const char * fmt, ...) VALIDATE_ARGS(4);
120
122 void COGSFOUNDATION_API logFileLine(const char* file, const int line, const char* source, const Category category, uint32_t errorNumber, _Printf_format_string_ const char * fmt, ...) VALIDATE_ARGS(6);
123
125 void COGSFOUNDATION_API logArgs(const char* source, const Category category, uint32_t errorNumber, const char * fmt, va_list args);
126
128 void COGSFOUNDATION_API logFileLineArgs(const char* file, const int line, const char * source, const Category category, uint32_t errorNumber, const char * fmt, va_list args);
129
131 Category COGSFOUNDATION_API parseCategoryString(const StringView category);
132
134 bool COGSFOUNDATION_API isErrorGroup(uint32_t errorNumber, ErrorGroup group);
135
139 class COGSFOUNDATION_API Log
140 {
141 public:
144 template<size_t LEN>
145 constexpr explicit Log(const char(&n)[LEN]) noexcept : literalName(n) {}
146
148 void logFileLine(const char* file, const int line, const Category category, uint32_t errorNumber, _Printf_format_string_ const char * fmt, ...) const VALIDATE_ARGS(6)
149 {
150 va_list argptr;
151 va_start(argptr, fmt);
152 Cogs::Logging::logFileLineArgs(file, line, getName(), category, errorNumber, fmt, argptr);
153 va_end(argptr);
154 }
155
157 void log(const Category category, uint32_t errorNumber, _Printf_format_string_ const char * fmt, ...) const VALIDATE_ARGS(4)
158 {
159 va_list argptr;
160 va_start(argptr, fmt);
161 Cogs::Logging::logArgs(getName(), category, errorNumber, fmt, argptr);
162 va_end(argptr);
163 }
164
165 [[nodiscard]] constexpr const char* getName() const noexcept {
166 return literalName;
167 }
168
169 protected:
172 const char* literalName = nullptr;
173
175 Log() = default;
176 };
177
180 template<size_t LEN>
181 constexpr Log getLogger(const char(&name)[LEN]) noexcept { return Log(name); }
182 }
183}
Log implementation class.
Definition: LogManager.h:140
void logFileLine(const char *file, const int line, const Category category, uint32_t errorNumber, _Printf_format_string_ const char *fmt,...) const VALIDATE_ARGS(6)
Log a formatted message with file/line information.
Definition: LogManager.h:148
constexpr Log(const char(&n)[LEN]) noexcept
Definition: LogManager.h:145
Log()=default
Default constructor available to dervied types.
void log(const Category category, uint32_t errorNumber, _Printf_format_string_ const char *fmt,...) const VALIDATE_ARGS(4)
Log a formatted message.
Definition: LogManager.h:157
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
void COGSFOUNDATION_API enableUnhandledExceptionLogging()
Enable catching and logging of hardware exceptions (crashes) before exiting. (Windows only - for now....
Definition: LogManager.cpp:245
void COGSFOUNDATION_API registerConsumer(Consumer *consumer)
Registers the specified consumer with the LogManager.
Definition: LogManager.cpp:271
void COGSFOUNDATION_API logFileLineArgs(const char *file, const int line, const char *source, const Category category, uint32_t errorNumber, const char *fmt, va_list args)
Log the given formatted string with argument list and file/line information.
Definition: LogManager.cpp:330
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:181
void(const char *message, const char *source, int category) LoggerCallback
Definition of logging callback.
Definition: LogManager.h:22
Category COGSFOUNDATION_API parseCategoryString(const StringView category)
Utility function that takes a loglevel name as a string and returns the corresponding log level enum ...
Definition: LogManager.cpp:290
void COGSFOUNDATION_API logFileLine(const char *file, const int line, const char *source, const Category category, uint32_t errorNumber, _Printf_format_string_ const char *fmt,...) VALIDATE_ARGS(6)
Logs the given formatted string (using printf formatting rules) with file/line information and source...
Definition: LogManager.cpp:322
bool COGSFOUNDATION_API isErrorGroup(uint32_t errorNumber, ErrorGroup group)
Tests whether the specified error number is from the given group.
Definition: LogManager.cpp:302
void COGSFOUNDATION_API updateLoggerCategory(Category category)
Definition: LogManager.cpp:219
void COGSFOUNDATION_API setLoggerCategory(Category category)
Sets the default category level for loggers created after this call.
Definition: LogManager.cpp:215
ErrorGroup
ErrorGroup values define the top 16-bits of module specific error numbers.
Definition: LogManager.h:48
@ NvidiaNvEnc
Error values from NvEnc are in this group.
Definition: LogManager.h:79
@ Unspecified
The default error number for legacy logger usage.
Definition: LogManager.h:49
@ NvidiaCUDA
CUDA errors are wrapped in this group.
Definition: LogManager.h:78
@ LibSRT
LibSRT doesn't define error numbers, so all SRT errors will be assigned this value.
Definition: LogManager.h:80
void(const char *file, int line, const char *message, const char *source, int category) FileLineLoggerCallback
Definition of logging callback including file and line information.
Definition: LogManager.h:25
void COGSFOUNDATION_API logArgs(const char *source, const Category category, uint32_t errorNumber, const char *fmt, va_list args)
Log the given formatted string with argument list.
Definition: LogManager.cpp:318
Category
Logging categories used to filter log messages.
Definition: LogManager.h:31
void updateMinimumCategory()
Internal.
Definition: LogManager.cpp:232
void COGSFOUNDATION_API unregisterConsumer(Consumer *consumer)
Removes the specified consumer from the LogManager.
Definition: LogManager.cpp:277
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
@ Debug
If available, the device will operate in debug mode, performing additional validation of input data,...