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
7
namespace
gpu
{
8
9
enum
LogLevel
{
kError
= 0,
kWarn
= 1,
kInfo
= 2,
kTrace
= 3 };
10
11
static
const
char
*
kLevelStr
[] = {
"error"
,
"warn"
,
"info"
,
"trace"
};
12
19
struct
Logger
{
20
FILE *
stream
;
21
char
buffer
[32768];
// TODO(avh): Expand as needed or fail gracefully.
22
int
level
;
23
};
24
25
#ifndef NDEBUG
34
inline
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
64
static
Logger
kDefLog
= {stdout,
""
,
kInfo
};
65
70
void
setLogLevel
(
int
level) {
71
kDefLog
.
level
= level;
72
}
73
74
}
// namespace gpu
75
76
#endif
gpu
Definition
gpu.h:27
gpu::kDefLog
static Logger kDefLog
Default logger for logging messages to stdout at the info level. Output stream and logging level for ...
Definition
logging.h:64
gpu::LOG
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
gpu::setLogLevel
void setLogLevel(int level)
Set the log level of the default logger.
Definition
logging.h:70
gpu::LogLevel
LogLevel
Definition
logging.h:9
gpu::kError
@ kError
Definition
logging.h:9
gpu::kWarn
@ kWarn
Definition
logging.h:9
gpu::kTrace
@ kTrace
Definition
logging.h:9
gpu::kInfo
@ kInfo
Definition
logging.h:9
gpu::kLevelStr
static const char * kLevelStr[]
Definition
logging.h:11
gpu::Logger
Logger struct for logging messages. stream: The stream to log to. buffer: A buffer to store the forma...
Definition
logging.h:19
gpu::Logger::stream
FILE * stream
Definition
logging.h:20
gpu::Logger::buffer
char buffer[32768]
Definition
logging.h:21
gpu::Logger::level
int level
Definition
logging.h:22
utils
logging.h
Generated by
1.11.0