Skip to content

Commit e350d40

Browse files
authored
Merge pull request #48 from sysprog21/logging
Consolidate logging with a unified interface
2 parents 6b95d08 + fbd18ab commit e350d40

File tree

9 files changed

+286
-11
lines changed

9 files changed

+286
-11
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ libtwin.a_includes-y := \
5959
src
6060

6161
# Features
62+
libtwin.a_files-$(CONFIG_LOGGING) += src/log.c
6263
libtwin.a_files-$(CONFIG_CURSOR) += src/cursor.c
6364

6465
# Image loaders

backend/sdl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ twin_context_t *twin_sdl_init(int width, int height)
143143
}
144144

145145
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
146-
printf("error : %s\n", SDL_GetError());
146+
log_error("%s", SDL_GetError());
147147
goto bail;
148148
}
149149

@@ -154,7 +154,7 @@ twin_context_t *twin_sdl_init(int width, int height)
154154
SDL_WINDOWPOS_UNDEFINED, width, height,
155155
SDL_WINDOW_SHOWN);
156156
if (!tx->win) {
157-
printf("error : %s\n", SDL_GetError());
157+
log_error("%s", SDL_GetError());
158158
goto bail;
159159
}
160160

@@ -163,7 +163,7 @@ twin_context_t *twin_sdl_init(int width, int height)
163163

164164
tx->render = SDL_CreateRenderer(tx->win, -1, SDL_RENDERER_ACCELERATED);
165165
if (!tx->render) {
166-
printf("error : %s\n", SDL_GetError());
166+
log_error("%s", SDL_GetError());
167167
goto bail_pixels;
168168
}
169169
SDL_SetRenderDrawColor(tx->render, 255, 255, 255, 255);

configs/Kconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ endchoice
1818

1919
menu "Features"
2020

21+
config LOGGING
22+
bool "Enable logging"
23+
default y
24+
25+
config LOGGING_COLOR
26+
bool "Use colors for logging"
27+
default y
28+
depends on LOGGING
29+
30+
config LOGGING_CALLBACK
31+
bool "Enable callback functions"
32+
default y
33+
depends on LOGGING
34+
35+
comment "Logging is disabled"
36+
depends on !LOGGING
37+
2138
config CURSOR
2239
bool "Manipulate cursor"
2340
default n

include/twin_private.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
#include "twin.h"
1414

15+
/* FIXME: Both twin_private.h and log.h are private header files. They should
16+
* be moved to src/ directory.
17+
*/
18+
#include "../src/log.h"
19+
1520
/* Boilerplate for compiler compatibility */
1621
#ifndef __has_attribute
1722
#define __has_attribute(x) 0

src/image-gif.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ static twin_gif_t *gif_open(const char *fname)
8585
/* Header */
8686
read(fd, sigver, 3);
8787
if (memcmp(sigver, "GIF", 3) != 0) {
88-
fprintf(stderr, "invalid signature\n");
88+
log_error("Invalid signature");
8989
goto fail;
9090
}
9191
/* Version */
9292
read(fd, sigver, 3);
9393
if (memcmp(sigver, "89a", 3) != 0) {
94-
fprintf(stderr, "invalid version\n");
94+
log_error("Invalid version");
9595
goto fail;
9696
}
9797
/* Width x Height */
@@ -101,7 +101,7 @@ static twin_gif_t *gif_open(const char *fname)
101101
read(fd, &fdsz, 1);
102102
/* Presence of GCT */
103103
if (!(fdsz & 0x80)) {
104-
fprintf(stderr, "no global color table\n");
104+
log_error("No global color table");
105105
goto fail;
106106
}
107107
/* Color Space's Depth */
@@ -480,7 +480,7 @@ static void read_ext(twin_gif_t *gif)
480480
read_application_ext(gif);
481481
break;
482482
default:
483-
fprintf(stderr, "unknown extension: %02X\n", label);
483+
log_error("Unknown extension: %02X", label);
484484
}
485485
}
486486

@@ -617,7 +617,7 @@ twin_pixmap_t *_twin_gif_to_pixmap(const char *filepath, twin_format_t fmt)
617617
return NULL;
618618
FILE *infile = fopen(filepath, "rb");
619619
if (!infile) {
620-
fprintf(stderr, "Failed to open %s\n", filepath);
620+
log_error("Failed to open %s", filepath);
621621
return NULL;
622622
}
623623
twin_animation_t *gif = _twin_animation_from_gif_file(filepath);

src/image-jpeg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ twin_pixmap_t *_twin_jpeg_to_pixmap(const char *filepath, twin_format_t fmt)
5151

5252
FILE *infile = fopen(filepath, "rb");
5353
if (!infile) {
54-
fprintf(stderr, "Failed to open %s\n", filepath);
54+
log_error("Failed to open %s", filepath);
5555
return NULL;
5656
}
5757

@@ -61,7 +61,7 @@ twin_pixmap_t *_twin_jpeg_to_pixmap(const char *filepath, twin_format_t fmt)
6161
struct twin_jpeg_err_mgr jerr = {.mgr.error_exit = twin_jpeg_error_exit};
6262
cinfo.err = jpeg_std_error(&jerr.mgr);
6363
if (setjmp(jerr.jbuf)) {
64-
fprintf(stderr, "Failed to decode %s\n", filepath);
64+
log_error("Failed to decode %s", filepath);
6565
if (pix)
6666
twin_pixmap_destroy(pix);
6767
jpeg_destroy_decompress(&cinfo);

src/image.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ static twin_image_format_t image_type_detect(const char *path)
6666
twin_image_format_t type = IMAGE_TYPE_unknown;
6767
FILE *file = fopen(path, "rb");
6868
if (!file) {
69-
fprintf(stderr, "Failed to open %s\n", path);
69+
log_error("Failed to open %s", path);
7070
return IMAGE_TYPE_unknown;
7171
}
7272

src/log.c

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright (c) 2020 rxi
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
#include <stdio.h>
24+
25+
#include "log.h"
26+
27+
#define MAX_CALLBACKS 32
28+
29+
typedef struct {
30+
log_func_t fn;
31+
void *udata;
32+
int level;
33+
} callback_t;
34+
35+
static struct {
36+
void *udata;
37+
log_lock_func_t lock;
38+
int level;
39+
bool quiet;
40+
callback_t callbacks[MAX_CALLBACKS];
41+
} L;
42+
43+
static const char *level_strings[] = {
44+
"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL",
45+
};
46+
47+
#if defined(CONFIG_LOGGING_COLOR)
48+
static const char *level_colors[] = {
49+
"\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m",
50+
};
51+
#endif
52+
53+
static void stdout_callback(log_event_t *ev)
54+
{
55+
char buf[16];
56+
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
57+
#if defined(CONFIG_LOGGING_COLOR)
58+
fprintf(ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf,
59+
level_colors[ev->level], level_strings[ev->level], ev->file,
60+
ev->line);
61+
#else
62+
fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level],
63+
ev->file, ev->line);
64+
#endif
65+
vfprintf(ev->udata, ev->fmt, ev->ap);
66+
fprintf(ev->udata, "\n");
67+
fflush(ev->udata);
68+
}
69+
70+
static void file_callback(log_event_t *ev)
71+
{
72+
char buf[64];
73+
buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
74+
fprintf(ev->udata, "%s %-5s %s:%d: ", buf, level_strings[ev->level],
75+
ev->file, ev->line);
76+
vfprintf(ev->udata, ev->fmt, ev->ap);
77+
fprintf(ev->udata, "\n");
78+
fflush(ev->udata);
79+
}
80+
81+
static void lock(void)
82+
{
83+
if (L.lock)
84+
L.lock(true, L.udata);
85+
}
86+
87+
static void unlock(void)
88+
{
89+
if (L.lock)
90+
L.lock(false, L.udata);
91+
}
92+
93+
const char *log_level_string(int level)
94+
{
95+
return level_strings[level];
96+
}
97+
98+
void log_set_lock(log_lock_func_t fn, void *udata)
99+
{
100+
L.lock = fn;
101+
L.udata = udata;
102+
}
103+
104+
void log_set_level(int level)
105+
{
106+
L.level = level;
107+
}
108+
109+
void log_set_quiet(bool enable)
110+
{
111+
L.quiet = enable;
112+
}
113+
114+
int log_add_callback(log_func_t fn, void *udata, int level)
115+
{
116+
for (int i = 0; i < MAX_CALLBACKS; i++) {
117+
if (!L.callbacks[i].fn) {
118+
L.callbacks[i] = (callback_t){fn, udata, level};
119+
return 0;
120+
}
121+
}
122+
return -1;
123+
}
124+
125+
int log_add_fp(FILE *fp, int level)
126+
{
127+
return log_add_callback(file_callback, fp, level);
128+
}
129+
130+
static void init_event(log_event_t *ev, void *udata)
131+
{
132+
if (!ev->time) {
133+
time_t t = time(NULL);
134+
ev->time = localtime(&t);
135+
}
136+
ev->udata = udata;
137+
}
138+
139+
void log_impl(int level, const char *file, int line, const char *fmt, ...)
140+
{
141+
log_event_t ev = {
142+
.fmt = fmt,
143+
.file = file,
144+
.line = line,
145+
.level = level,
146+
};
147+
148+
lock();
149+
150+
if (!L.quiet && level >= L.level) {
151+
init_event(&ev, stderr);
152+
va_start(ev.ap, fmt);
153+
stdout_callback(&ev);
154+
va_end(ev.ap);
155+
}
156+
157+
#ifdef CONFIG_LOGGING_CALLBACK
158+
for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
159+
callback_t *cb = &L.callbacks[i];
160+
if (level >= cb->level) {
161+
init_event(&ev, cb->udata);
162+
va_start(ev.ap, fmt);
163+
cb->fn(&ev);
164+
va_end(ev.ap);
165+
}
166+
}
167+
#endif
168+
169+
unlock();
170+
}

src/log.h

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright (c) 2020 rxi
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
*/
22+
23+
#ifndef _TWIN_LOGGING_H_
24+
#define _TWIN_LOGGING_H_
25+
26+
#include <stdarg.h>
27+
#include <stdbool.h>
28+
#include <time.h>
29+
30+
typedef struct {
31+
va_list ap;
32+
const char *fmt;
33+
const char *file;
34+
struct tm *time;
35+
void *udata;
36+
int line;
37+
int level;
38+
} log_event_t;
39+
40+
typedef void (*log_func_t)(log_event_t *ev);
41+
typedef void (*log_lock_func_t)(bool lock, void *udata);
42+
43+
enum { LOGC_TRACE, LOGC_DEBUG, LOGC_INFO, LOGC_WARN, LOGC_ERROR, LOGC_FATAL };
44+
45+
#if defined(CONFIG_LOGGING)
46+
#define log_trace(...) log_impl(LOGC_TRACE, __FILE__, __LINE__, __VA_ARGS__)
47+
#define log_debug(...) log_impl(LOGC_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
48+
#define log_info(...) log_impl(LOGC_INFO, __FILE__, __LINE__, __VA_ARGS__)
49+
50+
#else /* !CONFIG_LOGGING */
51+
#define log_trace(...) \
52+
do { \
53+
} while (0)
54+
#define log_debug(...) \
55+
do { \
56+
} while (0)
57+
#define log_info(...) \
58+
do { \
59+
} while (0)
60+
#endif /* CONFIG_LOGGING */
61+
62+
#define log_warn(...) log_impl(LOGC_WARN, __FILE__, __LINE__, __VA_ARGS__)
63+
#define log_error(...) log_impl(LOGC_ERROR, __FILE__, __LINE__, __VA_ARGS__)
64+
#define log_fatal(...) log_impl(LOGC_FATAL, __FILE__, __LINE__, __VA_ARGS__)
65+
66+
const char *log_level_string(int level);
67+
void log_set_lock(log_lock_func_t fn, void *udata);
68+
void log_set_level(int level);
69+
void log_set_quiet(bool enable);
70+
int log_add_callback(log_func_t fn, void *udata, int level);
71+
int log_add_fp(FILE *fp, int level);
72+
73+
#if defined(CONFIG_LOGGING)
74+
void log_impl(int level, const char *file, int line, const char *fmt, ...);
75+
#else
76+
#define log_impl(...) \
77+
do { \
78+
/* dummy implementation */ \
79+
} while (0)
80+
#endif
81+
82+
#endif /* _TWIN_LOGGING_H_ */

0 commit comments

Comments
 (0)