src/server/daemon/log.c

Tue, 01 Jan 2013 13:42:58 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 01 Jan 2013 13:42:58 +0100
changeset 42
cb9c63c1edee
parent 41
bb7a1f5a8b48
child 44
3da1f7b6847f
permissions
-rw-r--r--

fixed syntax error

28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2011 Olaf Wintermann. All rights reserved.
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
41
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 32
diff changeset
29 #ifdef __gnu_linux__
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 32
diff changeset
30 #define _GNU_SOURCE
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 32
diff changeset
31 #endif
bb7a1f5a8b48 added Linux support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 32
diff changeset
32
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <stdio.h>
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <stdlib.h>
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <string.h>
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <unistd.h>
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <aio.h>
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <time.h>
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "log.h"
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "../util/strbuf.h"
32
ebba53de8b18 added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
42 #include "../util/io.h"
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 int is_initialized = 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 int log_file_fd;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 int log_level = 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 /*
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 * if the log file is uninitialized, output is written to the ui_buffer
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 sbuf_t *ui_buffer = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 char *log_date_month[] = {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 "Jan",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 "Feb",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 "Mar",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 "Apr",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 "May",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 "Jun",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 "Jul",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 "Aug",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 "Sep",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 "Oct",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 "Nov",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 "Dec"
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 };
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 char *log_levels[] = {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 "error",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 "warning",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 "info"
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 };
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 int init_log_file(LogConfig *cfg) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 if(is_initialized) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 /* open the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 log_file_fd = open(cfg->file, O_WRONLY | O_CREAT | O_APPEND, mode);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 if(log_file_fd == -1) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return -1;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 if(!strcmp(cfg->level, "ERROR")) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 log_level = LOG_LEVEL_ERROR;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 } else if(!strcmp(cfg->level, "WARNING")) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 log_level = LOG_LEVEL_WARNING;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 } else if(!strcmp(cfg->level, "INFO")) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 log_level = LOG_LEVEL_INFO;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 if(cfg->log_stdout) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 // TODO
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 if(cfg->log_stderr) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 // TODO
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 is_initialized = 1;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 /* if ui_buffer is not NULL, write it to the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 if(ui_buffer) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 size_t len = ui_buffer->length;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 size_t r;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 while(len > 0) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 r = write(log_file_fd, ui_buffer->ptr, ui_buffer->length);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 len -= r;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 sbuf_free(ui_buffer);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 void log_uninitialized_writeln(char *str, size_t len) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 if(ui_buffer == NULL) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 ui_buffer = sbuf_new(1024);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 if(ui_buffer == NULL) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 return; /* TODO: critical error, exit */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 sstr_t s;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 s.ptr = str;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 s.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 sbuf_append(ui_buffer, s);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 sbuf_put(ui_buffer, '\n');
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 void log_file_writeln(char *str, size_t len) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 if(!is_initialized) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 log_uninitialized_writeln(str, len);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 struct iovec io[] = {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 { str, len },
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 { "\n", 1}
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 };
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 writev(log_file_fd, io, 2); /* TODO: aio */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 sstr_t log_get_prefix(int level) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 time_t t = time(NULL);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 sstr_t d;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 d.ptr = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 d.length = 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 struct tm date;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 localtime_r(&t, &date);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 char *buf = malloc(64);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 int len = snprintf(
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 buf,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 64,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 "[%02d/%s/%d:%02d:%02d:%02d] %s : ",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 date.tm_mday,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 log_date_month[date.tm_mon],
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 1900 + date.tm_year,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 date.tm_hour,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 date.tm_min,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 date.tm_sec,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 log_levels[level]);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 if(len > 0) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 d.ptr = buf;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 d.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 return d;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 /*
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 * log api functions
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 int log_ereport(int degree, const char *format, ...) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 if(degree > log_level) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 sstr_t lmsg;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 lmsg.ptr = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 va_list ap;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 va_start(ap, format);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 /* create log message prefix */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 sstr_t lpre = log_get_prefix(degree);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 /* format message */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 int len = vasprintf(&lmsg.ptr, format, ap);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 lmsg.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 /* create message string */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 sstr_t message;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 message.length = lpre.length + len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 message.ptr = malloc(message.length + 1);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 message = sstrncat(2, message, lpre, lmsg);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 /* write message to the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 log_file_writeln(message.ptr, message.length);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 /* cleanup */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 free(lmsg.ptr);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 free(message.ptr);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 }

mercurial