src/server/daemon/log.c

Tue, 12 Sep 2023 18:08:11 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Sep 2023 18:08:11 +0200
changeset 511
a5a142fea2ae
parent 490
d218607f5a7e
permissions
-rw-r--r--

update uwproj

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>
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
39 #include <pthread.h>
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include "log.h"
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include "../util/strbuf.h"
32
ebba53de8b18 added solaris 10 support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 28
diff changeset
43 #include "../util/io.h"
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
44 #include "../util/atomic.h"
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
46 #include <cx/hash_map.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
47 #include <cx/linked_list.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
48 #include <cx/compare.h>
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 87
diff changeset
49
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
50 static int is_initialized = 0;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
51
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
52 static int log_file_fd;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
53 static int log_level = 0;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
55 static uint32_t log_dup_count = 0;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
56 static CxList *log_dup_list = NULL;
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
57 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
58
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
59 WSBool main_is_daemon(void);
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
60
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 /*
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 * 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
63 */
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
64 static sbuf_t *ui_buffer = NULL;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
65
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
66 /*
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
67 * access log file map
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
68 */
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
69 static CxMap *access_log_files; // map of LogFile*
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
70
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
72 static char *log_date_month[] = {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 "Jan",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 "Feb",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 "Mar",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 "Apr",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 "May",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 "Jun",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 "Jul",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 "Aug",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 "Sep",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 "Oct",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 "Nov",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 "Dec"
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
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
87 static const char *log_levels[] = {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 "warning",
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
89 "config",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
90 "security",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
91 "failure",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
92 "catastrophe",
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
93 "info",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
94 "verbose",
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
95 "debug"
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
96 };
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
97
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
98 static int can_log[] = {
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
99 1, // warning
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
100 1, // config
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
101 1, // security
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
102 1, // failure
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
103 1, // catastrophe
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
104 1, // info
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
105 0, // verbose
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
106 0 // debug
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 };
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 442
diff changeset
109 int init_logging(void) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
110 log_dup_list = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 442
diff changeset
111 return log_dup_list == NULL;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 442
diff changeset
112 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 442
diff changeset
113
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 int init_log_file(LogConfig *cfg) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 if(is_initialized) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 return 0;
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 442
diff changeset
117 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
118
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 /* open the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 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
122 if(log_file_fd == -1) {
392
0aef555055ee print error in case the log file config doesn't work
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
123 log_ereport(LOG_FAILURE, "Cannot open log file %s: %s", cfg->file, strerror(errno));
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 return -1;
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 if(!strcmp(cfg->level, "ERROR")) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
128 can_log[LOG_WARN] = 0;
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
129 can_log[LOG_INFORM] = 0;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 } else if(!strcmp(cfg->level, "WARNING")) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
131 can_log[LOG_INFORM] = 0;
141
ff311b63c3af many fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 130
diff changeset
132 } else if(!strcmp(cfg->level, "VERBOSE")) {
ff311b63c3af many fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 130
diff changeset
133 can_log[LOG_VERBOSE] = 1;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
134 } else if(!strcmp(cfg->level, "DEBUG")) {
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
135 can_log[LOG_VERBOSE] = 1;
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
136 can_log[LOG_DEBUG] = 1;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 if(cfg->log_stdout) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 // TODO
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 if(cfg->log_stderr) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 // TODO
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 is_initialized = 1;
442
05c2b62448b1 fix stdout log message duplicates
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
148
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 /* if ui_buffer is not NULL, write it to the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 if(ui_buffer) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 size_t len = ui_buffer->length;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 size_t r;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 while(len > 0) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 r = write(log_file_fd, ui_buffer->ptr, ui_buffer->length);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 len -= r;
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 sbuf_free(ui_buffer);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163
442
05c2b62448b1 fix stdout log message duplicates
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 438
diff changeset
164 void log_uninitialized_writeln(char *str, size_t len) {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 if(ui_buffer == NULL) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 ui_buffer = sbuf_new(1024);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 if(ui_buffer == NULL) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 return; /* TODO: critical error, exit */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
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
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
172 cxstring s;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 s.ptr = str;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 s.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 sbuf_append(ui_buffer, s);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 sbuf_put(ui_buffer, '\n');
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
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
180 void log_file_writeln(char *str, size_t len) {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 struct iovec io[] = {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 { str, len },
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 { "\n", 1}
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
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
186 WSBool write_to_stdout = !main_is_daemon();
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
187 if(is_initialized) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
188 writev(log_file_fd, io, 2); /* TODO: aio? */
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
189 } else {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 442
diff changeset
190 //write_to_stdout = TRUE;
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
191 log_uninitialized_writeln(str, len);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
192 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
193
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
194 if(write_to_stdout) {
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
195 writev(STDOUT_FILENO, io, 2);
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 99
diff changeset
196 }
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
197
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
198 if(log_dup_count > 0) {
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
199 char *msg = malloc(len + 1);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
200 memcpy(msg, str, len);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
201 msg[len] = '\n';
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
202
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
203 pthread_mutex_lock(&mutex);
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
204 CxIterator i = cxListIterator(log_dup_list);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
205 cx_foreach(LogDup *, dup, i) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
206 dup->write(dup->cookie, msg, len + 1);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
207 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
208 pthread_mutex_unlock(&mutex);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
209
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
210 free(msg);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
211 }
28
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
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
214 cxmutstr log_get_prefix_str(const char *level) {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 time_t t = time(NULL);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
217 cxmutstr d;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 d.ptr = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 d.length = 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 struct tm date;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 localtime_r(&t, &date);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 char *buf = malloc(64);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 int len = snprintf(
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 buf,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 64,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 "[%02d/%s/%d:%02d:%02d:%02d] %s : ",
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 date.tm_mday,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 log_date_month[date.tm_mon],
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 1900 + date.tm_year,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 date.tm_hour,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 date.tm_min,
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 date.tm_sec,
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
235 level);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 if(len > 0) {
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 d.ptr = buf;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 d.length = len;
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
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 return d;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
245 cxmutstr log_get_prefix(int level) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
246 return log_get_prefix_str(log_levels[level]);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
247 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
248
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
249 void log_add_logdup(LogDup *dup) {
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
250 pthread_mutex_lock(&mutex);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
251 cxListAdd(log_dup_list, dup);
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
252 ws_atomic_inc32(&log_dup_count);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
253 pthread_mutex_unlock(&mutex);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
254 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
255
176
f2268fcbe487 fixes linux build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
256 void log_remove_logdup(LogDup *ldup) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
257 pthread_mutex_lock(&mutex);
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
258 CxMutIterator i = cxListMutIterator(log_dup_list);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
259 WSBool finished = 0;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
260 cx_foreach(LogDup *, dup, i) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
261 if(finished) break;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
262 if(dup == ldup) {
438
22eca559aded refactore http listener creation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
263 cxIteratorFlagRemoval(i);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
264 finished = 1;
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
265 ws_atomic_dec32(&log_dup_count);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
266 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
267 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
268 pthread_mutex_unlock(&mutex);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
269 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
270
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 /*
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 * log api functions
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
276 int log_ereport(int degree, const char *format, ...) {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
277 va_list args;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
278 va_start(args, format);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
279 int ret = log_ereport_v(degree, format, args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
280 va_end(args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
281 return ret;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
282 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
283
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
284 int log_ereport_v(int degree, const char *format, va_list args) {
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
285 if(degree < 0 || degree > 7) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
286 return 0;
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 45
diff changeset
287 }
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 141
diff changeset
288 if(!can_log[degree]) {
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 }
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
292 cxmutstr lmsg;
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 lmsg.ptr = NULL;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 /* create log message prefix */
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
296 cxmutstr lpre = log_get_prefix(degree);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 /* format message */
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
299 int len = vasprintf(&lmsg.ptr, format, args);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 lmsg.length = len;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 /* create message string */
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
303 cxmutstr message = cx_strcat(2, lpre, lmsg);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 /* write message to the log file */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 log_file_writeln(message.ptr, message.length);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 /* cleanup */
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 free(lmsg.ptr);
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 77
diff changeset
310 free(lpre.ptr);
28
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 free(message.ptr);
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 return 0;
f387669912e8 added logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 }
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
315
82
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
316 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
317 const char *format, ...)
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
318 {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
319 va_list args;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
320 va_start(args, format);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
321 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
322 va_end(args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
323 return ret;
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
324 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
325
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
326 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
327 const char *format, va_list args)
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
328 {
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
329 // TODO: implement
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
330 return log_ereport(degree, format, args);
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
331 }
740cfd9dd443 added some nsapi stuff
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
332
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
333 int log_message(const char *degree, const char *format, ...) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
334 va_list args;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
335 va_start(args, format);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
336 int ret = log_message_v(degree, format, args);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
337 va_end(args);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
338 return ret;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
339 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
340
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
341 int log_message_v(const char *degree, const char *format, va_list args) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
342 cxmutstr lmsg;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
343 lmsg.ptr = NULL;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
344
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
345 cxmutstr lpre = log_get_prefix_str(degree);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
346
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
347 /* format message */
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
348 int len = vasprintf(&lmsg.ptr, format, args);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
349 lmsg.length = len;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
350
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
351 /* create message string */
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
352 cxmutstr message = cx_strcat(2, lpre, lmsg);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
353
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
354 /* write message to the log file */
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
355 log_file_writeln(message.ptr, message.length);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
356
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
357 /* cleanup */
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
358 free(lmsg.ptr);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
359 free(lpre.ptr);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
360 free(message.ptr);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
361
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
362 return 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
363 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
364
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
365
87
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
366 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
367 log_ereport(
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
368 LOG_CATASTROPHE,
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
369 "assertion failed: %s: %s:%d",
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
370 file,
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
371 func,
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
372 line);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
373 exit(-1);
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
374 }
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
375
bdec069d2239 fixed pathcheck behavior
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 82
diff changeset
376
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
377 /*
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
378 * access log
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
379 * 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
380 * by AddLog safs.
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
381 */
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
382 LogFile* get_access_log_file(cxstring file) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
383 // TODO: this looks dubious
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
384
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
385 if(!access_log_files) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
386 access_log_files = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 4);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
387 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
388
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
389 if(file.ptr == NULL || file.length == 0) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
390 return NULL;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
391 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
392
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
393 CxHashKey key = cx_hash_key_bytes((unsigned const char*)file.ptr, file.length);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
394 LogFile *log = cxMapGet(access_log_files, key);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
395 if(log != NULL) {
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
396 ws_atomic_inc32(&log->ref);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
397 return log;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
398 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
399
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
400 // the log file is not opened
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
401 // check first if we can open it
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
402 FILE *out = fopen(file.ptr, "a");
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
403 if(out == NULL) {
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
404 return NULL;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
405 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
406
77
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
407 // create LogFile object
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
408 log = calloc(1, sizeof(LogFile));
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
409 log->file = out;
f1cff81e425a some fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
410 log->ref = 1;
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
411
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
412 // add access log to the map
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 392
diff changeset
413 cxMapPut(access_log_files, key, log);
45
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
414
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
415 return log;
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
416 }
a24aa388f02f added access log
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
417

mercurial