summaryrefslogtreecommitdiffstats
path: root/hw2/logger.c
blob: bffe50ba5bdb42ba6498d9c167204598c01062a7 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* b01902062 藍挺瑋 */
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "logger.h"
#include "xwrap.h"

#include <fcntl.h>
#include <locale.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

void comp135_init (Comp135* comp, const char* static_name, bool no_init) {
    if (!no_init) {
        setlocale (LC_ALL, "");
        tzset ();
    }

    comp->name = (char*)static_name;
    comp->pid = getpid ();
    comp->debug = false;

    const char* comp135_debug = getenv ("COMP135_DEBUG");
    const char* comp135_logfile = getenv ("COMP135_LOGFILE");
    if (comp135_debug == NULL ||
        strcmp (comp135_debug, "0")     == 0 ||
        strcmp (comp135_debug, "n")     == 0 ||
        strcmp (comp135_debug, "no")    == 0 ||
        strcmp (comp135_debug, "f")     == 0 ||
        strcmp (comp135_debug, "false") == 0) {

        return;
    }

    comp->debug = true;
    comp->log = NULL;
    comp->log_fd = STDERR_FILENO;
    comp->log_file = stderr;

    if (comp135_logfile == NULL || *comp135_logfile == '\0') {
        return;
    }

    int fd = open (
        comp135_logfile, O_WRONLY | O_CREAT | O_APPEND, S_IWUSR | S_IRUSR);
    if (fd < 0) {
        return;
    }

    FILE* fp = fdopen (fd, "wb");
    if (fp == NULL) {
        close (fd);
        return;
    }

    setvbuf (fp, NULL, _IONBF, 0);
    comp->log = xstrdup (comp135_logfile);
    comp->log_fd = fd;
    comp->log_file = fp;
}

void comp135_destroy (Comp135* comp) {
    if (!comp->debug) {
        return;
    }

    if (comp->log != NULL) {
        free (comp->log);
        fclose (comp->log_file);
    }
}

void comp135_log (Comp135* comp, const char* format, ...) {
    if (!comp->debug) {
        return;
    }

    time_t t;
    struct tm tmd;

    time (&t);
    localtime_r (&t, &tmd);

    struct flock lock_info = {
        .l_type = F_WRLCK,
        .l_whence = SEEK_END,
        .l_start = 0,
        .l_len = 0
    };
    fcntl (comp->log_fd, F_SETLKW, &lock_info);

    fprintf (comp->log_file,
        "%04d-%02d-%02d %02d:%02d:%02d %s [%d]: ",
        tmd.tm_year + 1900, tmd.tm_mon + 1, tmd.tm_mday,
        tmd.tm_hour, tmd.tm_min, tmd.tm_sec,
        comp->name, comp->pid);

    va_list ap;
    va_start (ap, format);
    vfprintf (comp->log_file, format, ap);
    va_end (ap);

    putc ('\n', comp->log_file);

    lock_info.l_type = F_UNLCK;
    fcntl (comp->log_fd, F_SETLK, &lock_info);
}