Cygwin: uname: add host machine tag to sysname.
[newlib-cygwin.git] / winsup / cygserver / bsd_log.cc
blob133aa46bc9f91c2f52b07fc970f78aecbb5d58fa
1 /* bsd_log.cc
3 This file is part of Cygwin.
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>
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;
21 static CRITICAL_SECTION cs;
22 static bool cs_inited;
24 void
25 loginit (tun_bool_t opt_stderr, tun_bool_t opt_syslog)
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;
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;
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;
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;
54 void
55 _vlog (const char *file, int line, int level,
56 const char *fmt, va_list ap)
58 char buf[16384];
59 char *pos;
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)
73 if (!cs_inited) /* Only occurs in --help scenario */
75 InitializeCriticalSection (&cs);
76 cs_inited = true;
78 EnterCriticalSection (&cs);
79 fputs (buf, stderr);
80 fputc ('\n', stderr);
81 LeaveCriticalSection (&cs);
85 void
86 _log (const char *file, int line, int level, const char *fmt, ...)
88 va_list ap;
89 va_start (ap, fmt);
90 _vlog (file, line, level, fmt, ap);
93 void
94 _vpanic (const char *file, int line, const char *fmt, va_list ap)
96 _vlog (file, line, LOG_EMERG, fmt, ap);
97 exit (1);
100 void
101 _panic (const char *file, int line, const char *fmt, ...)
103 va_list ap;
104 va_start (ap, fmt);
105 _vpanic (file, line, fmt, ap);
107 #endif /* __OUTSIDE_CYGWIN__ */