mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-11-01 22:58:50 -04:00
logger: reduce the number of appending snprintf calls
Write the timestamp and location into a temporary buffer, then include them in the message print. This makes bugs involving size vs length less likely and provides a fixed limit for how much space the filename can take in the message.
This commit is contained in:
parent
b142e7f09f
commit
1a710cad3c
1 changed files with 7 additions and 5 deletions
|
|
@ -78,6 +78,8 @@ impl_log_logv(void *object,
|
|||
#define RESERVED_LENGTH 24
|
||||
|
||||
struct impl *impl = object;
|
||||
char timestamp[19] = {0};
|
||||
char filename[64] = {0};
|
||||
char location[1000 + RESERVED_LENGTH], *p, *s;
|
||||
static const char *levels[] = { "-", "E", "W", "I", "D", "T", "*T*" };
|
||||
const char *prefix = "", *suffix = "";
|
||||
|
|
@ -101,21 +103,21 @@ impl_log_logv(void *object,
|
|||
p = location;
|
||||
len = sizeof(location) - RESERVED_LENGTH;
|
||||
|
||||
size = spa_scnprintf(p, len, "%s[%s]", prefix, levels[level]);
|
||||
|
||||
if (impl->timestamp) {
|
||||
struct timespec now;
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
|
||||
size += spa_scnprintf(p + size, len - size, "[%09lu.%06lu]",
|
||||
spa_scnprintf(timestamp, sizeof(timestamp), "[%09lu.%06lu]",
|
||||
now.tv_sec & 0x1FFFFFFF, now.tv_nsec / 1000);
|
||||
|
||||
}
|
||||
if (impl->line && line != 0) {
|
||||
s = strrchr(file, '/');
|
||||
size += spa_scnprintf(p + size, len - size, "[%s:%i %s()]",
|
||||
spa_scnprintf(filename, sizeof(filename), "[%s:%i %s()]",
|
||||
s ? s + 1 : file, line, func);
|
||||
}
|
||||
size += spa_scnprintf(p + size, len - size, " ");
|
||||
|
||||
size = spa_scnprintf(p, len, "%s[%s]%s%s", prefix, levels[level],
|
||||
timestamp, filename);
|
||||
/*
|
||||
* it is assumed that at this point `size` <= `len`,
|
||||
* which is reasonable as long as file names and function names
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue