dbf46c5ab4681f9d733327477ecd61d038aefc16
[cascardo/ovs.git] / lib / vlog.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
3  *
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:
7  *
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.
15  */
16
17 #include <config.h>
18 #include "openvswitch/vlog.h"
19 #include <assert.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <syslog.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include "async-append.h"
32 #include "coverage.h"
33 #include "dirs.h"
34 #include "dynamic-string.h"
35 #include "ofpbuf.h"
36 #include "ovs-thread.h"
37 #include "sat-math.h"
38 #include "socket-util.h"
39 #include "svec.h"
40 #include "timeval.h"
41 #include "unixctl.h"
42 #include "util.h"
43
44 VLOG_DEFINE_THIS_MODULE(vlog);
45
46 /* ovs_assert() logs the assertion message, so using ovs_assert() in this
47  * source file could cause recursion. */
48 #undef ovs_assert
49 #define ovs_assert use_assert_instead_of_ovs_assert_in_this_module
50
51 /* Name for each logging level. */
52 static const char *const level_names[VLL_N_LEVELS] = {
53 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) #NAME,
54     VLOG_LEVELS
55 #undef VLOG_LEVEL
56 };
57
58 /* Syslog value for each logging level. */
59 static const int syslog_levels[VLL_N_LEVELS] = {
60 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) SYSLOG_LEVEL,
61     VLOG_LEVELS
62 #undef VLOG_LEVEL
63 };
64
65 /* RFC 5424 defines specific values for each syslog level.  Normally LOG_* use
66  * the same values.  Verify that in fact they're the same.  If we get assertion
67  * failures here then we need to define a separate rfc5424_levels[] array. */
68 #define VLOG_LEVEL(NAME, SYSLOG_LEVEL, RFC5424) \
69     BUILD_ASSERT_DECL(SYSLOG_LEVEL == RFC5424);
70 VLOG_LEVELS
71 #undef VLOG_LEVELS
72
73 /* Similarly, RFC 5424 defines the local0 facility with the value ordinarily
74  * used for LOG_LOCAL0. */
75 BUILD_ASSERT_DECL(LOG_LOCAL0 == (16 << 3));
76
77 /* The log modules. */
78 struct ovs_list vlog_modules = OVS_LIST_INITIALIZER(&vlog_modules);
79
80 /* Protects the 'pattern' in all "struct facility"s, so that a race between
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
85 /* Information about each facility. */
86 struct facility {
87     const char *name;           /* Name. */
88     char *pattern OVS_GUARDED_BY(pattern_rwlock); /* Current pattern. */
89     bool default_pattern;       /* Whether current pattern is the default. */
90 };
91 static struct facility facilities[VLF_N_FACILITIES] = {
92 #define VLOG_FACILITY(NAME, PATTERN) {#NAME, PATTERN, true},
93     VLOG_FACILITIES
94 #undef VLOG_FACILITY
95 };
96
97 /* Sequence number for the message currently being composed. */
98 DEFINE_STATIC_PER_THREAD_DATA(unsigned int, msg_num, 0);
99
100 /* VLF_FILE configuration.
101  *
102  * All of the following is protected by 'log_file_mutex', which nests inside
103  * pattern_rwlock. */
104 static struct ovs_mutex log_file_mutex = OVS_MUTEX_INITIALIZER;
105 static char *log_file_name OVS_GUARDED_BY(log_file_mutex);
106 static int log_fd OVS_GUARDED_BY(log_file_mutex) = -1;
107 static struct async_append *log_writer OVS_GUARDED_BY(log_file_mutex);
108 static bool log_async OVS_GUARDED_BY(log_file_mutex);
109
110 /* Syslog export configuration. */
111 static int syslog_fd OVS_GUARDED_BY(pattern_rwlock) = -1;
112
113 static void format_log_message(const struct vlog_module *, enum vlog_level,
114                                const char *pattern,
115                                const char *message, va_list, struct ds *)
116     OVS_PRINTF_FORMAT(4, 0);
117
118 /* Searches the 'n_names' in 'names'.  Returns the index of a match for
119  * 'target', or 'n_names' if no name matches. */
120 static size_t
121 search_name_array(const char *target, const char *const *names, size_t n_names)
122 {
123     size_t i;
124
125     for (i = 0; i < n_names; i++) {
126         assert(names[i]);
127         if (!strcasecmp(names[i], target)) {
128             break;
129         }
130     }
131     return i;
132 }
133
134 /* Returns the name for logging level 'level'. */
135 const char *
136 vlog_get_level_name(enum vlog_level level)
137 {
138     assert(level < VLL_N_LEVELS);
139     return level_names[level];
140 }
141
142 /* Returns the logging level with the given 'name', or VLL_N_LEVELS if 'name'
143  * is not the name of a logging level. */
144 enum vlog_level
145 vlog_get_level_val(const char *name)
146 {
147     return search_name_array(name, level_names, ARRAY_SIZE(level_names));
148 }
149
150 /* Returns the name for logging facility 'facility'. */
151 const char *
152 vlog_get_facility_name(enum vlog_facility facility)
153 {
154     assert(facility < VLF_N_FACILITIES);
155     return facilities[facility].name;
156 }
157
158 /* Returns the logging facility named 'name', or VLF_N_FACILITIES if 'name' is
159  * not the name of a logging facility. */
160 enum vlog_facility
161 vlog_get_facility_val(const char *name)
162 {
163     size_t i;
164
165     for (i = 0; i < VLF_N_FACILITIES; i++) {
166         if (!strcasecmp(facilities[i].name, name)) {
167             break;
168         }
169     }
170     return i;
171 }
172
173 void vlog_insert_module(struct ovs_list *vlog)
174 {
175     list_insert(&vlog_modules, vlog);
176 }
177
178 /* Returns the name for logging module 'module'. */
179 const char *
180 vlog_get_module_name(const struct vlog_module *module)
181 {
182     return module->name;
183 }
184
185 /* Returns the logging module named 'name', or NULL if 'name' is not the name
186  * of a logging module. */
187 struct vlog_module *
188 vlog_module_from_name(const char *name)
189 {
190     struct vlog_module *mp;
191
192     LIST_FOR_EACH (mp, list, &vlog_modules) {
193         if (!strcasecmp(name, mp->name)) {
194             return mp;
195         }
196     }
197
198     return NULL;
199 }
200
201 /* Returns the current logging level for the given 'module' and 'facility'. */
202 enum vlog_level
203 vlog_get_level(const struct vlog_module *module, enum vlog_facility facility)
204 {
205     assert(facility < VLF_N_FACILITIES);
206     return module->levels[facility];
207 }
208
209 static void
210 update_min_level(struct vlog_module *module) OVS_REQUIRES(&log_file_mutex)
211 {
212     enum vlog_facility facility;
213
214     module->min_level = VLL_OFF;
215     for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
216         if (log_fd >= 0 || facility != VLF_FILE) {
217             enum vlog_level level = module->levels[facility];
218             if (level > module->min_level) {
219                 module->min_level = level;
220             }
221         }
222     }
223 }
224
225 static void
226 set_facility_level(enum vlog_facility facility, struct vlog_module *module,
227                    enum vlog_level level)
228 {
229     assert(facility >= 0 && facility < VLF_N_FACILITIES);
230     assert(level < VLL_N_LEVELS);
231
232     ovs_mutex_lock(&log_file_mutex);
233     if (!module) {
234         struct vlog_module *mp;
235         LIST_FOR_EACH (mp, list, &vlog_modules) {
236             mp->levels[facility] = level;
237             update_min_level(mp);
238         }
239     } else {
240         module->levels[facility] = level;
241         update_min_level(module);
242     }
243     ovs_mutex_unlock(&log_file_mutex);
244 }
245
246 /* Sets the logging level for the given 'module' and 'facility' to 'level'.  A
247  * null 'module' or a 'facility' of VLF_ANY_FACILITY is treated as a wildcard
248  * across all modules or facilities, respectively. */
249 void
250 vlog_set_levels(struct vlog_module *module, enum vlog_facility facility,
251                 enum vlog_level level)
252 {
253     assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
254     if (facility == VLF_ANY_FACILITY) {
255         for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
256             set_facility_level(facility, module, level);
257         }
258     } else {
259         set_facility_level(facility, module, level);
260     }
261 }
262
263 static void
264 do_set_pattern(enum vlog_facility facility, const char *pattern)
265 {
266     struct facility *f = &facilities[facility];
267
268     ovs_rwlock_wrlock(&pattern_rwlock);
269     if (!f->default_pattern) {
270         free(f->pattern);
271     } else {
272         f->default_pattern = false;
273     }
274     f->pattern = xstrdup(pattern);
275     ovs_rwlock_unlock(&pattern_rwlock);
276 }
277
278 /* Sets the pattern for the given 'facility' to 'pattern'. */
279 void
280 vlog_set_pattern(enum vlog_facility facility, const char *pattern)
281 {
282     assert(facility < VLF_N_FACILITIES || facility == VLF_ANY_FACILITY);
283     if (facility == VLF_ANY_FACILITY) {
284         for (facility = 0; facility < VLF_N_FACILITIES; facility++) {
285             do_set_pattern(facility, pattern);
286         }
287     } else {
288         do_set_pattern(facility, pattern);
289     }
290 }
291
292 /* Sets the name of the log file used by VLF_FILE to 'file_name', or to the
293  * default file name if 'file_name' is null.  Returns 0 if successful,
294  * otherwise a positive errno value. */
295 int
296 vlog_set_log_file(const char *file_name)
297 {
298     char *new_log_file_name;
299     struct vlog_module *mp;
300     struct stat old_stat;
301     struct stat new_stat;
302     int new_log_fd;
303     bool same_file;
304     bool log_close;
305
306     /* Open new log file. */
307     new_log_file_name = (file_name
308                          ? xstrdup(file_name)
309                          : xasprintf("%s/%s.log", ovs_logdir(), program_name));
310     new_log_fd = open(new_log_file_name, O_WRONLY | O_CREAT | O_APPEND, 0666);
311     if (new_log_fd < 0) {
312         VLOG_WARN("failed to open %s for logging: %s",
313                   new_log_file_name, ovs_strerror(errno));
314         free(new_log_file_name);
315         return errno;
316     }
317
318     /* If the new log file is the same one we already have open, bail out. */
319     ovs_mutex_lock(&log_file_mutex);
320     same_file = (log_fd >= 0
321                  && new_log_fd >= 0
322                  && !fstat(log_fd, &old_stat)
323                  && !fstat(new_log_fd, &new_stat)
324                  && old_stat.st_dev == new_stat.st_dev
325                  && old_stat.st_ino == new_stat.st_ino);
326     ovs_mutex_unlock(&log_file_mutex);
327     if (same_file) {
328         close(new_log_fd);
329         free(new_log_file_name);
330         return 0;
331     }
332
333     /* Log closing old log file (we can't log while holding log_file_mutex). */
334     ovs_mutex_lock(&log_file_mutex);
335     log_close = log_fd >= 0;
336     ovs_mutex_unlock(&log_file_mutex);
337     if (log_close) {
338         VLOG_INFO("closing log file");
339     }
340
341     /* Close old log file, if any, and install new one. */
342     ovs_mutex_lock(&log_file_mutex);
343     if (log_fd >= 0) {
344         free(log_file_name);
345         close(log_fd);
346         async_append_destroy(log_writer);
347     }
348
349     log_file_name = xstrdup(new_log_file_name);
350     log_fd = new_log_fd;
351     if (log_async) {
352         log_writer = async_append_create(new_log_fd);
353     }
354
355     LIST_FOR_EACH (mp, list, &vlog_modules) {
356         update_min_level(mp);
357     }
358     ovs_mutex_unlock(&log_file_mutex);
359
360     /* Log opening new log file (we can't log while holding log_file_mutex). */
361     VLOG_INFO("opened log file %s", new_log_file_name);
362     free(new_log_file_name);
363
364     return 0;
365 }
366
367 /* Closes and then attempts to re-open the current log file.  (This is useful
368  * just after log rotation, to ensure that the new log file starts being used.)
369  * Returns 0 if successful, otherwise a positive errno value. */
370 int
371 vlog_reopen_log_file(void)
372 {
373     char *fn;
374
375     ovs_mutex_lock(&log_file_mutex);
376     fn = log_file_name ? xstrdup(log_file_name) : NULL;
377     ovs_mutex_unlock(&log_file_mutex);
378
379     if (fn) {
380         int error = vlog_set_log_file(fn);
381         free(fn);
382         return error;
383     } else {
384         return 0;
385     }
386 }
387
388 /* Set debugging levels.  Returns null if successful, otherwise an error
389  * message that the caller must free(). */
390 char *
391 vlog_set_levels_from_string(const char *s_)
392 {
393     char *s = xstrdup(s_);
394     char *save_ptr = NULL;
395     char *msg = NULL;
396     char *word;
397
398     word = strtok_r(s, " ,:\t", &save_ptr);
399     if (word && !strcasecmp(word, "PATTERN")) {
400         enum vlog_facility facility;
401
402         word = strtok_r(NULL, " ,:\t", &save_ptr);
403         if (!word) {
404             msg = xstrdup("missing facility");
405             goto exit;
406         }
407
408         facility = (!strcasecmp(word, "ANY")
409                     ? VLF_ANY_FACILITY
410                     : vlog_get_facility_val(word));
411         if (facility == VLF_N_FACILITIES) {
412             msg = xasprintf("unknown facility \"%s\"", word);
413             goto exit;
414         }
415         vlog_set_pattern(facility, save_ptr);
416     } else {
417         struct vlog_module *module = NULL;
418         enum vlog_level level = VLL_N_LEVELS;
419         enum vlog_facility facility = VLF_N_FACILITIES;
420
421         for (; word != NULL; word = strtok_r(NULL, " ,:\t", &save_ptr)) {
422             if (!strcasecmp(word, "ANY")) {
423                 continue;
424             } else if (vlog_get_facility_val(word) != VLF_N_FACILITIES) {
425                 if (facility != VLF_N_FACILITIES) {
426                     msg = xstrdup("cannot specify multiple facilities");
427                     goto exit;
428                 }
429                 facility = vlog_get_facility_val(word);
430             } else if (vlog_get_level_val(word) != VLL_N_LEVELS) {
431                 if (level != VLL_N_LEVELS) {
432                     msg = xstrdup("cannot specify multiple levels");
433                     goto exit;
434                 }
435                 level = vlog_get_level_val(word);
436             } else if (vlog_module_from_name(word)) {
437                 if (module) {
438                     msg = xstrdup("cannot specify multiple modules");
439                     goto exit;
440                 }
441                 module = vlog_module_from_name(word);
442             } else {
443                 msg = xasprintf("no facility, level, or module \"%s\"", word);
444                 goto exit;
445             }
446         }
447
448         if (facility == VLF_N_FACILITIES) {
449             facility = VLF_ANY_FACILITY;
450         }
451         if (level == VLL_N_LEVELS) {
452             level = VLL_DBG;
453         }
454         vlog_set_levels(module, facility, level);
455     }
456
457 exit:
458     free(s);
459     return msg;
460 }
461
462 /* Set debugging levels.  Abort with an error message if 's' is invalid. */
463 void
464 vlog_set_levels_from_string_assert(const char *s)
465 {
466     char *error = vlog_set_levels_from_string(s);
467     if (error) {
468         ovs_fatal(0, "%s", error);
469     }
470 }
471
472 /* If 'arg' is null, configure maximum verbosity.  Otherwise, sets
473  * configuration according to 'arg' (see vlog_set_levels_from_string()). */
474 void
475 vlog_set_verbosity(const char *arg)
476 {
477     if (arg) {
478         char *msg = vlog_set_levels_from_string(arg);
479         if (msg) {
480             ovs_fatal(0, "processing \"%s\": %s", arg, msg);
481         }
482     } else {
483         vlog_set_levels(NULL, VLF_ANY_FACILITY, VLL_DBG);
484     }
485 }
486
487 /* Set the vlog udp syslog target. */
488 void
489 vlog_set_syslog_target(const char *target)
490 {
491     int new_fd;
492
493     inet_open_active(SOCK_DGRAM, target, 0, NULL, &new_fd, 0);
494
495     ovs_rwlock_wrlock(&pattern_rwlock);
496     if (syslog_fd >= 0) {
497         close(syslog_fd);
498     }
499     syslog_fd = new_fd;
500     ovs_rwlock_unlock(&pattern_rwlock);
501 }
502
503 static void
504 vlog_unixctl_set(struct unixctl_conn *conn, int argc, const char *argv[],
505                  void *aux OVS_UNUSED)
506 {
507     int i;
508
509     for (i = 1; i < argc; i++) {
510         char *msg = vlog_set_levels_from_string(argv[i]);
511         if (msg) {
512             unixctl_command_reply_error(conn, msg);
513             free(msg);
514             return;
515         }
516     }
517     unixctl_command_reply(conn, NULL);
518 }
519
520 static void
521 vlog_unixctl_list(struct unixctl_conn *conn, int argc OVS_UNUSED,
522                   const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
523 {
524     char *msg = vlog_get_levels();
525     unixctl_command_reply(conn, msg);
526     free(msg);
527 }
528
529 static void
530 vlog_unixctl_reopen(struct unixctl_conn *conn, int argc OVS_UNUSED,
531                     const char *argv[] OVS_UNUSED, void *aux OVS_UNUSED)
532 {
533     bool has_log_file;
534
535     ovs_mutex_lock(&log_file_mutex);
536     has_log_file = log_file_name != NULL;
537     ovs_mutex_unlock(&log_file_mutex);
538
539     if (has_log_file) {
540         int error = vlog_reopen_log_file();
541         if (error) {
542             unixctl_command_reply_error(conn, ovs_strerror(errno));
543         } else {
544             unixctl_command_reply(conn, NULL);
545         }
546     } else {
547         unixctl_command_reply_error(conn, "Logging to file not configured");
548     }
549 }
550
551 static void
552 set_all_rate_limits(bool enable)
553 {
554     struct vlog_module *mp;
555
556     LIST_FOR_EACH (mp, list, &vlog_modules) {
557         mp->honor_rate_limits = enable;
558     }
559 }
560
561 static void
562 set_rate_limits(struct unixctl_conn *conn, int argc,
563                 const char *argv[], bool enable)
564 {
565     if (argc > 1) {
566         int i;
567
568         for (i = 1; i < argc; i++) {
569             if (!strcasecmp(argv[i], "ANY")) {
570                 set_all_rate_limits(enable);
571             } else {
572                 struct vlog_module *module = vlog_module_from_name(argv[i]);
573                 if (!module) {
574                     unixctl_command_reply_error(conn, "unknown module");
575                     return;
576                 }
577                 module->honor_rate_limits = enable;
578             }
579         }
580     } else {
581         set_all_rate_limits(enable);
582     }
583     unixctl_command_reply(conn, NULL);
584 }
585
586 static void
587 vlog_enable_rate_limit(struct unixctl_conn *conn, int argc,
588                        const char *argv[], void *aux OVS_UNUSED)
589 {
590     set_rate_limits(conn, argc, argv, true);
591 }
592
593 static void
594 vlog_disable_rate_limit(struct unixctl_conn *conn, int argc,
595                        const char *argv[], void *aux OVS_UNUSED)
596 {
597     set_rate_limits(conn, argc, argv, false);
598 }
599
600 /* Initializes the logging subsystem and registers its unixctl server
601  * commands. */
602 void
603 vlog_init(void)
604 {
605     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
606
607     if (ovsthread_once_start(&once)) {
608         static char *program_name_copy;
609         long long int now;
610
611         /* Do initialization work that needs to be done before any logging
612          * occurs.  We want to keep this really minimal because any attempt to
613          * log anything before calling ovsthread_once_done() will deadlock. */
614
615         /* openlog() is allowed to keep the pointer passed in, without making a
616          * copy.  The daemonize code sometimes frees and replaces
617          * 'program_name', so make a private copy just for openlog().  (We keep
618          * a pointer to the private copy to suppress memory leak warnings in
619          * case openlog() does make its own copy.) */
620         program_name_copy = program_name ? xstrdup(program_name) : NULL;
621         openlog(program_name_copy, LOG_NDELAY, LOG_DAEMON);
622         ovsthread_once_done(&once);
623
624         /* Now do anything that we want to happen only once but doesn't have to
625          * finish before we start logging. */
626
627         now = time_wall_msec();
628         if (now < 0) {
629             char *s = xastrftime_msec("%a, %d %b %Y %H:%M:%S", now, true);
630             VLOG_ERR("current time is negative: %s (%lld)", s, now);
631             free(s);
632         }
633
634         unixctl_command_register(
635             "vlog/set", "{spec | PATTERN:facility:pattern}",
636             1, INT_MAX, vlog_unixctl_set, NULL);
637         unixctl_command_register("vlog/list", "", 0, 0, vlog_unixctl_list,
638                                  NULL);
639         unixctl_command_register("vlog/enable-rate-limit", "[module]...",
640                                  0, INT_MAX, vlog_enable_rate_limit, NULL);
641         unixctl_command_register("vlog/disable-rate-limit", "[module]...",
642                                  0, INT_MAX, vlog_disable_rate_limit, NULL);
643         unixctl_command_register("vlog/reopen", "", 0, 0,
644                                  vlog_unixctl_reopen, NULL);
645     }
646 }
647
648 /* Enables VLF_FILE log output to be written asynchronously to disk.
649  * Asynchronous file writes avoid blocking the process in the case of a busy
650  * disk, but on the other hand they are less robust: there is a chance that the
651  * write will not make it to the log file if the process crashes soon after the
652  * log call. */
653 void
654 vlog_enable_async(void)
655 {
656     ovs_mutex_lock(&log_file_mutex);
657     log_async = true;
658     if (log_fd >= 0 && !log_writer) {
659         log_writer = async_append_create(log_fd);
660     }
661     ovs_mutex_unlock(&log_file_mutex);
662 }
663
664 /* Print the current logging level for each module. */
665 char *
666 vlog_get_levels(void)
667 {
668     struct ds s = DS_EMPTY_INITIALIZER;
669     struct vlog_module *mp;
670     struct svec lines = SVEC_EMPTY_INITIALIZER;
671     char *line;
672     size_t i;
673
674     ds_put_format(&s, "                 console    syslog    file\n");
675     ds_put_format(&s, "                 -------    ------    ------\n");
676
677     LIST_FOR_EACH (mp, list, &vlog_modules) {
678         struct ds line;
679
680         ds_init(&line);
681         ds_put_format(&line, "%-16s  %4s       %4s       %4s",
682                       vlog_get_module_name(mp),
683                       vlog_get_level_name(vlog_get_level(mp, VLF_CONSOLE)),
684                       vlog_get_level_name(vlog_get_level(mp, VLF_SYSLOG)),
685                       vlog_get_level_name(vlog_get_level(mp, VLF_FILE)));
686         if (!mp->honor_rate_limits) {
687             ds_put_cstr(&line, "    (rate limiting disabled)");
688         }
689         ds_put_char(&line, '\n');
690
691         svec_add_nocopy(&lines, ds_steal_cstr(&line));
692     }
693
694     svec_sort(&lines);
695     SVEC_FOR_EACH (i, line, &lines) {
696         ds_put_cstr(&s, line);
697     }
698     svec_destroy(&lines);
699
700     return ds_cstr(&s);
701 }
702
703 /* Returns true if a log message emitted for the given 'module' and 'level'
704  * would cause some log output, false if that module and level are completely
705  * disabled. */
706 bool
707 vlog_is_enabled(const struct vlog_module *module, enum vlog_level level)
708 {
709     return module->min_level >= level;
710 }
711
712 static const char *
713 fetch_braces(const char *p, const char *def, char *out, size_t out_size)
714 {
715     if (*p == '{') {
716         size_t n = strcspn(p + 1, "}");
717         size_t n_copy = MIN(n, out_size - 1);
718         memcpy(out, p + 1, n_copy);
719         out[n_copy] = '\0';
720         p += n + 2;
721     } else {
722         ovs_strlcpy(out, def, out_size);
723     }
724     return p;
725 }
726
727 static void
728 format_log_message(const struct vlog_module *module, enum vlog_level level,
729                    const char *pattern, const char *message,
730                    va_list args_, struct ds *s)
731 {
732     char tmp[128];
733     va_list args;
734     const char *p;
735
736     ds_clear(s);
737     for (p = pattern; *p != '\0'; ) {
738         const char *subprogram_name;
739         enum { LEFT, RIGHT } justify = RIGHT;
740         int pad = '0';
741         size_t length, field, used;
742
743         if (*p != '%') {
744             ds_put_char(s, *p++);
745             continue;
746         }
747
748         p++;
749         if (*p == '-') {
750             justify = LEFT;
751             p++;
752         }
753         if (*p == '0') {
754             pad = '0';
755             p++;
756         }
757         field = 0;
758         while (isdigit((unsigned char)*p)) {
759             field = (field * 10) + (*p - '0');
760             p++;
761         }
762
763         length = s->length;
764         switch (*p++) {
765         case 'A':
766             ds_put_cstr(s, program_name);
767             break;
768         case 'B':
769             ds_put_format(s, "%d", LOG_LOCAL0 + syslog_levels[level]);
770             break;
771         case 'c':
772             p = fetch_braces(p, "", tmp, sizeof tmp);
773             ds_put_cstr(s, vlog_get_module_name(module));
774             break;
775         case 'd':
776             p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
777             ds_put_strftime_msec(s, tmp, time_wall_msec(), false);
778             break;
779         case 'D':
780             p = fetch_braces(p, "%Y-%m-%d %H:%M:%S.###", tmp, sizeof tmp);
781             ds_put_strftime_msec(s, tmp, time_wall_msec(), true);
782             break;
783         case 'E':
784             gethostname(tmp, sizeof tmp);
785             tmp[sizeof tmp - 1] = '\0';
786             ds_put_cstr(s, tmp);
787             break;
788         case 'm':
789             /* Format user-supplied log message and trim trailing new-lines. */
790             length = s->length;
791             va_copy(args, args_);
792             ds_put_format_valist(s, message, args);
793             va_end(args);
794             while (s->length > length && s->string[s->length - 1] == '\n') {
795                 s->length--;
796             }
797             break;
798         case 'N':
799             ds_put_format(s, "%u", *msg_num_get_unsafe());
800             break;
801         case 'n':
802             ds_put_char(s, '\n');
803             break;
804         case 'p':
805             ds_put_cstr(s, vlog_get_level_name(level));
806             break;
807         case 'P':
808             ds_put_format(s, "%ld", (long int) getpid());
809             break;
810         case 'r':
811             ds_put_format(s, "%lld", time_msec() - time_boot_msec());
812             break;
813         case 't':
814             subprogram_name = get_subprogram_name();
815             ds_put_cstr(s, subprogram_name[0] ? subprogram_name : "main");
816             break;
817         case 'T':
818             subprogram_name = get_subprogram_name();
819             if (subprogram_name[0]) {
820                 ds_put_format(s, "(%s)", subprogram_name);
821             }
822             break;
823         default:
824             ds_put_char(s, p[-1]);
825             break;
826         }
827         used = s->length - length;
828         if (used < field) {
829             size_t n_pad = field - used;
830             if (justify == RIGHT) {
831                 ds_put_uninit(s, n_pad);
832                 memmove(&s->string[length + n_pad], &s->string[length], used);
833                 memset(&s->string[length], pad, n_pad);
834             } else {
835                 ds_put_char_multiple(s, pad, n_pad);
836             }
837         }
838     }
839 }
840
841 /* Exports the given 'syslog_message' to the configured udp syslog sink. */
842 static void
843 send_to_syslog_fd(const char *s, size_t length)
844     OVS_REQ_RDLOCK(pattern_rwlock)
845 {
846     static size_t max_length = SIZE_MAX;
847     size_t send_len = MIN(length, max_length);
848
849     while (write(syslog_fd, s, send_len) < 0 && errno == EMSGSIZE) {
850         send_len -= send_len / 20;
851         max_length = send_len;
852     }
853 }
854
855 /* Writes 'message' to the log at the given 'level' and as coming from the
856  * given 'module'.
857  *
858  * Guaranteed to preserve errno. */
859 void
860 vlog_valist(const struct vlog_module *module, enum vlog_level level,
861             const char *message, va_list args)
862 {
863     bool log_to_console = module->levels[VLF_CONSOLE] >= level;
864     bool log_to_syslog = module->levels[VLF_SYSLOG] >= level;
865     bool log_to_file;
866
867     ovs_mutex_lock(&log_file_mutex);
868     log_to_file = module->levels[VLF_FILE] >= level && log_fd >= 0;
869     ovs_mutex_unlock(&log_file_mutex);
870     if (log_to_console || log_to_syslog || log_to_file) {
871         int save_errno = errno;
872         struct ds s;
873
874         vlog_init();
875
876         ds_init(&s);
877         ds_reserve(&s, 1024);
878         ++*msg_num_get();
879
880         ovs_rwlock_rdlock(&pattern_rwlock);
881         if (log_to_console) {
882             format_log_message(module, level, facilities[VLF_CONSOLE].pattern,
883                                message, args, &s);
884             ds_put_char(&s, '\n');
885             fputs(ds_cstr(&s), stderr);
886         }
887
888         if (log_to_syslog) {
889             int syslog_level = syslog_levels[level];
890             char *save_ptr = NULL;
891             char *line;
892
893             format_log_message(module, level, facilities[VLF_SYSLOG].pattern,
894                                message, args, &s);
895             for (line = strtok_r(s.string, "\n", &save_ptr); line;
896                  line = strtok_r(NULL, "\n", &save_ptr)) {
897                 syslog(syslog_level, "%s", line);
898             }
899
900             if (syslog_fd >= 0) {
901                 format_log_message(module, level,
902                                    "<%B>1 %D{%Y-%m-%dT%H:%M:%S.###Z} "
903                                    "%E %A %P %c - \xef\xbb\xbf%m",
904                                    message, args, &s);
905                 send_to_syslog_fd(ds_cstr(&s), s.length);
906             }
907         }
908
909         if (log_to_file) {
910             format_log_message(module, level, facilities[VLF_FILE].pattern,
911                                message, args, &s);
912             ds_put_char(&s, '\n');
913
914             ovs_mutex_lock(&log_file_mutex);
915             if (log_fd >= 0) {
916                 if (log_writer) {
917                     async_append_write(log_writer, s.string, s.length);
918                     if (level == VLL_EMER) {
919                         async_append_flush(log_writer);
920                     }
921                 } else {
922                     ignore(write(log_fd, s.string, s.length));
923                 }
924             }
925             ovs_mutex_unlock(&log_file_mutex);
926         }
927         ovs_rwlock_unlock(&pattern_rwlock);
928
929         ds_destroy(&s);
930         errno = save_errno;
931     }
932 }
933
934 void
935 vlog(const struct vlog_module *module, enum vlog_level level,
936      const char *message, ...)
937 {
938     va_list args;
939
940     va_start(args, message);
941     vlog_valist(module, level, message, args);
942     va_end(args);
943 }
944
945 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
946  * exit code.  Always writes the message to stderr, even if the console
947  * facility is disabled.
948  *
949  * Choose this function instead of vlog_abort_valist() if the daemon monitoring
950  * facility shouldn't automatically restart the current daemon.  */
951 void
952 vlog_fatal_valist(const struct vlog_module *module_,
953                   const char *message, va_list args)
954 {
955     struct vlog_module *module = CONST_CAST(struct vlog_module *, module_);
956
957     /* Don't log this message to the console to avoid redundancy with the
958      * message written by the later ovs_fatal_valist(). */
959     module->levels[VLF_CONSOLE] = VLL_OFF;
960
961     vlog_valist(module, VLL_EMER, message, args);
962     ovs_fatal_valist(0, message, args);
963 }
964
965 /* Logs 'message' to 'module' at maximum verbosity, then exits with a failure
966  * exit code.  Always writes the message to stderr, even if the console
967  * facility is disabled.
968  *
969  * Choose this function instead of vlog_abort() if the daemon monitoring
970  * facility shouldn't automatically restart the current daemon.  */
971 void
972 vlog_fatal(const struct vlog_module *module, const char *message, ...)
973 {
974     va_list args;
975
976     va_start(args, message);
977     vlog_fatal_valist(module, message, args);
978     va_end(args);
979 }
980
981 /* Logs 'message' to 'module' at maximum verbosity, then calls abort().  Always
982  * writes the message to stderr, even if the console facility is disabled.
983  *
984  * Choose this function instead of vlog_fatal_valist() if the daemon monitoring
985  * facility should automatically restart the current daemon.  */
986 void
987 vlog_abort_valist(const struct vlog_module *module_,
988                   const char *message, va_list args)
989 {
990     struct vlog_module *module = (struct vlog_module *) module_;
991
992     /* Don't log this message to the console to avoid redundancy with the
993      * message written by the later ovs_abort_valist(). */
994     module->levels[VLF_CONSOLE] = VLL_OFF;
995
996     vlog_valist(module, VLL_EMER, message, args);
997     ovs_abort_valist(0, message, args);
998 }
999
1000 /* Logs 'message' to 'module' at maximum verbosity, then calls abort().  Always
1001  * writes the message to stderr, even if the console facility is disabled.
1002  *
1003  * Choose this function instead of vlog_fatal() if the daemon monitoring
1004  * facility should automatically restart the current daemon.  */
1005 void
1006 vlog_abort(const struct vlog_module *module, const char *message, ...)
1007 {
1008     va_list args;
1009
1010     va_start(args, message);
1011     vlog_abort_valist(module, message, args);
1012     va_end(args);
1013 }
1014
1015 bool
1016 vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
1017                  struct vlog_rate_limit *rl)
1018 {
1019     if (!module->honor_rate_limits) {
1020         return false;
1021     }
1022
1023     if (!vlog_is_enabled(module, level)) {
1024         return true;
1025     }
1026
1027     ovs_mutex_lock(&rl->mutex);
1028     if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) {
1029         time_t now = time_now();
1030         if (!rl->n_dropped) {
1031             rl->first_dropped = now;
1032         }
1033         rl->last_dropped = now;
1034         rl->n_dropped++;
1035         ovs_mutex_unlock(&rl->mutex);
1036         return true;
1037     }
1038
1039     if (!rl->n_dropped) {
1040         ovs_mutex_unlock(&rl->mutex);
1041     } else {
1042         time_t now = time_now();
1043         unsigned int n_dropped = rl->n_dropped;
1044         unsigned int first_dropped_elapsed = now - rl->first_dropped;
1045         unsigned int last_dropped_elapsed = now - rl->last_dropped;
1046         rl->n_dropped = 0;
1047         ovs_mutex_unlock(&rl->mutex);
1048
1049         vlog(module, level,
1050              "Dropped %u log messages in last %u seconds (most recently, "
1051              "%u seconds ago) due to excessive rate",
1052              n_dropped, first_dropped_elapsed, last_dropped_elapsed);
1053     }
1054
1055     return false;
1056 }
1057
1058 void
1059 vlog_rate_limit(const struct vlog_module *module, enum vlog_level level,
1060                 struct vlog_rate_limit *rl, const char *message, ...)
1061 {
1062     if (!vlog_should_drop(module, level, rl)) {
1063         va_list args;
1064
1065         va_start(args, message);
1066         vlog_valist(module, level, message, args);
1067         va_end(args);
1068     }
1069 }
1070
1071 void
1072 vlog_usage(void)
1073 {
1074     printf("\n\
1075 Logging options:\n\
1076   -vSPEC, --verbose=SPEC   set logging levels\n\
1077   -v, --verbose            set maximum verbosity level\n\
1078   --log-file[=FILE]        enable logging to specified FILE\n\
1079                            (default: %s/%s.log)\n\
1080   --syslog-target=HOST:PORT  also send syslog msgs to HOST:PORT via UDP\n",
1081            ovs_logdir(), program_name);
1082 }