2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * bootstrap -- fundamental service initiator and port server
26 * Mike DeMoney, NeXT, Inc.
27 * Copyright, 1990. All rights reserved.
29 * error_log.c -- implementation of logging routines
31 * Routines may be safely invoked from multiple threads
35 #import <mach/mach_error.h>
38 #import <sys/syslimits.h>
42 #import "bootstrap_internal.h"
45 static pthread_mutex_t errlog_lock
= PTHREAD_MUTEX_INITIALIZER
;
48 init_errlog(boolean_t is_init
)
54 freopen("/dev/console", "r", stdin
);
57 freopen("/dev/console", "w", stdout
);
60 freopen("/dev/console", "w", stderr
);
64 nfds
= getdtablesize();
65 for (fd
= 3; fd
< nfds
; fd
++)
67 openlog((char *)program_name
, LOG_PID
, LOG_DAEMON
);
68 setlogmask(LOG_UPTO(LOG_INFO
));
77 static void do_log(const int level
, const char *format
, va_list ap
)
79 pthread_mutex_lock(&errlog_lock
);
81 fprintf(stderr
, "%s[%d]%s: ",
82 level
== LOG_ALERT
? " FATAL" : "",
83 getpid(), program_name
);
84 vfprintf(stderr
, format
, ap
);
85 fprintf(stderr
, "\n");
87 vsyslog(level
, format
, ap
);
89 pthread_mutex_unlock(&errlog_lock
);
92 void debug(const char *format
, ...)
98 do_log(LOG_DEBUG
, format
, ap
);
103 void info(const char *format
, ...)
107 va_start(ap
, format
);
108 do_log(LOG_INFO
, format
, ap
);
112 __private_extern__
void log(const char *format
, ...)
116 va_start(ap
, format
);
117 do_log(LOG_NOTICE
, format
, ap
);
121 void error(const char *format
, ...)
125 va_start(ap
, format
);
126 do_log(LOG_CRIT
, format
, ap
);
130 void kern_error(kern_return_t result
, const char *format
, ...)
135 sprintf(buf
, "%s: %s(%d)", format
, mach_error_string(result
), result
);
137 va_start(ap
, format
);
138 do_log(LOG_CRIT
, buf
, ap
);
142 void unix_error(const char *format
, ...)
147 sprintf(buf
, "%s: %s(%d)", format
, strerror(errno
), errno
);
149 va_start(ap
, format
);
150 do_log(LOG_CRIT
, buf
, ap
);
154 void parse_error(const char *token_string
, const char *format
, ...)
159 sprintf(buf
, "%s unexpected: %s", token_string
, format
);
161 va_start(ap
, format
);
162 do_log(LOG_CRIT
, buf
, ap
);
166 void fatal(const char *format
, ...)
170 va_start(ap
, format
);
171 do_log(LOG_ALERT
, format
, ap
);
176 void kern_fatal(kern_return_t result
, const char *format
, ...)
181 sprintf(buf
, "%s: %s(%d)", format
, mach_error_string(result
), result
);
183 va_start(ap
, format
);
184 do_log(LOG_ALERT
, buf
, ap
);
189 void unix_fatal(const char *format
, ...)
194 sprintf(buf
, "%s: %s(%d)", format
, strerror(errno
), errno
);
196 va_start(ap
, format
);
197 do_log(LOG_ALERT
, buf
, ap
);