src/server/daemon/log.c

Sat, 31 Dec 2016 18:05:40 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 31 Dec 2016 18:05:40 +0100
changeset 144
4b546c4f25ed
parent 141
ff311b63c3af
child 166
c07122f66676
permissions
-rw-r--r--

fixes cgi fd leak

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 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 41
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
28
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"
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
43 #include "../util/atomic.h"
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 87
diff changeset
45 #include <ucx/map.h>
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 87
diff changeset
46
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
47 static int is_initialized = 0;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
48
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
49 static int log_file_fd;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
50 static int log_level = 0;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
52 WSBool main_is_daemon(void);
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
53
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 /*
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 * 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
56 */
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
57 static sbuf_t *ui_buffer = NULL;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
58
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
59 /*
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
60 * access log file map
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
61 */
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
62 static UcxMap *access_log_files; // map of LogFile*
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
63
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
65 static char *log_date_month[] = {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 "Jan",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 "Feb",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 "Mar",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 "Apr",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 "May",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 "Jun",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 "Jul",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 "Aug",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 "Sep",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 "Oct",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 "Nov",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 "Dec"
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
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
80 static char *log_levels[] = {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 "warning",
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
82 "config",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
83 "security",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
84 "failure",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
85 "catastrophe",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
86 "info",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
87 "verbose"
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
88 };
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
89
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
90 static int can_log[] = {
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
91 1, // warning
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
92 1, // config
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
93 1, // security
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
94 1, // failure
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
95 1, // catastrophe
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
96 1, // info
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
97 0 // verbose
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 };
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 int init_log_file(LogConfig *cfg) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if(is_initialized) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
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 /* open the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 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
108 if(log_file_fd == -1) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 return -1;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 if(!strcmp(cfg->level, "ERROR")) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
113 can_log[LOG_WARN] = 0;
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
114 can_log[LOG_INFORM] = 0;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 } else if(!strcmp(cfg->level, "WARNING")) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
116 can_log[LOG_INFORM] = 0;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 } else if(!strcmp(cfg->level, "INFO")) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
118
141
ff311b63c3af many fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 130
diff changeset
119 } else if(!strcmp(cfg->level, "VERBOSE")) {
ff311b63c3af many fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 130
diff changeset
120 can_log[LOG_VERBOSE] = 1;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 if(cfg->log_stdout) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 // TODO
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 if(cfg->log_stderr) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 // TODO
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 is_initialized = 1;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 /* if ui_buffer is not NULL, write it to the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 if(ui_buffer) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 size_t len = ui_buffer->length;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 size_t r;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 while(len > 0) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 r = write(log_file_fd, ui_buffer->ptr, ui_buffer->length);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 len -= r;
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 sbuf_free(ui_buffer);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
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 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 }
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 void log_uninitialized_writeln(char *str, size_t len) {
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
149 printf("%.*s\n", (int)len, str);
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
150
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 if(ui_buffer == NULL) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 ui_buffer = sbuf_new(1024);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 if(ui_buffer == NULL) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 return; /* TODO: critical error, exit */
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 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 sstr_t s;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 s.ptr = str;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 s.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 sbuf_append(ui_buffer, s);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 sbuf_put(ui_buffer, '\n');
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 void log_file_writeln(char *str, size_t len) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 if(!is_initialized) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 log_uninitialized_writeln(str, len);
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
169 return;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 }
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 struct iovec io[] = {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 { str, len },
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 { "\n", 1}
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
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
177 writev(log_file_fd, io, 2); /* TODO: aio? */
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
178 if(!main_is_daemon()) {
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
179 writev(STDOUT_FILENO, io, 2);
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
180 }
28
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 sstr_t log_get_prefix(int level) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 time_t t = time(NULL);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 sstr_t d;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 d.ptr = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 d.length = 0;
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 struct tm date;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 localtime_r(&t, &date);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 char *buf = malloc(64);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 int len = snprintf(
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 buf,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 64,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 "[%02d/%s/%d:%02d:%02d:%02d] %s : ",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 date.tm_mday,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 log_date_month[date.tm_mon],
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 1900 + date.tm_year,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 date.tm_hour,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 date.tm_min,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 date.tm_sec,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 log_levels[level]);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 if(len > 0) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 d.ptr = buf;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 d.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 return d;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
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 * log api functions
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
219 int log_ereport(int degree, const char *format, ...) {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
220 va_list args;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
221 va_start(args, format);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
222 int ret = log_ereport_v(degree, format, args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
223 va_end(args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
224 return ret;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
225 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
226
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
227 int log_ereport_v(int degree, const char *format, va_list args) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
228 if(degree > 6) {
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
229 return 0;
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
230 }
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
231 if(degree > 0 && !can_log[degree]) {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 sstr_t lmsg;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 lmsg.ptr = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 /* create log message prefix */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 sstr_t lpre = log_get_prefix(degree);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 /* format message */
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
242 int len = vasprintf(&lmsg.ptr, format, args);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 lmsg.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 /* create message string */
99
b9a6af0ae41a ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 91
diff changeset
246 sstr_t message = sstrcat(2, lpre, lmsg);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 /* write message to the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 log_file_writeln(message.ptr, message.length);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 /* cleanup */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 free(lmsg.ptr);
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
253 free(lpre.ptr);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 free(message.ptr);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 }
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
258
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
259 int log_error(int degree, const char *func, Session *sn, Request *rq,
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
260 const char *format, ...)
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
261 {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
262 va_list args;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
263 va_start(args, format);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
264 int ret = log_error_v(degree, func, sn, rq, format, args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
265 va_end(args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
266 return ret;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
267 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
268
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
269 int log_error_v(int degree, const char *func, Session *sn, Request *rq,
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
270 const char *format, va_list args)
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
271 {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
272 // TODO: implement
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
273 return log_ereport(degree, format, args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
274 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
275
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
276
87
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
277 void ws_log_assert(const char *file, const char *func, int line) {
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
278 log_ereport(
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
279 LOG_CATASTROPHE,
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
280 "assertion failed: %s: %s:%d",
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
281 file,
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
282 func,
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
283 line);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
284 exit(-1);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
285 }
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
286
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
287
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
288 /*
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
289 * access log
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
290 * This source file only manages access log files. IO is performed directly
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
291 * by AddLog safs.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
292 */
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
293 LogFile* get_access_log_file(sstr_t file) {
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
294 if(!access_log_files) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
295 access_log_files = ucx_map_new(4);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
296 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
297
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
298 if(file.ptr == NULL || file.length == 0) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
299 return NULL;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
300 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
301
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
302 LogFile *log = ucx_map_sstr_get(access_log_files, file);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
303 if(log != NULL) {
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
304 ws_atomic_inc32(&log->ref);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
305 return log;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
306 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
307
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
308 // the log file is not opened
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
309 // check first if we can open it
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
310 FILE *out = fopen(file.ptr, "a");
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
311 if(out == NULL) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
312 return NULL;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
313 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
314
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
315 // create LogFile object
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
316 log = calloc(1, sizeof(LogFile));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
317 log->file = out;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
318 log->ref = 1;
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
319
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
320 // add access log to the map
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
321 ucx_map_sstr_put(access_log_files, file, log);
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
322
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
323 return log;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
324 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
325

mercurial