-
Notifications
You must be signed in to change notification settings - Fork 62
/
lionPrint.c
71 lines (59 loc) · 1.47 KB
/
lionPrint.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "lionPrint.h"
#include <assert.h>
#include <stdarg.h>
int lion_verbosity_level = 0;
FILE* lion_stdout = NULL;
FILE* lion_stderr = NULL;
int lion_get_verbosity() {
return lion_verbosity_level;
}
void lion_set_verbosity(int lvl) {
assert(lvl >= 0);
lion_verbosity_level = lvl;
}
void lion_set_stdout(FILE* out) {
assert(out != NULL);
lion_stdout = out;
}
void lion_set_stderr(FILE* err) {
assert(err != NULL);
lion_stderr = err;
}
#define print(lvl,dest,fmt) \
va_list ap; \
va_start(ap,fmt); \
int written = 0; \
if(lion_verbosity_level >= lvl) \
written = vfprintf(dest,fmt,ap); \
va_end(ap); \
return written;
#define setstream(stream,defaultStream) \
static int calls = 0; \
if( !calls && !stream) \
stream = defaultStream; \
calls++; \
int lion_oprint(int lvl, char const* fmt, ...) {
setstream(lion_stdout,stdout);
assert(lvl >= 0);
print(lvl,lion_stdout,fmt);
}
int lion_eprint(int lvl, char const* fmt, ...) {
setstream(lion_stderr,stderr);
assert(lvl >= 0);
print(lvl,lion_stderr,fmt);
}
#define vprint(lvl,dest,fmt,ap) \
int written = 0; \
if(lion_verbosity_level >= lvl) \
written = vfprintf(dest,fmt,ap); \
return written;
int lion_voprint(int lvl, char const* fmt, va_list ap) {
setstream(lion_stdout,stdout);
assert(lvl >= 0);
vprint(lvl,lion_stdout,fmt,ap);
}
int lion_veprint(int lvl, char const* fmt, va_list ap) {
setstream(lion_stderr,stderr);
assert(lvl >= 0);
vprint(lvl,lion_stderr,fmt,ap);
}