]>
Commit | Line | Data |
---|---|---|
1 | /* bsd_log.cc | |
2 | ||
3 | This file is part of Cygwin. | |
4 | ||
5 | This software is a copyrighted work licensed under the terms of the | |
6 | Cygwin license. Please consult the file "CYGWIN_LICENSE" for | |
7 | details. */ | |
8 | #ifdef __OUTSIDE_CYGWIN__ | |
9 | #include "woutsup.h" | |
10 | #define _KERNEL 1 | |
11 | #define __BSD_VISIBLE 1 | |
12 | #include <stdio.h> | |
13 | #include <stdlib.h> | |
14 | #include <libgen.h> | |
15 | ||
16 | int32_t log_level = 8; /* Illegal value. Don't change! */ | |
17 | tun_bool_t log_debug = TUN_UNDEF; | |
18 | tun_bool_t log_syslog = TUN_UNDEF; | |
19 | tun_bool_t log_stderr = TUN_UNDEF; | |
20 | ||
21 | static CRITICAL_SECTION cs; | |
22 | static bool cs_inited; | |
23 | ||
24 | void | |
25 | loginit (tun_bool_t opt_stderr, tun_bool_t opt_syslog) | |
26 | { | |
27 | if (log_debug == TUN_UNDEF) | |
28 | TUNABLE_BOOL_FETCH ("kern.log.debug", &log_debug); | |
29 | if (log_debug == TUN_UNDEF) | |
30 | log_debug = TUN_FALSE; | |
31 | ||
32 | if (opt_stderr != TUN_UNDEF) | |
33 | log_stderr = opt_stderr; | |
34 | else | |
35 | TUNABLE_BOOL_FETCH ("kern.log.stderr", &log_stderr); | |
36 | if (log_stderr == TUN_UNDEF) | |
37 | log_stderr = TUN_FALSE; | |
38 | ||
39 | if (opt_syslog != TUN_UNDEF) | |
40 | log_syslog = opt_syslog; | |
41 | else | |
42 | TUNABLE_BOOL_FETCH ("kern.log.syslog", &log_syslog); | |
43 | if (log_syslog == TUN_UNDEF) | |
44 | log_syslog = TUN_FALSE; | |
45 | ||
46 | if (log_level == 8) | |
47 | TUNABLE_INT_FETCH ("kern.log.level", &log_level); | |
48 | if (log_level == 8) | |
49 | log_level = 6; | |
50 | InitializeCriticalSection (&cs); | |
51 | cs_inited = true; | |
52 | } | |
53 | ||
54 | void | |
55 | _vlog (const char *file, int line, int level, | |
56 | const char *fmt, va_list ap) | |
57 | { | |
58 | char buf[16384]; | |
59 | char *pos; | |
60 | ||
61 | if ((level == LOG_DEBUG && log_debug != TUN_TRUE) | |
62 | || (level != LOG_DEBUG && level >= log_level)) | |
63 | return; | |
64 | pos = stpcpy (buf, "cygserver: "); | |
65 | if (file && log_debug == TUN_TRUE) | |
66 | pos += snprintf (pos, 16384 - (pos - buf), "%s, line %d: ", | |
67 | basename ((char *) file), line); | |
68 | vsnprintf (pos, 16384 - (pos - buf), fmt, ap); | |
69 | if (log_syslog == TUN_TRUE && level != LOG_DEBUG) | |
70 | syslog (level, buf); | |
71 | if (log_stderr == TUN_TRUE || level == LOG_DEBUG) | |
72 | { | |
73 | if (!cs_inited) /* Only occurs in --help scenario */ | |
74 | { | |
75 | InitializeCriticalSection (&cs); | |
76 | cs_inited = true; | |
77 | } | |
78 | EnterCriticalSection (&cs); | |
79 | fputs (buf, stderr); | |
80 | fputc ('\n', stderr); | |
81 | LeaveCriticalSection (&cs); | |
82 | } | |
83 | } | |
84 | ||
85 | void | |
86 | _log (const char *file, int line, int level, const char *fmt, ...) | |
87 | { | |
88 | va_list ap; | |
89 | va_start (ap, fmt); | |
90 | _vlog (file, line, level, fmt, ap); | |
91 | } | |
92 | ||
93 | void | |
94 | _vpanic (const char *file, int line, const char *fmt, va_list ap) | |
95 | { | |
96 | _vlog (file, line, LOG_EMERG, fmt, ap); | |
97 | exit (1); | |
98 | } | |
99 | ||
100 | void | |
101 | _panic (const char *file, int line, const char *fmt, ...) | |
102 | { | |
103 | va_list ap; | |
104 | va_start (ap, fmt); | |
105 | _vpanic (file, line, fmt, ap); | |
106 | } | |
107 | #endif /* __OUTSIDE_CYGWIN__ */ |