TinyGPU
Loading...
Searching...
No Matches
TinyGPULogger.h
Go to the documentation of this file.
1#pragma once
2#include <cstdarg>
3#include <cstdio>
4
5namespace tinygpu {
6
7/**
8 * @class TinyGPULoggerClass
9 * @brief Simple header-only logger for TinyGPU with log levels and vararg
10 * support.
11 *
12 * This logger provides printf-style logging with selectable log levels.
13 * Usage example:
14 * TinyGPULogger.setLevel(TinyGPULogger::DEBUG);
15 * TinyGPULogger.log(TinyGPULogger::INFO, "Hello %d", 42);
16 *
17 * Log levels:
18 * - DEBUG: Detailed debug information
19 * - INFO: General information
20 * - WARN: Warnings
21 * - ERROR: Errors only
22 * - NONE: Disable all logging
23 */
25 public:
26 /**
27 * @enum Level
28 * @brief Logging levels for TinyGPULogger
29 */
30 enum Level {
31 DEBUG = 0, ///< Detailed debug information
32 INFO, ///< General information
33 WARN, ///< Warnings
34 ERROR, ///< Errors only
35 NONE ///< Disable all logging
36 };
37
38 /**
39 * @brief Set the current log level.
40 * @param level The minimum log level to output.
41 */
42 void setLevel(Level level) { currentLevel() = level; }
43
44 /**
45 * @brief Get the current log level.
46 * @return The current log level.
47 */
48 Level getLevel() { return currentLevel(); }
49
50 /**
51 * @brief Log a message with printf-style formatting.
52 * @param level Log level for this message.
53 * @param fmt Format string (like printf).
54 * @param ... Arguments for the format string.
55 */
56 void log(Level level, const char* fmt, ...) {
57 if (level < currentLevel() || level == NONE) return;
58 va_list args;
59 va_start(args, fmt);
60 vlog(level, fmt, args);
61 va_end(args);
62 }
63
64 private:
65 Level lvl = INFO;
66
67 Level& currentLevel() { return lvl; }
68
69 void vlog(Level level, const char* fmt, va_list args) {
70 const char* levelNames[] = {"DEBUG", "INFO", "WARN", "ERROR"};
71 if (level < DEBUG || level > ERROR) level = ERROR;
72 printf("[%s] ", levelNames[level]);
73 vprintf(fmt, args);
74 printf("\n");
75 }
76};
77
78/// Global instance of TinyGPULogger
79static TinyGPULoggerClass TinyGPULogger;
80
81} // namespace tinygpu
Simple header-only logger for TinyGPU with log levels and vararg support.
Definition: TinyGPULogger.h:24
Level
Logging levels for TinyGPULogger.
Definition: TinyGPULogger.h:30
@ DEBUG
Detailed debug information.
Definition: TinyGPULogger.h:31
@ ERROR
Errors only.
Definition: TinyGPULogger.h:34
@ INFO
General information.
Definition: TinyGPULogger.h:32
@ WARN
Warnings.
Definition: TinyGPULogger.h:33
@ NONE
Disable all logging.
Definition: TinyGPULogger.h:35
void log(Level level, const char *fmt,...)
Log a message with printf-style formatting.
Definition: TinyGPULogger.h:56
void setLevel(Level level)
Set the current log level.
Definition: TinyGPULogger.h:42
Level getLevel()
Get the current log level.
Definition: TinyGPULogger.h:48
Definition: AVIWriter.h:9