libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_logger.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblLogger filterable log and logging macros
3 * \ingroup core
4 *
5 * This file provides the public API for creating and
6 * managing custom logger types as well as the utility
7 * macros for actually using the log system.
8 *
9 * \todo
10 * - migrate away from GblThd and towards GblThread
11 * - Spam filter: deduplicate messages within a certain interval
12 * - Default constructor values
13 *
14 * \author 2023 Falco Girgis
15 * \copyright MIT License
16 */
17
18#ifndef GIMBAL_LOGGER_H
19#define GIMBAL_LOGGER_H
20
21#include <time.h>
22#include "../meta/instances/gimbal_object.h"
23#include "../containers/gimbal_linked_list.h"
24
25/*! \name Type System
26 * \brief Type UUID and cast operators
27 * @{
28 */
29#define GBL_LOGGER_TYPE (GBL_TYPEID(GblLogger)) //!< Type UUID for GblLogger
30#define GBL_LOGGER(self) (GBL_CAST(GblLogger, self)) //!< Function-style cast to GblLogger
31#define GBL_LOGGER_CLASS(klass) (GBL_CLASS_CAST(GblLogger, klass)) //!< Function-style cast to GblLoggerClass
32#define GBL_LOGGER_GET_CLASS(self) (GBL_CLASSOF(GblLogger, self)) //!< Get GblLoggerClass from GblLogger
33//! @}
34
35/*! \name Logging Macros
36 * \brief Top-level macros used with logging system
37 * @{
38 */
39#define GBL_LOG_WRITE(flags, domain, ...) GblLogger_write(__FILE__, __func__, __LINE__,
40 domain, flags, __VA_ARGS__) //!< Generalized log write operation
41#define GBL_LOG_DEBUG(domain, ...) GBL_LOG_WRITE(GBL_LOG_DEBUG, domain, __VA_ARGS__) //!< Writes to log with GBL_LOG_DEBUG
42#define GBL_LOG_VERBOSE(domain, ...) GBL_LOG_WRITE(GBL_LOG_VERBOSE, domain, __VA_ARGS__) //!< Writes to log with GBL_LOG_VERBOSE
43#define GBL_LOG_INFO(domain, ...) GBL_LOG_WRITE(GBL_LOG_INFO, domain, __VA_ARGS__) //!< Writes to log with GBL_LOG_INFO
44#define GBL_LOG_WARN(domain, ...) GBL_LOG_WRITE(GBL_LOG_WARN, domain, __VA_ARGS__) //!< Writes to log with GBL_LOG_WARN
45#define GBL_LOG_ERROR(domain, ...) GBL_LOG_WRITE(GBL_LOG_ERROR, domain, __VA_ARGS__) //!< Writes to log with GBL_LOG_ERROR
46#define GBL_LOG_PUSH() GblLogger_push() //!< Pushes level to log stack
47#define GBL_LOG_POP(n) GblLogger_pop(n) //!< Pops N levels from log stack
48//! @}
49
50#define GBL_SELF_TYPE GblLogger
51
53
55
56//! Function signature for iterating over all loggers (see \ref GblLogger_foreach())
57typedef GblBool (*GblLoggerIterFn)(GblLogger* pLogger, void* pClosure);
59//! Flags used to tag a message or message filter
60GBL_DECLARE_FLAGS(GBL_LOG_FLAGS) {
61 GBL_LOG_REENTRANT = 0x1, //!< Denotes a GblLogger's virtual methods are reentrant
62 GBL_LOG_DEBUG = 0x2, //!< Denotes a "debug" level message, which is typically disabled
63 GBL_LOG_VERBOSE = 0x4, //!< Denotes a "verbose" level message, which is regular-level
64 GBL_LOG_INFO = 0x8, //!< Denotes an "info" message, which is typically used for noteworthy events
65 GBL_LOG_WARN = 0x10, //!< Denotes a "warning" message, which means a potential issue has arisen
66 GBL_LOG_ERROR = 0x20, //!< Denotes an "error" message, which means something has failed
67 GBL_LOG_USER = 0x40 //!< Denotess the first flag which can be used for arbitrary userdata
68};
70/*! \struct GblLoggerClass
71 * \extends GblObjectClass
72 * \brief GblClass vtable for GblLogger
73 *
74 * GblLoggerClass provides virtual methods for implementing
75 * a logger back-end. These include push/pop operations for
76 * the stack-based structured logging as well as a generic
77 * write method for doing the actual message logging.
78 *
79 * \sa GblLogger
80 */
81GBL_CLASS_DERIVE(GblLogger, GblObject)
82 //! Virtual method used to do the actual log writing for a message
83 GBL_RESULT (*pFnWrite)(GBL_SELF,
84 const char* pFile,
85 const char* pFunction,
86 size_t line,
87 GblThd* pThread,
88 time_t timeStamp,
89 const char* pDomain,
90 GBL_LOG_FLAGS flags,
91 const char* pFmt,
92 va_list varArgs);
93 //! Virtual method used to signify that another level has been pushed onto the log stack
94 GBL_RESULT (*pFnPush) (GBL_SELF,
95 GblThd* pThread);
96 //! Virtual method to denote that that \param{count} levels have been popped from the log stack
97 GBL_RESULT (*pFnPop) (GBL_SELF,
98 GblThd* pThread,
99 size_t count);
101
102/*! \struct GblLogger
103 * \extends GblObject
104 * \ingroup core
105 * \brief Object used for implementing a custom logger
106 *
107 * GblLogger is used to create a new instance of a custom logger.
108 * You can either subclass it and reimplement its virtual methods or
109 * do so by giving it a floating class (see \ref {Classes::Floating Classes}).
110 *
111 * \sa GblLoggerClass
112 */
113GBL_INSTANCE_DERIVE(GblLogger, GblObject)
115 union {
116 GblLinkedListNode listNode; //!< Internal generic list node
117 GblLogger* pNext; //!< Internal pointer to next logger in list
118 };
119 GblBool reentrant; //!< Whether the logger is already handling a messaage
121 GblFlags flagsFilter; //!< Combination of GBL_LOG_FLAGS to filter messages for
123
124//! \cond
125GBL_PROPERTIES(GblLogger,
126 (flagsFilter, GBL_GENERIC, (READ, WRITE), GBL_FLAGS_TYPE),
127 (threadFilters, GBL_GENERIC, (READ, WRITE), GBL_POINTER_TYPE),
128 (domainFilters, GBL_GENERIC, (READ, WRITE), GBL_POINTER_TYPE)
129)
130//! \endcond
131
132// ===== Static Methods =====
133GBL_EXPORT GblType GblLogger_type (void) GBL_NOEXCEPT;
134GBL_EXPORT GBL_RESULT GblLogger_register (GblLogger* pLogger) GBL_NOEXCEPT;
135GBL_EXPORT GBL_RESULT GblLogger_unregister (GblLogger* pLogger) GBL_NOEXCEPT;
136
137GBL_EXPORT GblBool GblLogger_foreach (GblLoggerIterFn pIt,
138 void* pClosure) GBL_NOEXCEPT;
139
140GBL_EXPORT GBL_RESULT GblLogger_push (void) GBL_NOEXCEPT;
141GBL_EXPORT GBL_RESULT GblLogger_pop (size_t count) GBL_NOEXCEPT;
142GBL_EXPORT size_t GblLogger_depth (void) GBL_NOEXCEPT;
143
144GBL_EXPORT GBL_RESULT GblLogger_write (const char* pFile,
145 const char* pFunction,
146 size_t line,
147 const char* pDomain,
148 GBL_LOG_FLAGS flags,
149 const char* pFmt,
150 ...) GBL_NOEXCEPT;
151
152GBL_EXPORT GBL_RESULT GblLogger_writeVa (const char* pFile,
153 const char* pFunction,
154 size_t line,
155 const char* pDomain,
156 GBL_LOG_FLAGS flags,
157 const char* pFmt,
158 va_list varArgs) GBL_NOEXCEPT;
159// ===== Instance Methods =====
160GBL_EXPORT GblLogger* GblLogger_create (GblType derived,
161 size_t allocSize,
162 GblLoggerClass* pClass) GBL_NOEXCEPT;
163
164GBL_EXPORT GBL_RESULT GblLogger_construct (GBL_SELF,
165 GblType derived,
166 GblLoggerClass* pClass) GBL_NOEXCEPT;
167
169
170GBL_EXPORT GblBool GblLogger_hasFilter (GBL_CSELF,
171 const GblThd* pThread,
172 const char* pDomain,
173 GBL_LOG_FLAGS flags) GBL_NOEXCEPT;
174
175GBL_EXPORT GBL_RESULT GblLogger_setDomainFilters (GBL_SELF,
176 const char* domains[]) GBL_NOEXCEPT;
177
178GBL_EXPORT const char** GblLogger_domainFilters (GBL_CSELF) GBL_NOEXCEPT;
179
180GBL_EXPORT GblBool GblLogger_hasDomainFilter (GBL_CSELF,
181 const char* pDomain) GBL_NOEXCEPT;
182
183GBL_EXPORT GBL_RESULT GblLogger_setThreadFilters (GBL_SELF,
184 const GblThd* pThrs[]) GBL_NOEXCEPT;
185
186GBL_EXPORT const GblThd**
187 GblLogger_threadFilters (GBL_CSELF) GBL_NOEXCEPT;
188
189GBL_EXPORT GblBool GblLogger_hasThreadFilter (GBL_CSELF,
190 const GblThd* pThr) GBL_NOEXCEPT;
191
193
194#undef GBL_SELF_TYPE
195
196#endif // GIMBAL_LOGGER_H
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_PRIVATE_BEGIN
#define GBL_CLASS_DERIVE(...)
#define GBL_DECLARE_FLAGS(F)
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_PRIVATE_END
Private data structure.
#define GBL_FLAGS_TYPE
GblType UUID for flags.
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
#define GBL_LOG_ERROR(domain,...)
Writes to log with GBL_LOG_ERROR.
#define GBL_LOG_WRITE(flags, domain,...)
Generalized log write operation/*#end#*‍/.
#define GBL_LOG_INFO(domain,...)
Writes to log with GBL_LOG_INFO.
#define GBL_LOG_DEBUG(domain,...)
Writes to log with GBL_LOG_DEBUG.
#define GBL_LOG_VERBOSE(domain,...)
Writes to log with GBL_LOG_VERBOSE.
#define GBL_LOG_WARN(domain,...)
Writes to log with GBL_LOG_WARN.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
uint32_t GblFlags
Standard-sized flags type, 32-bits across platforms.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
#define GBL_POINTER_TYPE
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:51