summaryrefslogtreecommitdiffstats
path: root/hw2/logger.c
blob: e8c2cbff1fd47bf85e83672c388ca33c592c7cbc (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* 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 <sys/stat.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 = strrchr (static_name, '/');
    if (comp->name == NULL) {
        comp->name = (char*)static_name;
    } else {
        comp->name++;
    }
    comp->pid = getpid ();
    comp->debug = false;
    comp->is_term = 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 (isatty (comp->log_fd)) {
        comp->is_term = true;
    }

    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;
    }

    if (isatty (fd)) {
        comp->is_term = true;
    }

    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);

    char pid_color[] = "\033[1;44;37m";
    pid_color[5] = comp->pid % 6 + '1';
    fprintf (comp->log_file,
        "%s%04d-%02d-%02d %s%02d:%02d:%02d %s%s[%s%d%s]: %s",
        comp->is_term ? "\033[31m" : "",
        tmd.tm_year + 1900, tmd.tm_mon + 1, tmd.tm_mday,
        comp->is_term ? "\033[32m" : "",
        tmd.tm_hour, tmd.tm_min, tmd.tm_sec,
        comp->is_term ? "\033[33m" : "",
        comp->name,
        comp->is_term ? pid_color : "",
        comp->pid,
        comp->is_term ? "\033[0m\033[33m" : "",
        comp->is_term ? "\033[0m" : "");

    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);
}