aboutsummaryrefslogtreecommitdiff
path: root/src/log_file.c
blob: c9c9f12b1c8aa266c67026506836818d97fc10c8 (plain)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <fcntl.h>
#include <assert.h>

#include "log_file.h"

char *log_file = NULL;
static FILE *flog = NULL;


int log_open_file(void)
{
    if (!log_file) {
        fprintf(stderr, "%s\n", "The path to the logfile was not set.");
        return 1;
    }

    flog = fopen(log_file, "a+");
    if (!flog) {
        fprintf(stderr, "Could not open '%s' for writing: %s\n",
            log_file, strerror(errno));
        return 1;
    }

    if (setvbuf(flog, NULL, _IOLBF, BUFSIZ)) {
        log_close_file();
        return 1;
    }

    return 0;
}

void log_close_file(void)
{
    fclose(flog);
    flog = NULL;
}

void log_fmt_file(log_priority prio, const char *fmt, ...)
{
    pid_t my_pid;
    char out[LOGMSG_MAXLEN+1] = {0};
    va_list arglist;

    if (prio < log_prio)
        return;
    assert(fmt);
    va_start(arglist, fmt);
    assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 );
    va_end(arglist);

    my_pid = getpid();
    switch (prio) {
        case DEBUG:
            fprintf(flog, "[DEBUG]  [%d] %s\n", my_pid, out);
            break;
        case NOTICE:
            fprintf(flog, "[NOTICE] [%d] %s\n", my_pid, out);
            break;
        case WARNING:
            fprintf(flog, "[WARNING][%d] %s\n", my_pid, out);
            break;
        case ERROR:
            fprintf(flog, "[ERROR]  [%d] %s\n", my_pid, out);
            break;
        case CMD:
            fprintf(flog, "[CMD]    [%d] %s\n", my_pid, out);
            break;
    }
}

void log_fmtex_file(log_priority prio, const char *srcfile,
                    size_t line, const char *fmt, ...)
{
    pid_t my_pid;
    char out[LOGMSG_MAXLEN+1] = {0};
    va_list arglist;

    if (prio < log_prio)
        return;
    assert(fmt);
    va_start(arglist, fmt);
    assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 );
    va_end(arglist);

    my_pid = getpid();
    switch (prio) {
        case DEBUG:
            fprintf(flog, "[DEBUG]  [%d] %s.%zu: %s\n", my_pid, srcfile,
                line, out);
            break;
        case NOTICE:
            fprintf(flog, "[NOTICE] [%d] %s.%zu: %s\n",
                my_pid, srcfile, line, out);
            break;
        case WARNING:
            fprintf(flog, "[WARNING][%d] %s.%zu: %s\n",
                my_pid, srcfile, line, out);
            break;
        case ERROR:
            fprintf(flog, "[ERROR]  [%d] %s.%zu: %s\n",
                my_pid, srcfile, line, out);
            break;
        case CMD:
            break;
    }
}

void log_fmtexerr_file(log_priority prio, const char *srcfile,
                       size_t line, const char *fmt, ...)
{
    pid_t my_pid;
    int saved_errno = errno;
    char out[LOGMSG_MAXLEN+1] = {0};
    va_list arglist;

    if (prio < log_prio)
        return;
    assert(fmt);
    va_start(arglist, fmt);
    assert( vsnprintf(&out[0], LOGMSG_MAXLEN, fmt, arglist) >= 0 );
    va_end(arglist);

    my_pid = getpid();
    switch (prio) {
        case DEBUG:
            if (saved_errno)
                fprintf(flog, "[DEBUG]  [%d] %s.%zu: %s failed: %s\n",
                    my_pid, srcfile, line, out,
                    strerror(saved_errno));
            else
                fprintf(flog, "[DEBUG]  [%d] %s.%zu: %s failed\n",
                    my_pid, srcfile, line, out);
            break;
        case NOTICE:
            if (saved_errno)
                fprintf(flog, "[NOTICE] [%d] %s.%zu: %s failed: %s\n",
                    my_pid, srcfile,
                    line, out, strerror(saved_errno));
            else
                fprintf(flog, "[NOTICE] [%d] %s.%zu: %s failed\n",
                    my_pid, srcfile,
                    line, out);
            break;
        case WARNING:
            if (saved_errno)
                fprintf(flog, "[WARNING][%d] %s.%zu: %s failed: %s\n",
                    my_pid, srcfile,
                    line, out, strerror(saved_errno));
            else
                fprintf(flog, "[WARNING][%d] %s.%zu: %s failed\n",
                    my_pid, srcfile,
                    line, out);
            break;
        case ERROR:
            if (saved_errno)
                fprintf(flog, "[ERROR]  [%d] %s.%zu: %s failed: %s\n",
                    my_pid, srcfile,
                    line, out, strerror(saved_errno));
            else
                fprintf(flog, "[ERROR]  [%d] %s.%zu: %s failed\n",
                    my_pid, srcfile,
                    line, out);
            break;
        case CMD:
            break;
    }
}