gpu.cpp 0.1.0
 
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1#ifndef LOGGING_H
2#define LOGGING_H
3
4#include <cstdio>
5#include <cstdarg>
6
7namespace gpu {
8
9enum LogLevel { kError = 0, kWarn = 1, kInfo = 2, kTrace = 3 };
10
11static const char *kLevelStr[] = {"error", "warn", "info", "trace"};
12
19struct Logger {
20 FILE *stream;
21 char buffer[32768]; // TODO(avh): Expand as needed or fail gracefully.
22 int level;
23};
24
25#ifndef NDEBUG
34inline void LOG(Logger& logger, int level, const char *message, ...) {
35 static const char *orange = "\033[0;33m";
36 static const char *red = "\033[0;31m";
37 static const char *white = "\033[0;37m";
38 static const char *gray = "\033[0;90m";
39 static const char *reset = "\033[0m";
40 static const char *logColors[] = {red, red, orange, gray};
41 if (level <= logger.level) {
42 va_list(args);
43 va_start(args, message);
44 snprintf(logger.buffer, sizeof(logger.buffer), message, args);
45 // Brackets and messages are white.
46 // Log levels are red for error and warning, orange for info, and grey for trace.
47 // Then the color is reset.
48 fprintf(logger.stream, "%s[%s%s%s] ", white, logColors[level], kLevelStr[level],
49 white);
50 vfprintf(logger.stream, message, args);
51 fprintf(logger.stream, "%s\n", reset);
52 va_end(args);
53 }
54}
55#else
56#define LOG(logger, level, message, ...) ((void)0)
57#endif
58
64static Logger kDefLog = {stdout, "", kInfo};
65
70void setLogLevel(int level) {
71 kDefLog.level = level;
72}
73
74} // namespace gpu
75
76#endif
Definition gpu.h:27
static Logger kDefLog
Default logger for logging messages to stdout at the info level. Output stream and logging level for ...
Definition logging.h:64
void LOG(Logger &logger, int level, const char *message,...)
Log a message to the logger. If NDEBUG is defined in a source or as a compiler flag,...
Definition logging.h:34
void setLogLevel(int level)
Set the log level of the default logger.
Definition logging.h:70
LogLevel
Definition logging.h:9
@ kError
Definition logging.h:9
@ kWarn
Definition logging.h:9
@ kTrace
Definition logging.h:9
@ kInfo
Definition logging.h:9
static const char * kLevelStr[]
Definition logging.h:11
Logger struct for logging messages. stream: The stream to log to. buffer: A buffer to store the forma...
Definition logging.h:19
FILE * stream
Definition logging.h:20
char buffer[32768]
Definition logging.h:21
int level
Definition logging.h:22