Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/dep/cubeb/src/cubeb_log.cpp
4246 views
1
/*
2
* Copyright © 2016 Mozilla Foundation
3
*
4
* This program is made available under an ISC-style license. See the
5
* accompanying file LICENSE for details.
6
*/
7
#define NOMINMAX
8
9
#include "cubeb_log.h"
10
#include "cubeb_ringbuffer.h"
11
#include "cubeb_tracing.h"
12
#include <cstdarg>
13
#ifdef _WIN32
14
#include <windows.h>
15
#else
16
#include <time.h>
17
#endif
18
19
static std::atomic<cubeb_log_level> g_cubeb_log_level;
20
static std::atomic<cubeb_log_callback> g_cubeb_log_callback;
21
22
/** The maximum size of a log message, after having been formatted. */
23
const size_t CUBEB_LOG_MESSAGE_MAX_SIZE = 256;
24
/** The maximum number of log messages that can be queued before dropping
25
* messages. */
26
const size_t CUBEB_LOG_MESSAGE_QUEUE_DEPTH = 40;
27
/** Number of milliseconds to wait before dequeuing log messages. */
28
const size_t CUBEB_LOG_BATCH_PRINT_INTERVAL_MS = 10;
29
30
void
31
cubeb_noop_log_callback(char const * /* fmt */, ...)
32
{
33
}
34
35
void
36
cubeb_log_internal(char const * file, uint32_t line, char const * fmt, ...)
37
{
38
va_list args;
39
va_start(args, fmt);
40
char msg[CUBEB_LOG_MESSAGE_MAX_SIZE];
41
vsnprintf(msg, CUBEB_LOG_MESSAGE_MAX_SIZE, fmt, args);
42
va_end(args);
43
g_cubeb_log_callback.load()("%s:%d:%s", file, line, msg);
44
}
45
46
void
47
cubeb_log_internal_no_format(const char * msg)
48
{
49
g_cubeb_log_callback.load()(msg);
50
}
51
52
void
53
cubeb_log_set(cubeb_log_level log_level, cubeb_log_callback log_callback)
54
{
55
g_cubeb_log_level = log_level;
56
// Once a callback has a been set, `g_cubeb_log_callback` is never set back to
57
// nullptr, to prevent a TOCTOU race between checking the pointer
58
if (log_callback && log_level != CUBEB_LOG_DISABLED) {
59
g_cubeb_log_callback = log_callback;
60
} else if (!log_callback || CUBEB_LOG_DISABLED) {
61
g_cubeb_log_callback = cubeb_noop_log_callback;
62
} else {
63
assert(false && "Incorrect parameters passed to cubeb_log_set");
64
}
65
}
66
67
cubeb_log_level
68
cubeb_log_get_level()
69
{
70
return g_cubeb_log_level;
71
}
72
73
cubeb_log_callback
74
cubeb_log_get_callback()
75
{
76
if (g_cubeb_log_callback == cubeb_noop_log_callback) {
77
return nullptr;
78
}
79
return g_cubeb_log_callback;
80
}
81
82