Skip to content

Commit cd762c5

Browse files
committed
Add wob_log_* functions
1 parent d16c141 commit cd762c5

9 files changed

+221
-45
lines changed

.clang-format

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ UseTab: ForContinuationAndIndentation
1717
AlwaysBreakBeforeMultilineStrings: 'true'
1818
BinPackArguments: 'false'
1919
ForEachMacros: ['wl_list_for_each_safe', 'wl_list_for_each']
20+
AlignAfterOpenBracket: AlwaysBreak

buffer.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1+
#define WOB_FILE "buffer.c"
2+
13
#define _POSIX_C_SOURCE 200112L
24

35
#include <errno.h>
46
#include <fcntl.h>
57
#include <limits.h>
68
#include <stdio.h>
9+
#include <string.h>
710
#include <sys/mman.h>
811
#include <unistd.h>
912

1013
#include "buffer.h"
14+
#include "log.h"
1115

1216
int
1317
wob_shm_create()
@@ -25,12 +29,12 @@ wob_shm_create()
2529
}
2630

2731
if (shmid < 0) {
28-
perror("shm_open");
32+
wob_log_error("shm_open() failed: %s", strerror(errno));
2933
return -1;
3034
}
3135

3236
if (shm_unlink(shm_name) != 0) {
33-
perror("shm_unlink");
37+
wob_log_error("shm_unlink() failed: %s", strerror(errno));
3438
return -1;
3539
}
3640

@@ -41,13 +45,13 @@ void *
4145
wob_shm_alloc(const int shmid, const size_t size)
4246
{
4347
if (ftruncate(shmid, size) != 0) {
44-
perror("ftruncate");
48+
wob_log_error("ftruncate() failed: %s", strerror(errno));
4549
return NULL;
4650
}
4751

4852
void *buffer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shmid, 0);
4953
if (buffer == MAP_FAILED) {
50-
perror("mmap");
54+
wob_log_error("mmap() failed: %s", strerror(errno));
5155
return NULL;
5256
}
5357

include/log.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef _WOB_LOG_H
2+
#define _WOB_LOG_H
3+
4+
#include <stdbool.h>
5+
6+
typedef enum {
7+
_WOB_LOG_DEBUG = 0,
8+
_WOB_LOG_INFO = 1,
9+
_WOB_LOG_WARN = 2,
10+
_WOB_LOG_ERROR = 3,
11+
} _wob_log_importance;
12+
13+
void _wob_log(const _wob_log_importance importance, const char *file, const int line, const char *fmt, ...);
14+
15+
void _wob_log_set_level(const _wob_log_importance importance);
16+
17+
void wob_log_inc_verbosity(void);
18+
19+
void wob_log_use_colors(bool use_colors);
20+
21+
#define wob_log_debug(...) _wob_log(_WOB_LOG_DEBUG, WOB_FILE, __LINE__, __VA_ARGS__)
22+
#define wob_log_info(...) _wob_log(_WOB_LOG_INFO, WOB_FILE, __LINE__, __VA_ARGS__)
23+
#define wob_log_warn(...) _wob_log(_WOB_LOG_WARN, WOB_FILE, __LINE__, __VA_ARGS__)
24+
#define wob_log_error(...) _wob_log(_WOB_LOG_ERROR, WOB_FILE, __LINE__, __VA_ARGS__)
25+
26+
#define wob_log_level_debug() _wob_log_set_level(_WOB_LOG_DEBUG);
27+
#define wob_log_level_info() _wob_log_set_level(_WOB_LOG_INFO);
28+
#define wob_log_level_warn() _wob_log_set_level(_WOB_LOG_WARN);
29+
#define wob_log_level_error() _wob_log_set_level(_WOB_LOG_ERROR);
30+
31+
#endif

log.c

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#define WOB_FILE "log.c"
2+
3+
#define _POSIX_C_SOURCE 199506L
4+
5+
#define COLOR_RESET "\x1B[0m"
6+
#define COLOR_WHITE "\x1B[1;37m"
7+
#define COLOR_BLACK "\x1B[0;30m"
8+
#define COLOR_BLUE "\x1B[0;34m"
9+
#define COLOR_LIGHT_BLUE "\x1B[1;34m"
10+
#define COLOR_GREEN "\x1B[0;32m"
11+
#define COLOR_LIGHT_GREEN "\x1B[1;32m"
12+
#define COLOR_CYAN "\x1B[0;36m"
13+
#define COLOR_LIGHT_CYAN "\x1B[1;36m"
14+
#define COLOR_RED "\x1B[0;31m"
15+
#define COLOR_LIGHT_RED "\x1B[1;31m"
16+
#define COLOR_PURPLE "\x1B[0;35m"
17+
#define COLOR_LIGHT_PURPLE "\x1B[1;35m"
18+
#define COLOR_BROWN "\x1B[0;33m"
19+
#define COLOR_YELLOW "\x1B[1;33m"
20+
#define COLOR_GRAY "\x1B[0;30m"
21+
#define COLOR_LIGHT_GRAY "\x1B[0;37m"
22+
23+
#include <errno.h>
24+
#include <stdarg.h>
25+
#include <stdbool.h>
26+
#include <stdint.h>
27+
#include <stdio.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include <time.h>
31+
#include <unistd.h>
32+
33+
#include "log.h"
34+
35+
static _wob_log_importance min_importance_to_log = _WOB_LOG_WARN;
36+
37+
static bool use_colors = false;
38+
39+
static const char *verbosity_names[] = {
40+
"DEBUG",
41+
"INFO",
42+
"WARN",
43+
"ERROR",
44+
};
45+
46+
static const char *verbosity_colors[] = {
47+
COLOR_LIGHT_CYAN,
48+
COLOR_GREEN,
49+
COLOR_YELLOW,
50+
COLOR_LIGHT_RED,
51+
};
52+
53+
void
54+
_wob_log(const _wob_log_importance importance, const char *file, const int line, const char *fmt, ...)
55+
{
56+
if (importance < min_importance_to_log) {
57+
return;
58+
}
59+
60+
struct timespec ts;
61+
if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
62+
fprintf(stderr, "clock_gettime() failed: %s\n", strerror(errno));
63+
ts.tv_sec = 0;
64+
ts.tv_nsec = 0;
65+
}
66+
67+
// formatting time via localtime() requires open syscall (to read /etc/localtime)
68+
// and that is problematic with seccomp rules in place
69+
if (use_colors) {
70+
fprintf(
71+
stderr,
72+
"%jd.%06ld %s%-5s%s %s%s:%d:%s ",
73+
(intmax_t) ts.tv_sec,
74+
ts.tv_nsec / 1000,
75+
verbosity_colors[importance],
76+
verbosity_names[importance],
77+
COLOR_RESET,
78+
COLOR_GRAY,
79+
file,
80+
line,
81+
COLOR_RESET);
82+
}
83+
else {
84+
fprintf(stderr, "%jd.%06ld %s %s:%d: ", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000, verbosity_names[importance], file, line);
85+
}
86+
87+
va_list args;
88+
va_start(args, fmt);
89+
vfprintf(stderr, fmt, args);
90+
va_end(args);
91+
fprintf(stderr, "\n");
92+
}
93+
94+
void
95+
_wob_log_set_level(const _wob_log_importance importance)
96+
{
97+
min_importance_to_log = importance;
98+
}
99+
100+
void
101+
wob_log_use_colors(const bool colors)
102+
{
103+
use_colors = colors;
104+
}
105+
106+
void
107+
wob_log_inc_verbosity(void)
108+
{
109+
if (min_importance_to_log != _WOB_LOG_DEBUG) {
110+
min_importance_to_log -= 1;
111+
wob_log_debug("Set log level to %s", verbosity_names[min_importance_to_log]);
112+
}
113+
}

0 commit comments

Comments
 (0)