]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
934d84fb | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #include <config.h> | |
e6211adc | 18 | #include "openvswitch/vlog.h" |
064af421 BP |
19 | #include <assert.h> |
20 | #include <ctype.h> | |
21 | #include <errno.h> | |
9eb94563 | 22 | #include <fcntl.h> |
064af421 BP |
23 | #include <stdarg.h> |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
68cb8aaf | 26 | #include <sys/stat.h> |
064af421 BP |
27 | #include <sys/types.h> |
28 | #include <syslog.h> | |
29 | #include <time.h> | |
30 | #include <unistd.h> | |
a5fb0e29 | 31 | #include "async-append.h" |
4958e3ee | 32 | #include "coverage.h" |
064af421 | 33 | #include "dirs.h" |
3e8a2ad1 | 34 | #include "openvswitch/dynamic-string.h" |
64c96779 | 35 | #include "openvswitch/ofpbuf.h" |
81d6495f | 36 | #include "ovs-thread.h" |
064af421 | 37 | #include "sat-math.h" |
afc9f547 | 38 | #include "socket-util.h" |
8628b0b7 | 39 | #include "svec.h" |
fe089c0d AA |
40 | #include "syslog-direct.h" |
41 | #include "syslog-libc.h" | |
42 | #include "syslog-provider.h" | |
064af421 BP |
43 | #include "timeval.h" |
44 | #include "unixctl.h" | |
45 | #include "util.h" | |
46 | ||
d98e6007 | 47 | VLOG_DEFINE_THIS_MODULE(vlog); |
064af421 | 48 | |
4958e3ee BP |
49 | /* ovs_assert() logs the assertion message, so using ovs_assert() in this |
50 | * source file could cause recursion. */ | |
51 | #undef ovs_assert | |
52 | #define ovs_assert use_assert_instead_of_ovs_assert_in_this_module | |
53 | ||
064af421 | 54 | /* Name for each logging level. */ |
4b5f1d2c | 55 | static const char *const level_names[VLL_N_LEVELS] = { |
afc9f547 | 56 | #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) #NAME, |
064af421 BP |
57 | VLOG_LEVELS |
58 | #undef VLOG_LEVEL | |
59 | }; | |
60 | ||
61 | /* Syslog value for each logging level. */ | |
4b5f1d2c | 62 | static const int syslog_levels[VLL_N_LEVELS] = { |
afc9f547 | 63 | #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) SYSLOG_LEVEL, |
064af421 BP |
64 | VLOG_LEVELS |
65 | #undef VLOG_LEVEL | |
66 | }; | |
67 | ||
afc9f547 HM |
68 | /* RFC 5424 defines specific values for each syslog level. Normally LOG_* use |
69 | * the same values. Verify that in fact they're the same. If we get assertion | |
70 | * failures here then we need to define a separate rfc5424_levels[] array. */ | |
71 | #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) \ | |
72 | BUILD_ASSERT_DECL(SYSLOG_LEVEL == RFC5424); | |
73 | VLOG_LEVELS | |
74 | #undef VLOG_LEVELS | |
75 | ||
76 | /* Similarly, RFC 5424 defines the local0 facility with the value ordinarily | |
77 | * used for LOG_LOCAL0. */ | |
78 | BUILD_ASSERT_DECL(LOG_LOCAL0 == (16 << 3)); | |
79 | ||
d5460484 | 80 | /* Protects the 'pattern' in all "struct destination"s, so that a race between |
97be1538 EJ |
81 | * changing and reading the pattern does not cause an access to freed |
82 | * memory. */ | |
83 | static struct ovs_rwlock pattern_rwlock = OVS_RWLOCK_INITIALIZER; | |
84 | ||
d5460484 GS |
85 | /* Information about each destination. */ |
86 | struct destination { | |
064af421 | 87 | const char *name; /* Name. */ |
97be1538 | 88 | char *pattern OVS_GUARDED_BY(pattern_rwlock); /* Current pattern. */ |
064af421 BP |
89 | bool default_pattern; /* Whether current pattern is the default. */ |
90 | }; | |
d5460484 GS |
91 | static struct destination destinations[VLF_N_DESTINATIONS] = { |
92 | #define VLOG_DESTINATION(NAME, PATTERN) {#NAME, PATTERN, true}, | |
93 | VLOG_DESTINATIONS | |
94 | #undef VLOG_DESTINATION | |
064af421 BP |
95 | }; |
96 | ||
81d6495f | 97 | /* Sequence number for the message currently being composed. */ |
2ba4f163 | 98 | DEFINE_STATIC_PER_THREAD_DATA(unsigned int, msg_num, 0); |
81d6495f BP |
99 | |
100 | /* VLF_FILE configuration. | |
101 | * | |
102 | * All of the following is protected by 'log_file_mutex', which nests inside | |
103 | * pattern_rwlock. */ | |
867a2e3a BP |
104 | static struct ovs_mutex log_file_mutex OVS_ACQ_AFTER(pattern_rwlock) |
105 | = OVS_MUTEX_INITIALIZER; | |
3e2493e1 | 106 | static char *log_file_name OVS_GUARDED_BY(log_file_mutex) = NULL; |
97be1538 EJ |
107 | static int log_fd OVS_GUARDED_BY(log_file_mutex) = -1; |
108 | static struct async_append *log_writer OVS_GUARDED_BY(log_file_mutex); | |
888e0cf4 | 109 | static bool log_async OVS_GUARDED_BY(log_file_mutex); |
fe089c0d | 110 | static struct syslogger *syslogger = NULL; |
064af421 | 111 | |
867a2e3a BP |
112 | /* The log modules. */ |
113 | static struct ovs_list vlog_modules OVS_GUARDED_BY(log_file_mutex) | |
114 | = OVS_LIST_INITIALIZER(&vlog_modules); | |
115 | ||
afc9f547 HM |
116 | /* Syslog export configuration. */ |
117 | static int syslog_fd OVS_GUARDED_BY(pattern_rwlock) = -1; | |
118 | ||
d69d61c7 GS |
119 | /* Log facility configuration. */ |
120 | static atomic_int log_facility = ATOMIC_VAR_INIT(0); | |
121 | ||
122 | /* Facility name and its value. */ | |
123 | struct vlog_facility { | |
124 | char *name; /* Name. */ | |
125 | unsigned int value; /* Facility associated with 'name'. */ | |
126 | }; | |
127 | static struct vlog_facility vlog_facilities[] = { | |
128 | {"kern", LOG_KERN}, | |
129 | {"user", LOG_USER}, | |
130 | {"mail", LOG_MAIL}, | |
131 | {"daemon", LOG_DAEMON}, | |
132 | {"auth", LOG_AUTH}, | |
133 | {"syslog", LOG_SYSLOG}, | |
134 | {"lpr", LOG_LPR}, | |
135 | {"news", LOG_NEWS}, | |
136 | {"uucp", LOG_UUCP}, | |
137 | {"clock", LOG_CRON}, | |
138 | {"ftp", LOG_FTP}, | |
139 | {"ntp", 12<<3}, | |
140 | {"audit", 13<<3}, | |
141 | {"alert", 14<<3}, | |
142 | {"clock2", 15<<3}, | |
143 | {"local0", LOG_LOCAL0}, | |
144 | {"local1", LOG_LOCAL1}, | |
145 | {"local2", LOG_LOCAL2}, | |
146 | {"local3", LOG_LOCAL3}, | |
147 | {"local4", LOG_LOCAL4}, | |
148 | {"local5", LOG_LOCAL5}, | |
149 | {"local6", LOG_LOCAL6}, | |
150 | {"local7", LOG_LOCAL7} | |
151 | }; | |
152 | static bool vlog_facility_exists(const char* facility, int *value); | |
153 | ||
480ce8ab | 154 | static void format_log_message(const struct vlog_module *, enum vlog_level, |
afc9f547 | 155 | const char *pattern, |
064af421 | 156 | const char *message, va_list, struct ds *) |
cab50449 | 157 | OVS_PRINTF_FORMAT(4, 0); |
064af421 BP |
158 | |
159 | /* Searches the 'n_names' in 'names'. Returns the index of a match for | |
160 | * 'target', or 'n_names' if no name matches. */ | |
161 | static size_t | |
4b5f1d2c | 162 | search_name_array(const char *target, const char *const *names, size_t n_names) |
064af421 BP |
163 | { |
164 | size_t i; | |
165 | ||
166 | for (i = 0; i < n_names; i++) { | |
167 | assert(names[i]); | |
168 | if (!strcasecmp(names[i], target)) { | |
169 | break; | |
170 | } | |
171 | } | |
172 | return i; | |
173 | } | |
174 | ||
175 | /* Returns the name for logging level 'level'. */ | |
176 | const char * | |
177 | vlog_get_level_name(enum vlog_level level) | |
178 | { | |
179 | assert(level < VLL_N_LEVELS); | |
180 | return level_names[level]; | |
181 | } | |
182 | ||
183 | /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name' | |
184 | * is not the name of a logging level. */ | |
185 | enum vlog_level | |
d295e8e9 | 186 | vlog_get_level_val(const char *name) |
064af421 BP |
187 | { |
188 | return search_name_array(name, level_names, ARRAY_SIZE(level_names)); | |
189 | } | |
190 | ||
d5460484 | 191 | /* Returns the name for logging destination 'destination'. */ |
064af421 | 192 | const char * |
d5460484 | 193 | vlog_get_destination_name(enum vlog_destination destination) |
064af421 | 194 | { |
d5460484 GS |
195 | assert(destination < VLF_N_DESTINATIONS); |
196 | return destinations[destination].name; | |
064af421 BP |
197 | } |
198 | ||
d5460484 GS |
199 | /* Returns the logging destination named 'name', or VLF_N_DESTINATIONS if |
200 | * 'name' is not the name of a logging destination. */ | |
201 | enum vlog_destination | |
202 | vlog_get_destination_val(const char *name) | |
064af421 BP |
203 | { |
204 | size_t i; | |
205 | ||
d5460484 GS |
206 | for (i = 0; i < VLF_N_DESTINATIONS; i++) { |
207 | if (!strcasecmp(destinations[i].name, name)) { | |
064af421 BP |
208 | break; |
209 | } | |
210 | } | |
211 | return i; | |
212 | } | |
213 | ||
922fed06 BP |
214 | void |
215 | vlog_insert_module(struct ovs_list *vlog) | |
e6211adc | 216 | { |
867a2e3a | 217 | ovs_mutex_lock(&log_file_mutex); |
417e7e66 | 218 | ovs_list_insert(&vlog_modules, vlog); |
867a2e3a | 219 | ovs_mutex_unlock(&log_file_mutex); |
e6211adc TG |
220 | } |
221 | ||
064af421 | 222 | /* Returns the name for logging module 'module'. */ |
480ce8ab BP |
223 | const char * |
224 | vlog_get_module_name(const struct vlog_module *module) | |
064af421 | 225 | { |
480ce8ab | 226 | return module->name; |
064af421 BP |
227 | } |
228 | ||
480ce8ab BP |
229 | /* Returns the logging module named 'name', or NULL if 'name' is not the name |
230 | * of a logging module. */ | |
231 | struct vlog_module * | |
232 | vlog_module_from_name(const char *name) | |
064af421 | 233 | { |
86e504e1 | 234 | struct vlog_module *mp; |
480ce8ab | 235 | |
867a2e3a | 236 | ovs_mutex_lock(&log_file_mutex); |
86e504e1 HS |
237 | LIST_FOR_EACH (mp, list, &vlog_modules) { |
238 | if (!strcasecmp(name, mp->name)) { | |
867a2e3a | 239 | ovs_mutex_unlock(&log_file_mutex); |
86e504e1 | 240 | return mp; |
480ce8ab BP |
241 | } |
242 | } | |
867a2e3a | 243 | ovs_mutex_unlock(&log_file_mutex); |
86e504e1 | 244 | |
480ce8ab | 245 | return NULL; |
064af421 BP |
246 | } |
247 | ||
d5460484 GS |
248 | /* Returns the current logging level for the given 'module' and |
249 | * 'destination'. */ | |
064af421 | 250 | enum vlog_level |
d5460484 GS |
251 | vlog_get_level(const struct vlog_module *module, |
252 | enum vlog_destination destination) | |
064af421 | 253 | { |
d5460484 GS |
254 | assert(destination < VLF_N_DESTINATIONS); |
255 | return module->levels[destination]; | |
064af421 BP |
256 | } |
257 | ||
97be1538 EJ |
258 | static void |
259 | update_min_level(struct vlog_module *module) OVS_REQUIRES(&log_file_mutex) | |
064af421 | 260 | { |
d5460484 | 261 | enum vlog_destination destination; |
064af421 | 262 | |
c1a543a8 | 263 | module->min_level = VLL_OFF; |
d5460484 GS |
264 | for (destination = 0; destination < VLF_N_DESTINATIONS; destination++) { |
265 | if (log_fd >= 0 || destination != VLF_FILE) { | |
266 | enum vlog_level level = module->levels[destination]; | |
56cee53b | 267 | if (level > module->min_level) { |
480ce8ab BP |
268 | module->min_level = level; |
269 | } | |
064af421 BP |
270 | } |
271 | } | |
064af421 BP |
272 | } |
273 | ||
274 | static void | |
d5460484 GS |
275 | set_destination_level(enum vlog_destination destination, |
276 | struct vlog_module *module, enum vlog_level level) | |
064af421 | 277 | { |
d5460484 | 278 | assert(destination >= 0 && destination < VLF_N_DESTINATIONS); |
064af421 BP |
279 | assert(level < VLL_N_LEVELS); |
280 | ||
97be1538 | 281 | ovs_mutex_lock(&log_file_mutex); |
480ce8ab | 282 | if (!module) { |
86e504e1 HS |
283 | struct vlog_module *mp; |
284 | LIST_FOR_EACH (mp, list, &vlog_modules) { | |
d5460484 | 285 | mp->levels[destination] = level; |
86e504e1 | 286 | update_min_level(mp); |
064af421 BP |
287 | } |
288 | } else { | |
d5460484 | 289 | module->levels[destination] = level; |
064af421 BP |
290 | update_min_level(module); |
291 | } | |
97be1538 | 292 | ovs_mutex_unlock(&log_file_mutex); |
064af421 BP |
293 | } |
294 | ||
d5460484 GS |
295 | /* Sets the logging level for the given 'module' and 'destination' to 'level'. |
296 | * A null 'module' or a 'destination' of VLF_ANY_DESTINATION is treated as a | |
297 | * wildcard across all modules or destinations, respectively. */ | |
064af421 | 298 | void |
d5460484 | 299 | vlog_set_levels(struct vlog_module *module, enum vlog_destination destination, |
d295e8e9 | 300 | enum vlog_level level) |
064af421 | 301 | { |
d5460484 GS |
302 | assert(destination < VLF_N_DESTINATIONS || |
303 | destination == VLF_ANY_DESTINATION); | |
304 | if (destination == VLF_ANY_DESTINATION) { | |
305 | for (destination = 0; destination < VLF_N_DESTINATIONS; | |
306 | destination++) { | |
307 | set_destination_level(destination, module, level); | |
064af421 BP |
308 | } |
309 | } else { | |
d5460484 | 310 | set_destination_level(destination, module, level); |
064af421 BP |
311 | } |
312 | } | |
313 | ||
314 | static void | |
d5460484 | 315 | do_set_pattern(enum vlog_destination destination, const char *pattern) |
064af421 | 316 | { |
d5460484 | 317 | struct destination *f = &destinations[destination]; |
81d6495f | 318 | |
97be1538 | 319 | ovs_rwlock_wrlock(&pattern_rwlock); |
064af421 BP |
320 | if (!f->default_pattern) { |
321 | free(f->pattern); | |
322 | } else { | |
323 | f->default_pattern = false; | |
324 | } | |
325 | f->pattern = xstrdup(pattern); | |
97be1538 | 326 | ovs_rwlock_unlock(&pattern_rwlock); |
064af421 BP |
327 | } |
328 | ||
d5460484 | 329 | /* Sets the pattern for the given 'destination' to 'pattern'. */ |
064af421 | 330 | void |
d5460484 GS |
331 | vlog_set_pattern(enum vlog_destination destination, const char *pattern) |
332 | { | |
333 | assert(destination < VLF_N_DESTINATIONS || | |
334 | destination == VLF_ANY_DESTINATION); | |
335 | if (destination == VLF_ANY_DESTINATION) { | |
336 | for (destination = 0; destination < VLF_N_DESTINATIONS; | |
337 | destination++) { | |
338 | do_set_pattern(destination, pattern); | |
064af421 BP |
339 | } |
340 | } else { | |
d5460484 | 341 | do_set_pattern(destination, pattern); |
064af421 BP |
342 | } |
343 | } | |
344 | ||
064af421 BP |
345 | /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the |
346 | * default file name if 'file_name' is null. Returns 0 if successful, | |
347 | * otherwise a positive errno value. */ | |
348 | int | |
349 | vlog_set_log_file(const char *file_name) | |
350 | { | |
81d6495f | 351 | char *new_log_file_name; |
86e504e1 | 352 | struct vlog_module *mp; |
81d6495f BP |
353 | struct stat old_stat; |
354 | struct stat new_stat; | |
355 | int new_log_fd; | |
356 | bool same_file; | |
97be1538 | 357 | bool log_close; |
81d6495f BP |
358 | |
359 | /* Open new log file. */ | |
360 | new_log_file_name = (file_name | |
361 | ? xstrdup(file_name) | |
362 | : xasprintf("%s/%s.log", ovs_logdir(), program_name)); | |
03736a67 | 363 | new_log_fd = open(new_log_file_name, O_WRONLY | O_CREAT | O_APPEND, 0660); |
81d6495f BP |
364 | if (new_log_fd < 0) { |
365 | VLOG_WARN("failed to open %s for logging: %s", | |
366 | new_log_file_name, ovs_strerror(errno)); | |
367 | free(new_log_file_name); | |
368 | return errno; | |
369 | } | |
370 | ||
371 | /* If the new log file is the same one we already have open, bail out. */ | |
97be1538 | 372 | ovs_mutex_lock(&log_file_mutex); |
81d6495f BP |
373 | same_file = (log_fd >= 0 |
374 | && new_log_fd >= 0 | |
375 | && !fstat(log_fd, &old_stat) | |
376 | && !fstat(new_log_fd, &new_stat) | |
377 | && old_stat.st_dev == new_stat.st_dev | |
378 | && old_stat.st_ino == new_stat.st_ino); | |
97be1538 | 379 | ovs_mutex_unlock(&log_file_mutex); |
81d6495f BP |
380 | if (same_file) { |
381 | close(new_log_fd); | |
382 | free(new_log_file_name); | |
383 | return 0; | |
384 | } | |
064af421 | 385 | |
81d6495f | 386 | /* Log closing old log file (we can't log while holding log_file_mutex). */ |
97be1538 EJ |
387 | ovs_mutex_lock(&log_file_mutex); |
388 | log_close = log_fd >= 0; | |
389 | ovs_mutex_unlock(&log_file_mutex); | |
390 | if (log_close) { | |
064af421 | 391 | VLOG_INFO("closing log file"); |
81d6495f | 392 | } |
a5fb0e29 | 393 | |
81d6495f | 394 | /* Close old log file, if any, and install new one. */ |
97be1538 | 395 | ovs_mutex_lock(&log_file_mutex); |
81d6495f BP |
396 | if (log_fd >= 0) { |
397 | free(log_file_name); | |
9eb94563 | 398 | close(log_fd); |
81d6495f | 399 | async_append_destroy(log_writer); |
064af421 BP |
400 | } |
401 | ||
81d6495f BP |
402 | log_file_name = xstrdup(new_log_file_name); |
403 | log_fd = new_log_fd; | |
888e0cf4 BP |
404 | if (log_async) { |
405 | log_writer = async_append_create(new_log_fd); | |
406 | } | |
81d6495f | 407 | |
86e504e1 HS |
408 | LIST_FOR_EACH (mp, list, &vlog_modules) { |
409 | update_min_level(mp); | |
064af421 | 410 | } |
97be1538 | 411 | ovs_mutex_unlock(&log_file_mutex); |
064af421 | 412 | |
81d6495f BP |
413 | /* Log opening new log file (we can't log while holding log_file_mutex). */ |
414 | VLOG_INFO("opened log file %s", new_log_file_name); | |
415 | free(new_log_file_name); | |
064af421 | 416 | |
81d6495f | 417 | return 0; |
064af421 BP |
418 | } |
419 | ||
420 | /* Closes and then attempts to re-open the current log file. (This is useful | |
421 | * just after log rotation, to ensure that the new log file starts being used.) | |
422 | * Returns 0 if successful, otherwise a positive errno value. */ | |
423 | int | |
424 | vlog_reopen_log_file(void) | |
425 | { | |
81d6495f | 426 | char *fn; |
68cb8aaf | 427 | |
97be1538 | 428 | ovs_mutex_lock(&log_file_mutex); |
2225c0b9 | 429 | fn = nullable_xstrdup(log_file_name); |
97be1538 | 430 | ovs_mutex_unlock(&log_file_mutex); |
68cb8aaf | 431 | |
81d6495f BP |
432 | if (fn) { |
433 | int error = vlog_set_log_file(fn); | |
434 | free(fn); | |
435 | return error; | |
436 | } else { | |
68cb8aaf BP |
437 | return 0; |
438 | } | |
064af421 BP |
439 | } |
440 | ||
de929213 | 441 | #ifndef _WIN32 |
3de44dd1 AZ |
442 | /* In case a log file exists, change its owner to new 'user' and 'group'. |
443 | * | |
444 | * This is useful for handling cases where the --log-file option is | |
445 | * specified ahead of the --user option. */ | |
446 | void | |
de929213 | 447 | vlog_change_owner_unix(uid_t user, gid_t group) |
3de44dd1 | 448 | { |
e9d6808c AZ |
449 | struct ds err = DS_EMPTY_INITIALIZER; |
450 | int error; | |
3de44dd1 | 451 | |
e9d6808c AZ |
452 | ovs_mutex_lock(&log_file_mutex); |
453 | error = log_file_name ? chown(log_file_name, user, group) : 0; | |
3de44dd1 | 454 | if (error) { |
e9d6808c AZ |
455 | /* Build the error message. We can not call VLOG_FATAL directly |
456 | * here because VLOG_FATAL() will try again to to acquire | |
457 | * 'log_file_mutex' lock, causing deadlock. | |
458 | */ | |
459 | ds_put_format(&err, "Failed to change %s ownership: %s.", | |
460 | log_file_name, ovs_strerror(errno)); | |
3de44dd1 | 461 | } |
3e2493e1 | 462 | ovs_mutex_unlock(&log_file_mutex); |
e9d6808c AZ |
463 | |
464 | if (error) { | |
465 | VLOG_FATAL("%s", ds_steal_cstr(&err)); | |
466 | } | |
3de44dd1 | 467 | } |
de929213 | 468 | #endif |
3de44dd1 | 469 | |
2a3e30b2 BP |
470 | /* Set debugging levels. Returns null if successful, otherwise an error |
471 | * message that the caller must free(). */ | |
064af421 BP |
472 | char * |
473 | vlog_set_levels_from_string(const char *s_) | |
474 | { | |
064af421 | 475 | char *s = xstrdup(s_); |
2a3e30b2 BP |
476 | char *save_ptr = NULL; |
477 | char *msg = NULL; | |
478 | char *word; | |
064af421 | 479 | |
2a3e30b2 BP |
480 | word = strtok_r(s, " ,:\t", &save_ptr); |
481 | if (word && !strcasecmp(word, "PATTERN")) { | |
d5460484 | 482 | enum vlog_destination destination; |
064af421 | 483 | |
2a3e30b2 BP |
484 | word = strtok_r(NULL, " ,:\t", &save_ptr); |
485 | if (!word) { | |
d5460484 | 486 | msg = xstrdup("missing destination"); |
2a3e30b2 | 487 | goto exit; |
064af421 BP |
488 | } |
489 | ||
d5460484 GS |
490 | destination = (!strcasecmp(word, "ANY") |
491 | ? VLF_ANY_DESTINATION | |
492 | : vlog_get_destination_val(word)); | |
493 | if (destination == VLF_N_DESTINATIONS) { | |
494 | msg = xasprintf("unknown destination \"%s\"", word); | |
2a3e30b2 BP |
495 | goto exit; |
496 | } | |
d5460484 | 497 | vlog_set_pattern(destination, save_ptr); |
d69d61c7 GS |
498 | } else if (word && !strcasecmp(word, "FACILITY")) { |
499 | int value; | |
500 | ||
501 | if (!vlog_facility_exists(save_ptr, &value)) { | |
502 | msg = xstrdup("invalid facility"); | |
503 | goto exit; | |
504 | } | |
505 | atomic_store_explicit(&log_facility, value, memory_order_relaxed); | |
2a3e30b2 BP |
506 | } else { |
507 | struct vlog_module *module = NULL; | |
508 | enum vlog_level level = VLL_N_LEVELS; | |
d5460484 | 509 | enum vlog_destination destination = VLF_N_DESTINATIONS; |
2a3e30b2 BP |
510 | |
511 | for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) { | |
512 | if (!strcasecmp(word, "ANY")) { | |
513 | continue; | |
d5460484 GS |
514 | } else if (vlog_get_destination_val(word) != VLF_N_DESTINATIONS) { |
515 | if (destination != VLF_N_DESTINATIONS) { | |
516 | msg = xstrdup("cannot specify multiple destinations"); | |
2a3e30b2 | 517 | goto exit; |
064af421 | 518 | } |
d5460484 | 519 | destination = vlog_get_destination_val(word); |
2a3e30b2 BP |
520 | } else if (vlog_get_level_val(word) != VLL_N_LEVELS) { |
521 | if (level != VLL_N_LEVELS) { | |
522 | msg = xstrdup("cannot specify multiple levels"); | |
523 | goto exit; | |
524 | } | |
525 | level = vlog_get_level_val(word); | |
526 | } else if (vlog_module_from_name(word)) { | |
527 | if (module) { | |
528 | msg = xstrdup("cannot specify multiple modules"); | |
529 | goto exit; | |
530 | } | |
531 | module = vlog_module_from_name(word); | |
532 | } else { | |
d5460484 GS |
533 | msg = xasprintf("no destination, level, or module \"%s\"", |
534 | word); | |
2a3e30b2 | 535 | goto exit; |
064af421 | 536 | } |
2a3e30b2 | 537 | } |
064af421 | 538 | |
d5460484 GS |
539 | if (destination == VLF_N_DESTINATIONS) { |
540 | destination = VLF_ANY_DESTINATION; | |
2a3e30b2 BP |
541 | } |
542 | if (level == VLL_N_LEVELS) { | |
543 | level = VLL_DBG; | |
064af421 | 544 | } |
d5460484 | 545 | vlog_set_levels(module, destination, level); |
064af421 | 546 | } |
2a3e30b2 BP |
547 | |
548 | exit: | |
064af421 | 549 | free(s); |
2a3e30b2 | 550 | return msg; |
064af421 BP |
551 | } |
552 | ||
316bd0f8 BP |
553 | /* Set debugging levels. Abort with an error message if 's' is invalid. */ |
554 | void | |
555 | vlog_set_levels_from_string_assert(const char *s) | |
556 | { | |
557 | char *error = vlog_set_levels_from_string(s); | |
558 | if (error) { | |
559 | ovs_fatal(0, "%s", error); | |
560 | } | |
561 | } | |
562 | ||
064af421 BP |
563 | /* If 'arg' is null, configure maximum verbosity. Otherwise, sets |
564 | * configuration according to 'arg' (see vlog_set_levels_from_string()). */ | |
565 | void | |
566 | vlog_set_verbosity(const char *arg) | |
567 | { | |
568 | if (arg) { | |
569 | char *msg = vlog_set_levels_from_string(arg); | |
570 | if (msg) { | |
571 | ovs_fatal(0, "processing \"%s\": %s", arg, msg); | |
572 | } | |
573 | } else { | |
d5460484 | 574 | vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_DBG); |
064af421 BP |
575 | } |
576 | } | |
577 | ||
fe089c0d AA |
578 | void |
579 | vlog_set_syslog_method(const char *method) | |
580 | { | |
581 | if (syslogger) { | |
582 | /* Set syslogger only, if one is not already set. This effectively | |
583 | * means that only the first --syslog-method argument is honored. */ | |
584 | return; | |
585 | } | |
586 | ||
587 | if (!strcmp(method, "libc")) { | |
588 | syslogger = syslog_libc_create(); | |
589 | } else if (!strncmp(method, "udp:", 4) || !strncmp(method, "unix:", 5)) { | |
590 | syslogger = syslog_direct_create(method); | |
591 | } else { | |
592 | ovs_fatal(0, "unsupported syslog method '%s'", method); | |
593 | } | |
594 | } | |
595 | ||
afc9f547 HM |
596 | /* Set the vlog udp syslog target. */ |
597 | void | |
598 | vlog_set_syslog_target(const char *target) | |
599 | { | |
600 | int new_fd; | |
601 | ||
1bb01121 | 602 | inet_open_active(SOCK_DGRAM, target, -1, NULL, &new_fd, 0); |
afc9f547 HM |
603 | |
604 | ovs_rwlock_wrlock(&pattern_rwlock); | |
605 | if (syslog_fd >= 0) { | |
606 | close(syslog_fd); | |
607 | } | |
608 | syslog_fd = new_fd; | |
609 | ovs_rwlock_unlock(&pattern_rwlock); | |
610 | } | |
611 | ||
d69d61c7 GS |
612 | /* Returns 'false' if 'facility' is not a valid string. If 'facility' |
613 | * is a valid string, sets 'value' with the integer value of 'facility' | |
614 | * and returns 'true'. */ | |
615 | static bool | |
616 | vlog_facility_exists(const char* facility, int *value) | |
617 | { | |
618 | size_t i; | |
619 | for (i = 0; i < ARRAY_SIZE(vlog_facilities); i++) { | |
620 | if (!strcasecmp(vlog_facilities[i].name, facility)) { | |
621 | *value = vlog_facilities[i].value; | |
622 | return true; | |
623 | } | |
624 | } | |
625 | return false; | |
626 | } | |
627 | ||
064af421 | 628 | static void |
0e15264f BP |
629 | vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[], |
630 | void *aux OVS_UNUSED) | |
064af421 | 631 | { |
0e15264f BP |
632 | int i; |
633 | ||
f446e7ce JP |
634 | /* With no argument, set all destinations and modules to "dbg". */ |
635 | if (argc == 1) { | |
636 | vlog_set_levels(NULL, VLF_ANY_DESTINATION, VLL_DBG); | |
637 | } | |
0e15264f BP |
638 | for (i = 1; i < argc; i++) { |
639 | char *msg = vlog_set_levels_from_string(argv[i]); | |
640 | if (msg) { | |
bde9f75d | 641 | unixctl_command_reply_error(conn, msg); |
0e15264f BP |
642 | free(msg); |
643 | return; | |
644 | } | |
645 | } | |
bde9f75d | 646 | unixctl_command_reply(conn, NULL); |
064af421 BP |
647 | } |
648 | ||
649 | static void | |
0e15264f BP |
650 | vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED, |
651 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
064af421 BP |
652 | { |
653 | char *msg = vlog_get_levels(); | |
bde9f75d | 654 | unixctl_command_reply(conn, msg); |
064af421 BP |
655 | free(msg); |
656 | } | |
657 | ||
532e1463 AA |
658 | static void |
659 | vlog_unixctl_list_pattern(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
660 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
661 | { | |
662 | char *msg; | |
663 | ||
664 | msg = vlog_get_patterns(); | |
665 | unixctl_command_reply(conn, msg); | |
666 | free(msg); | |
667 | } | |
668 | ||
064af421 | 669 | static void |
0e15264f BP |
670 | vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED, |
671 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
064af421 | 672 | { |
97be1538 EJ |
673 | bool has_log_file; |
674 | ||
675 | ovs_mutex_lock(&log_file_mutex); | |
676 | has_log_file = log_file_name != NULL; | |
677 | ovs_mutex_unlock(&log_file_mutex); | |
678 | ||
679 | if (has_log_file) { | |
064af421 BP |
680 | int error = vlog_reopen_log_file(); |
681 | if (error) { | |
10a89ef0 | 682 | unixctl_command_reply_error(conn, ovs_strerror(errno)); |
064af421 | 683 | } else { |
bde9f75d | 684 | unixctl_command_reply(conn, NULL); |
064af421 BP |
685 | } |
686 | } else { | |
bde9f75d | 687 | unixctl_command_reply_error(conn, "Logging to file not configured"); |
064af421 BP |
688 | } |
689 | } | |
690 | ||
06380128 BP |
691 | static void |
692 | vlog_unixctl_close(struct unixctl_conn *conn, int argc OVS_UNUSED, | |
693 | const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED) | |
694 | { | |
695 | ovs_mutex_lock(&log_file_mutex); | |
696 | if (log_fd >= 0) { | |
697 | close(log_fd); | |
698 | log_fd = -1; | |
699 | ||
700 | async_append_destroy(log_writer); | |
701 | log_writer = NULL; | |
702 | ||
703 | struct vlog_module *mp; | |
704 | LIST_FOR_EACH (mp, list, &vlog_modules) { | |
705 | update_min_level(mp); | |
706 | } | |
707 | } | |
708 | ovs_mutex_unlock(&log_file_mutex); | |
709 | ||
710 | unixctl_command_reply(conn, NULL); | |
711 | } | |
712 | ||
4958e3ee BP |
713 | static void |
714 | set_all_rate_limits(bool enable) | |
715 | { | |
86e504e1 | 716 | struct vlog_module *mp; |
4958e3ee | 717 | |
867a2e3a | 718 | ovs_mutex_lock(&log_file_mutex); |
86e504e1 HS |
719 | LIST_FOR_EACH (mp, list, &vlog_modules) { |
720 | mp->honor_rate_limits = enable; | |
4958e3ee | 721 | } |
867a2e3a | 722 | ovs_mutex_unlock(&log_file_mutex); |
4958e3ee BP |
723 | } |
724 | ||
725 | static void | |
726 | set_rate_limits(struct unixctl_conn *conn, int argc, | |
727 | const char *argv[], bool enable) | |
728 | { | |
729 | if (argc > 1) { | |
730 | int i; | |
731 | ||
732 | for (i = 1; i < argc; i++) { | |
733 | if (!strcasecmp(argv[i], "ANY")) { | |
734 | set_all_rate_limits(enable); | |
735 | } else { | |
736 | struct vlog_module *module = vlog_module_from_name(argv[i]); | |
737 | if (!module) { | |
738 | unixctl_command_reply_error(conn, "unknown module"); | |
739 | return; | |
740 | } | |
741 | module->honor_rate_limits = enable; | |
742 | } | |
743 | } | |
744 | } else { | |
745 | set_all_rate_limits(enable); | |
746 | } | |
747 | unixctl_command_reply(conn, NULL); | |
748 | } | |
749 | ||
750 | static void | |
751 | vlog_enable_rate_limit(struct unixctl_conn *conn, int argc, | |
752 | const char *argv[], void *aux OVS_UNUSED) | |
753 | { | |
754 | set_rate_limits(conn, argc, argv, true); | |
755 | } | |
756 | ||
757 | static void | |
758 | vlog_disable_rate_limit(struct unixctl_conn *conn, int argc, | |
759 | const char *argv[], void *aux OVS_UNUSED) | |
760 | { | |
761 | set_rate_limits(conn, argc, argv, false); | |
762 | } | |
763 | ||
81d6495f BP |
764 | /* Initializes the logging subsystem and registers its unixctl server |
765 | * commands. */ | |
766 | void | |
767 | vlog_init(void) | |
768 | { | |
c3b758f6 BP |
769 | static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER; |
770 | ||
771 | if (ovsthread_once_start(&once)) { | |
c3b758f6 | 772 | long long int now; |
d69d61c7 | 773 | int facility; |
195360dc | 774 | bool print_syslog_target_deprecation; |
c3b758f6 BP |
775 | |
776 | /* Do initialization work that needs to be done before any logging | |
777 | * occurs. We want to keep this really minimal because any attempt to | |
778 | * log anything before calling ovsthread_once_done() will deadlock. */ | |
d69d61c7 | 779 | atomic_read_explicit(&log_facility, &facility, memory_order_relaxed); |
fe089c0d AA |
780 | if (!syslogger) { |
781 | syslogger = syslog_libc_create(); | |
782 | } | |
783 | syslogger->class->openlog(syslogger, facility ? facility : LOG_DAEMON); | |
c3b758f6 BP |
784 | ovsthread_once_done(&once); |
785 | ||
786 | /* Now do anything that we want to happen only once but doesn't have to | |
787 | * finish before we start logging. */ | |
788 | ||
789 | now = time_wall_msec(); | |
790 | if (now < 0) { | |
791 | char *s = xastrftime_msec("%a, %d %b %Y %H:%M:%S", now, true); | |
792 | VLOG_ERR("current time is negative: %s (%lld)", s, now); | |
793 | free(s); | |
794 | } | |
795 | ||
796 | unixctl_command_register( | |
d5460484 | 797 | "vlog/set", "{spec | PATTERN:destination:pattern}", |
f446e7ce | 798 | 0, INT_MAX, vlog_unixctl_set, NULL); |
c3b758f6 BP |
799 | unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list, |
800 | NULL); | |
532e1463 AA |
801 | unixctl_command_register("vlog/list-pattern", "", 0, 0, |
802 | vlog_unixctl_list_pattern, NULL); | |
c3b758f6 BP |
803 | unixctl_command_register("vlog/enable-rate-limit", "[module]...", |
804 | 0, INT_MAX, vlog_enable_rate_limit, NULL); | |
805 | unixctl_command_register("vlog/disable-rate-limit", "[module]...", | |
806 | 0, INT_MAX, vlog_disable_rate_limit, NULL); | |
807 | unixctl_command_register("vlog/reopen", "", 0, 0, | |
808 | vlog_unixctl_reopen, NULL); | |
06380128 BP |
809 | unixctl_command_register("vlog/close", "", 0, 0, |
810 | vlog_unixctl_close, NULL); | |
195360dc AA |
811 | |
812 | ovs_rwlock_rdlock(&pattern_rwlock); | |
813 | print_syslog_target_deprecation = syslog_fd >= 0; | |
814 | ovs_rwlock_unlock(&pattern_rwlock); | |
815 | ||
816 | if (print_syslog_target_deprecation) { | |
817 | VLOG_WARN("--syslog-target flag is deprecated, use " | |
818 | "--syslog-method instead"); | |
819 | } | |
c3b758f6 | 820 | } |
81d6495f BP |
821 | } |
822 | ||
888e0cf4 BP |
823 | /* Enables VLF_FILE log output to be written asynchronously to disk. |
824 | * Asynchronous file writes avoid blocking the process in the case of a busy | |
825 | * disk, but on the other hand they are less robust: there is a chance that the | |
826 | * write will not make it to the log file if the process crashes soon after the | |
827 | * log call. */ | |
828 | void | |
829 | vlog_enable_async(void) | |
830 | { | |
831 | ovs_mutex_lock(&log_file_mutex); | |
832 | log_async = true; | |
833 | if (log_fd >= 0 && !log_writer) { | |
834 | log_writer = async_append_create(log_fd); | |
835 | } | |
836 | ovs_mutex_unlock(&log_file_mutex); | |
837 | } | |
838 | ||
93f55842 BP |
839 | void |
840 | vlog_disable_async(void) | |
841 | { | |
842 | ovs_mutex_lock(&log_file_mutex); | |
843 | log_async = false; | |
844 | async_append_destroy(log_writer); | |
845 | log_writer = NULL; | |
846 | ovs_mutex_unlock(&log_file_mutex); | |
847 | } | |
848 | ||
064af421 BP |
849 | /* Print the current logging level for each module. */ |
850 | char * | |
851 | vlog_get_levels(void) | |
852 | { | |
853 | struct ds s = DS_EMPTY_INITIALIZER; | |
86e504e1 | 854 | struct vlog_module *mp; |
8628b0b7 | 855 | struct svec lines = SVEC_EMPTY_INITIALIZER; |
8628b0b7 | 856 | size_t i; |
064af421 BP |
857 | |
858 | ds_put_format(&s, " console syslog file\n"); | |
859 | ds_put_format(&s, " ------- ------ ------\n"); | |
860 | ||
867a2e3a | 861 | ovs_mutex_lock(&log_file_mutex); |
86e504e1 | 862 | LIST_FOR_EACH (mp, list, &vlog_modules) { |
4958e3ee BP |
863 | struct ds line; |
864 | ||
865 | ds_init(&line); | |
866 | ds_put_format(&line, "%-16s %4s %4s %4s", | |
86e504e1 HS |
867 | vlog_get_module_name(mp), |
868 | vlog_get_level_name(vlog_get_level(mp, VLF_CONSOLE)), | |
869 | vlog_get_level_name(vlog_get_level(mp, VLF_SYSLOG)), | |
870 | vlog_get_level_name(vlog_get_level(mp, VLF_FILE))); | |
871 | if (!mp->honor_rate_limits) { | |
4958e3ee BP |
872 | ds_put_cstr(&line, " (rate limiting disabled)"); |
873 | } | |
874 | ds_put_char(&line, '\n'); | |
875 | ||
876 | svec_add_nocopy(&lines, ds_steal_cstr(&line)); | |
8628b0b7 | 877 | } |
867a2e3a | 878 | ovs_mutex_unlock(&log_file_mutex); |
8628b0b7 JP |
879 | |
880 | svec_sort(&lines); | |
71f21279 BP |
881 | |
882 | char *line; | |
8628b0b7 JP |
883 | SVEC_FOR_EACH (i, line, &lines) { |
884 | ds_put_cstr(&s, line); | |
064af421 | 885 | } |
8628b0b7 | 886 | svec_destroy(&lines); |
064af421 BP |
887 | |
888 | return ds_cstr(&s); | |
889 | } | |
890 | ||
532e1463 AA |
891 | /* Returns as a string current logging patterns for each destination. |
892 | * This string must be released by caller. */ | |
893 | char * | |
894 | vlog_get_patterns(void) | |
895 | { | |
896 | struct ds ds = DS_EMPTY_INITIALIZER; | |
897 | enum vlog_destination destination; | |
898 | ||
899 | ovs_rwlock_rdlock(&pattern_rwlock); | |
900 | ds_put_format(&ds, " prefix format\n"); | |
901 | ds_put_format(&ds, " ------ ------\n"); | |
902 | ||
903 | for (destination = 0; destination < VLF_N_DESTINATIONS; destination++) { | |
ea53e3a8 | 904 | struct destination *f = &destinations[destination]; |
532e1463 AA |
905 | const char *prefix = "none"; |
906 | ||
907 | if (destination == VLF_SYSLOG && syslogger) { | |
908 | prefix = syslog_get_prefix(syslogger); | |
909 | } | |
910 | ds_put_format(&ds, "%-7s %-32s %s\n", f->name, prefix, f->pattern); | |
911 | } | |
912 | ovs_rwlock_unlock(&pattern_rwlock); | |
913 | ||
914 | return ds_cstr(&ds); | |
915 | } | |
916 | ||
064af421 BP |
917 | /* Returns true if a log message emitted for the given 'module' and 'level' |
918 | * would cause some log output, false if that module and level are completely | |
919 | * disabled. */ | |
920 | bool | |
480ce8ab | 921 | vlog_is_enabled(const struct vlog_module *module, enum vlog_level level) |
064af421 | 922 | { |
480ce8ab | 923 | return module->min_level >= level; |
064af421 BP |
924 | } |
925 | ||
926 | static const char * | |
927 | fetch_braces(const char *p, const char *def, char *out, size_t out_size) | |
928 | { | |
929 | if (*p == '{') { | |
930 | size_t n = strcspn(p + 1, "}"); | |
931 | size_t n_copy = MIN(n, out_size - 1); | |
932 | memcpy(out, p + 1, n_copy); | |
933 | out[n_copy] = '\0'; | |
934 | p += n + 2; | |
935 | } else { | |
936 | ovs_strlcpy(out, def, out_size); | |
937 | } | |
938 | return p; | |
939 | } | |
940 | ||
941 | static void | |
480ce8ab | 942 | format_log_message(const struct vlog_module *module, enum vlog_level level, |
afc9f547 HM |
943 | const char *pattern, const char *message, |
944 | va_list args_, struct ds *s) | |
064af421 BP |
945 | { |
946 | char tmp[128]; | |
947 | va_list args; | |
948 | const char *p; | |
d69d61c7 | 949 | int facility; |
064af421 BP |
950 | |
951 | ds_clear(s); | |
afc9f547 | 952 | for (p = pattern; *p != '\0'; ) { |
bc9fb3a9 | 953 | const char *subprogram_name; |
064af421 | 954 | enum { LEFT, RIGHT } justify = RIGHT; |
c9bc8c11 | 955 | int pad = ' '; |
064af421 BP |
956 | size_t length, field, used; |
957 | ||
958 | if (*p != '%') { | |
959 | ds_put_char(s, *p++); | |
960 | continue; | |
961 | } | |
962 | ||
963 | p++; | |
964 | if (*p == '-') { | |
965 | justify = LEFT; | |
966 | p++; | |
967 | } | |
968 | if (*p == '0') { | |
969 | pad = '0'; | |
970 | p++; | |
971 | } | |
972 | field = 0; | |
be2c418b | 973 | while (isdigit((unsigned char)*p)) { |
064af421 BP |
974 | field = (field * 10) + (*p - '0'); |
975 | p++; | |
976 | } | |
977 | ||
978 | length = s->length; | |
979 | switch (*p++) { | |
980 | case 'A': | |
981 | ds_put_cstr(s, program_name); | |
982 | break; | |
afc9f547 | 983 | case 'B': |
d69d61c7 GS |
984 | atomic_read_explicit(&log_facility, &facility, |
985 | memory_order_relaxed); | |
986 | facility = facility ? facility : LOG_LOCAL0; | |
987 | ds_put_format(s, "%d", facility + syslog_levels[level]); | |
afc9f547 | 988 | break; |
064af421 BP |
989 | case 'c': |
990 | p = fetch_braces(p, "", tmp, sizeof tmp); | |
991 | ds_put_cstr(s, vlog_get_module_name(module)); | |
992 | break; | |
993 | case 'd': | |
2b31d8e7 PI |
994 | p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp); |
995 | ds_put_strftime_msec(s, tmp, time_wall_msec(), false); | |
b5d29991 GS |
996 | break; |
997 | case 'D': | |
2b31d8e7 PI |
998 | p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp); |
999 | ds_put_strftime_msec(s, tmp, time_wall_msec(), true); | |
064af421 | 1000 | break; |
afc9f547 HM |
1001 | case 'E': |
1002 | gethostname(tmp, sizeof tmp); | |
1003 | tmp[sizeof tmp - 1] = '\0'; | |
1004 | ds_put_cstr(s, tmp); | |
1005 | break; | |
064af421 BP |
1006 | case 'm': |
1007 | /* Format user-supplied log message and trim trailing new-lines. */ | |
1008 | length = s->length; | |
1009 | va_copy(args, args_); | |
1010 | ds_put_format_valist(s, message, args); | |
1011 | va_end(args); | |
1012 | while (s->length > length && s->string[s->length - 1] == '\n') { | |
1013 | s->length--; | |
1014 | } | |
1015 | break; | |
1016 | case 'N': | |
81d6495f | 1017 | ds_put_format(s, "%u", *msg_num_get_unsafe()); |
064af421 BP |
1018 | break; |
1019 | case 'n': | |
1020 | ds_put_char(s, '\n'); | |
1021 | break; | |
1022 | case 'p': | |
1023 | ds_put_cstr(s, vlog_get_level_name(level)); | |
1024 | break; | |
1025 | case 'P': | |
1026 | ds_put_format(s, "%ld", (long int) getpid()); | |
1027 | break; | |
1028 | case 'r': | |
4ae90ff9 | 1029 | ds_put_format(s, "%lld", time_msec() - time_boot_msec()); |
064af421 | 1030 | break; |
781dee08 | 1031 | case 't': |
bc9fb3a9 | 1032 | subprogram_name = get_subprogram_name(); |
781dee08 BP |
1033 | ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main"); |
1034 | break; | |
1035 | case 'T': | |
bc9fb3a9 | 1036 | subprogram_name = get_subprogram_name(); |
781dee08 BP |
1037 | if (subprogram_name[0]) { |
1038 | ds_put_format(s, "(%s)", subprogram_name); | |
1039 | } | |
1040 | break; | |
064af421 BP |
1041 | default: |
1042 | ds_put_char(s, p[-1]); | |
1043 | break; | |
1044 | } | |
1045 | used = s->length - length; | |
1046 | if (used < field) { | |
1047 | size_t n_pad = field - used; | |
1048 | if (justify == RIGHT) { | |
1049 | ds_put_uninit(s, n_pad); | |
1050 | memmove(&s->string[length + n_pad], &s->string[length], used); | |
1051 | memset(&s->string[length], pad, n_pad); | |
1052 | } else { | |
1053 | ds_put_char_multiple(s, pad, n_pad); | |
1054 | } | |
1055 | } | |
1056 | } | |
1057 | } | |
1058 | ||
afc9f547 HM |
1059 | /* Exports the given 'syslog_message' to the configured udp syslog sink. */ |
1060 | static void | |
1061 | send_to_syslog_fd(const char *s, size_t length) | |
1062 | OVS_REQ_RDLOCK(pattern_rwlock) | |
1063 | { | |
1064 | static size_t max_length = SIZE_MAX; | |
1065 | size_t send_len = MIN(length, max_length); | |
1066 | ||
1067 | while (write(syslog_fd, s, send_len) < 0 && errno == EMSGSIZE) { | |
1068 | send_len -= send_len / 20; | |
1069 | max_length = send_len; | |
1070 | } | |
1071 | } | |
1072 | ||
064af421 BP |
1073 | /* Writes 'message' to the log at the given 'level' and as coming from the |
1074 | * given 'module'. | |
1075 | * | |
1076 | * Guaranteed to preserve errno. */ | |
1077 | void | |
480ce8ab | 1078 | vlog_valist(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
1079 | const char *message, va_list args) |
1080 | { | |
480ce8ab BP |
1081 | bool log_to_console = module->levels[VLF_CONSOLE] >= level; |
1082 | bool log_to_syslog = module->levels[VLF_SYSLOG] >= level; | |
97be1538 EJ |
1083 | bool log_to_file; |
1084 | ||
1085 | ovs_mutex_lock(&log_file_mutex); | |
1086 | log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0; | |
1087 | ovs_mutex_unlock(&log_file_mutex); | |
064af421 BP |
1088 | if (log_to_console || log_to_syslog || log_to_file) { |
1089 | int save_errno = errno; | |
064af421 BP |
1090 | struct ds s; |
1091 | ||
1e8cf0f7 BP |
1092 | vlog_init(); |
1093 | ||
064af421 BP |
1094 | ds_init(&s); |
1095 | ds_reserve(&s, 1024); | |
81d6495f | 1096 | ++*msg_num_get(); |
064af421 | 1097 | |
97be1538 | 1098 | ovs_rwlock_rdlock(&pattern_rwlock); |
064af421 | 1099 | if (log_to_console) { |
d5460484 GS |
1100 | format_log_message(module, level, |
1101 | destinations[VLF_CONSOLE].pattern, message, | |
1102 | args, &s); | |
064af421 BP |
1103 | ds_put_char(&s, '\n'); |
1104 | fputs(ds_cstr(&s), stderr); | |
1105 | } | |
1106 | ||
1107 | if (log_to_syslog) { | |
1108 | int syslog_level = syslog_levels[level]; | |
1109 | char *save_ptr = NULL; | |
1110 | char *line; | |
d69d61c7 | 1111 | int facility; |
064af421 | 1112 | |
d5460484 | 1113 | format_log_message(module, level, destinations[VLF_SYSLOG].pattern, |
afc9f547 | 1114 | message, args, &s); |
064af421 BP |
1115 | for (line = strtok_r(s.string, "\n", &save_ptr); line; |
1116 | line = strtok_r(NULL, "\n", &save_ptr)) { | |
d69d61c7 GS |
1117 | atomic_read_explicit(&log_facility, &facility, |
1118 | memory_order_relaxed); | |
fe089c0d | 1119 | syslogger->class->syslog(syslogger, syslog_level|facility, line); |
064af421 | 1120 | } |
afc9f547 HM |
1121 | |
1122 | if (syslog_fd >= 0) { | |
1123 | format_log_message(module, level, | |
1124 | "<%B>1 %D{%Y-%m-%dT%H:%M:%S.###Z} " | |
1125 | "%E %A %P %c - \xef\xbb\xbf%m", | |
1126 | message, args, &s); | |
1127 | send_to_syslog_fd(ds_cstr(&s), s.length); | |
1128 | } | |
064af421 BP |
1129 | } |
1130 | ||
1131 | if (log_to_file) { | |
d5460484 | 1132 | format_log_message(module, level, destinations[VLF_FILE].pattern, |
afc9f547 | 1133 | message, args, &s); |
064af421 | 1134 | ds_put_char(&s, '\n'); |
81d6495f | 1135 | |
97be1538 | 1136 | ovs_mutex_lock(&log_file_mutex); |
81d6495f | 1137 | if (log_fd >= 0) { |
888e0cf4 BP |
1138 | if (log_writer) { |
1139 | async_append_write(log_writer, s.string, s.length); | |
1140 | if (level == VLL_EMER) { | |
1141 | async_append_flush(log_writer); | |
1142 | } | |
1143 | } else { | |
1144 | ignore(write(log_fd, s.string, s.length)); | |
81d6495f | 1145 | } |
a5fb0e29 | 1146 | } |
97be1538 | 1147 | ovs_mutex_unlock(&log_file_mutex); |
064af421 | 1148 | } |
97be1538 | 1149 | ovs_rwlock_unlock(&pattern_rwlock); |
064af421 BP |
1150 | |
1151 | ds_destroy(&s); | |
1152 | errno = save_errno; | |
1153 | } | |
1154 | } | |
1155 | ||
1156 | void | |
480ce8ab BP |
1157 | vlog(const struct vlog_module *module, enum vlog_level level, |
1158 | const char *message, ...) | |
064af421 BP |
1159 | { |
1160 | va_list args; | |
1161 | ||
1162 | va_start(args, message); | |
1163 | vlog_valist(module, level, message, args); | |
1164 | va_end(args); | |
1165 | } | |
1166 | ||
d41d4b71 BP |
1167 | /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure |
1168 | * exit code. Always writes the message to stderr, even if the console | |
d5460484 | 1169 | * destination is disabled. |
d41d4b71 BP |
1170 | * |
1171 | * Choose this function instead of vlog_abort_valist() if the daemon monitoring | |
1172 | * facility shouldn't automatically restart the current daemon. */ | |
279c9e03 | 1173 | void |
c1a543a8 | 1174 | vlog_fatal_valist(const struct vlog_module *module_, |
279c9e03 BP |
1175 | const char *message, va_list args) |
1176 | { | |
ebc56baa | 1177 | struct vlog_module *module = CONST_CAST(struct vlog_module *, module_); |
279c9e03 BP |
1178 | |
1179 | /* Don't log this message to the console to avoid redundancy with the | |
1180 | * message written by the later ovs_fatal_valist(). */ | |
c1a543a8 | 1181 | module->levels[VLF_CONSOLE] = VLL_OFF; |
279c9e03 | 1182 | |
c1a543a8 | 1183 | vlog_valist(module, VLL_EMER, message, args); |
279c9e03 BP |
1184 | ovs_fatal_valist(0, message, args); |
1185 | } | |
1186 | ||
d41d4b71 BP |
1187 | /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure |
1188 | * exit code. Always writes the message to stderr, even if the console | |
d5460484 | 1189 | * destination is disabled. |
d41d4b71 BP |
1190 | * |
1191 | * Choose this function instead of vlog_abort() if the daemon monitoring | |
1192 | * facility shouldn't automatically restart the current daemon. */ | |
279c9e03 | 1193 | void |
c1a543a8 | 1194 | vlog_fatal(const struct vlog_module *module, const char *message, ...) |
279c9e03 BP |
1195 | { |
1196 | va_list args; | |
1197 | ||
1198 | va_start(args, message); | |
c1a543a8 | 1199 | vlog_fatal_valist(module, message, args); |
279c9e03 BP |
1200 | va_end(args); |
1201 | } | |
1202 | ||
d41d4b71 | 1203 | /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always |
d5460484 | 1204 | * writes the message to stderr, even if the console destination is disabled. |
d41d4b71 BP |
1205 | * |
1206 | * Choose this function instead of vlog_fatal_valist() if the daemon monitoring | |
1207 | * facility should automatically restart the current daemon. */ | |
1208 | void | |
1209 | vlog_abort_valist(const struct vlog_module *module_, | |
1210 | const char *message, va_list args) | |
1211 | { | |
1212 | struct vlog_module *module = (struct vlog_module *) module_; | |
1213 | ||
1214 | /* Don't log this message to the console to avoid redundancy with the | |
1215 | * message written by the later ovs_abort_valist(). */ | |
1216 | module->levels[VLF_CONSOLE] = VLL_OFF; | |
1217 | ||
1218 | vlog_valist(module, VLL_EMER, message, args); | |
1219 | ovs_abort_valist(0, message, args); | |
1220 | } | |
1221 | ||
1222 | /* Logs 'message' to 'module' at maximum verbosity, then calls abort(). Always | |
d5460484 | 1223 | * writes the message to stderr, even if the console destination is disabled. |
d41d4b71 BP |
1224 | * |
1225 | * Choose this function instead of vlog_fatal() if the daemon monitoring | |
1226 | * facility should automatically restart the current daemon. */ | |
1227 | void | |
1228 | vlog_abort(const struct vlog_module *module, const char *message, ...) | |
1229 | { | |
1230 | va_list args; | |
1231 | ||
1232 | va_start(args, message); | |
1233 | vlog_abort_valist(module, message, args); | |
1234 | va_end(args); | |
1235 | } | |
1236 | ||
064af421 | 1237 | bool |
480ce8ab | 1238 | vlog_should_drop(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
1239 | struct vlog_rate_limit *rl) |
1240 | { | |
4958e3ee BP |
1241 | if (!module->honor_rate_limits) { |
1242 | return false; | |
1243 | } | |
1244 | ||
064af421 BP |
1245 | if (!vlog_is_enabled(module, level)) { |
1246 | return true; | |
1247 | } | |
1248 | ||
97be1538 | 1249 | ovs_mutex_lock(&rl->mutex); |
648f4f1f | 1250 | if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) { |
064af421 | 1251 | time_t now = time_now(); |
648f4f1f BP |
1252 | if (!rl->n_dropped) { |
1253 | rl->first_dropped = now; | |
064af421 | 1254 | } |
648f4f1f BP |
1255 | rl->last_dropped = now; |
1256 | rl->n_dropped++; | |
97be1538 | 1257 | ovs_mutex_unlock(&rl->mutex); |
648f4f1f | 1258 | return true; |
064af421 | 1259 | } |
064af421 | 1260 | |
fda28014 | 1261 | if (!rl->n_dropped) { |
97be1538 | 1262 | ovs_mutex_unlock(&rl->mutex); |
fda28014 | 1263 | } else { |
e2eed6a7 | 1264 | time_t now = time_now(); |
fda28014 | 1265 | unsigned int n_dropped = rl->n_dropped; |
e2eed6a7 BP |
1266 | unsigned int first_dropped_elapsed = now - rl->first_dropped; |
1267 | unsigned int last_dropped_elapsed = now - rl->last_dropped; | |
fda28014 | 1268 | rl->n_dropped = 0; |
97be1538 | 1269 | ovs_mutex_unlock(&rl->mutex); |
e2eed6a7 | 1270 | |
064af421 | 1271 | vlog(module, level, |
e2eed6a7 BP |
1272 | "Dropped %u log messages in last %u seconds (most recently, " |
1273 | "%u seconds ago) due to excessive rate", | |
fda28014 | 1274 | n_dropped, first_dropped_elapsed, last_dropped_elapsed); |
064af421 | 1275 | } |
fda28014 | 1276 | |
064af421 BP |
1277 | return false; |
1278 | } | |
1279 | ||
1280 | void | |
480ce8ab | 1281 | vlog_rate_limit(const struct vlog_module *module, enum vlog_level level, |
064af421 BP |
1282 | struct vlog_rate_limit *rl, const char *message, ...) |
1283 | { | |
1284 | if (!vlog_should_drop(module, level, rl)) { | |
1285 | va_list args; | |
1286 | ||
1287 | va_start(args, message); | |
1288 | vlog_valist(module, level, message, args); | |
1289 | va_end(args); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | void | |
d295e8e9 | 1294 | vlog_usage(void) |
064af421 | 1295 | { |
afc9f547 HM |
1296 | printf("\n\ |
1297 | Logging options:\n\ | |
1298 | -vSPEC, --verbose=SPEC set logging levels\n\ | |
1299 | -v, --verbose set maximum verbosity level\n\ | |
1300 | --log-file[=FILE] enable logging to specified FILE\n\ | |
1301 | (default: %s/%s.log)\n\ | |
fe089c0d AA |
1302 | --syslog-method=(libc|unix:file|udp:ip:port)\n\ |
1303 | specify how to send messages to syslog daemon\n\ | |
afc9f547 | 1304 | --syslog-target=HOST:PORT also send syslog msgs to HOST:PORT via UDP\n", |
b43c6fe2 | 1305 | ovs_logdir(), program_name); |
064af421 | 1306 | } |