2003-11-20 02:49:41 +08:00
|
|
|
/* bsd_log.cc
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
#ifdef __OUTSIDE_CYGWIN__
|
|
|
|
#include "woutsup.h"
|
|
|
|
#define _KERNEL 1
|
|
|
|
#define __BSD_VISIBLE 1
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-03-24 23:12:00 +08:00
|
|
|
#include <libgen.h>
|
2003-11-20 02:49:41 +08:00
|
|
|
|
2013-04-23 17:44:36 +08:00
|
|
|
int32_t log_level = 8; /* Illegal value. Don't change! */
|
2003-11-20 02:49:41 +08:00
|
|
|
tun_bool_t log_debug = TUN_UNDEF;
|
|
|
|
tun_bool_t log_syslog = TUN_UNDEF;
|
|
|
|
tun_bool_t log_stderr = TUN_UNDEF;
|
|
|
|
|
2017-03-24 23:12:00 +08:00
|
|
|
static CRITICAL_SECTION cs;
|
|
|
|
static bool cs_inited;
|
|
|
|
|
2003-11-20 02:49:41 +08:00
|
|
|
void
|
|
|
|
loginit (tun_bool_t opt_stderr, tun_bool_t opt_syslog)
|
|
|
|
{
|
|
|
|
if (log_debug == TUN_UNDEF)
|
|
|
|
TUNABLE_BOOL_FETCH ("kern.log.debug", &log_debug);
|
|
|
|
if (log_debug == TUN_UNDEF)
|
|
|
|
log_debug = TUN_FALSE;
|
|
|
|
|
|
|
|
if (opt_stderr != TUN_UNDEF)
|
|
|
|
log_stderr = opt_stderr;
|
|
|
|
else
|
|
|
|
TUNABLE_BOOL_FETCH ("kern.log.stderr", &log_stderr);
|
|
|
|
if (log_stderr == TUN_UNDEF)
|
|
|
|
log_stderr = TUN_FALSE;
|
|
|
|
|
|
|
|
if (opt_syslog != TUN_UNDEF)
|
|
|
|
log_syslog = opt_syslog;
|
|
|
|
else
|
|
|
|
TUNABLE_BOOL_FETCH ("kern.log.syslog", &log_syslog);
|
|
|
|
if (log_syslog == TUN_UNDEF)
|
|
|
|
log_syslog = TUN_FALSE;
|
|
|
|
|
|
|
|
if (log_level == 8)
|
|
|
|
TUNABLE_INT_FETCH ("kern.log.level", &log_level);
|
|
|
|
if (log_level == 8)
|
|
|
|
log_level = 6;
|
2017-03-24 23:12:00 +08:00
|
|
|
InitializeCriticalSection (&cs);
|
|
|
|
cs_inited = true;
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_vlog (const char *file, int line, int level,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
char buf[16384];
|
2008-02-07 06:01:30 +08:00
|
|
|
char *pos;
|
2003-11-20 02:49:41 +08:00
|
|
|
|
|
|
|
if ((level == LOG_DEBUG && log_debug != TUN_TRUE)
|
|
|
|
|| (level != LOG_DEBUG && level >= log_level))
|
|
|
|
return;
|
2008-02-07 06:01:30 +08:00
|
|
|
pos = stpcpy (buf, "cygserver: ");
|
2003-11-20 02:49:41 +08:00
|
|
|
if (file && log_debug == TUN_TRUE)
|
2017-03-24 23:12:52 +08:00
|
|
|
pos += snprintf (pos, 16384 - (pos - buf), "%s, line %d: ",
|
|
|
|
basename ((char *) file), line);
|
2008-02-07 06:01:30 +08:00
|
|
|
vsnprintf (pos, 16384 - (pos - buf), fmt, ap);
|
2003-11-20 02:49:41 +08:00
|
|
|
if (log_syslog == TUN_TRUE && level != LOG_DEBUG)
|
|
|
|
syslog (level, buf);
|
|
|
|
if (log_stderr == TUN_TRUE || level == LOG_DEBUG)
|
|
|
|
{
|
2017-03-24 23:12:00 +08:00
|
|
|
if (!cs_inited) /* Only occurs in --help scenario */
|
|
|
|
{
|
|
|
|
InitializeCriticalSection (&cs);
|
|
|
|
cs_inited = true;
|
|
|
|
}
|
|
|
|
EnterCriticalSection (&cs);
|
2003-11-20 02:49:41 +08:00
|
|
|
fputs (buf, stderr);
|
|
|
|
fputc ('\n', stderr);
|
2017-03-24 23:12:00 +08:00
|
|
|
LeaveCriticalSection (&cs);
|
2003-11-20 02:49:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_log (const char *file, int line, int level, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
_vlog (file, line, level, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_vpanic (const char *file, int line, const char *fmt, va_list ap)
|
|
|
|
{
|
2017-03-25 00:26:37 +08:00
|
|
|
_vlog (file, line, LOG_EMERG, fmt, ap);
|
2003-11-20 02:49:41 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_panic (const char *file, int line, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, fmt);
|
|
|
|
_vpanic (file, line, fmt, ap);
|
|
|
|
}
|
|
|
|
#endif /* __OUTSIDE_CYGWIN__ */
|