-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
47 lines (41 loc) · 854 Bytes
/
logger.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
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdbool.h>
#include "logger.h"
#include "filesys.h"
#ifndef NDEBUG
FILE *logfile = NULL;
#endif
// open logfile
void start_log(const char *filename) {
#ifndef NDEBUG
logfile = fopen(filename, "w");
assert(logfile != NULL);
#endif
}
// printf for the logger.
// called so to match the length of printf command
void pr_log(const char *fmt, ...) {
#ifndef NDEBUG
if(logfile == NULL)
logfile = stderr;
va_list argptr;
va_start(argptr, fmt);
vfprintf(logfile, fmt, argptr);
va_end(argptr);
fflush(logfile);
#endif
}
// chek if file is stdin/stdout/stderr
static bool isstream(FILE *file) {
return !isfile(file);
}
// close logfile
void close_log() {
#ifndef NDEBUG
if(!(logfile == NULL || isstream(logfile)))
fclose(logfile);
#endif
}