X-Git-Url: https://git.saurik.com/apple/syslog.git/blobdiff_plain/8158235332f5a3f4f20cebe26bf739b481ca2df5..refs/heads/master:/libsystem_asl.tproj/src/syslog.c diff --git a/libsystem_asl.tproj/src/syslog.c b/libsystem_asl.tproj/src/syslog.c index cc4110f..6894d49 100644 --- a/libsystem_asl.tproj/src/syslog.c +++ b/libsystem_asl.tproj/src/syslog.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 1999-2010 Apple Inc. All rights reserved. + * Copyright (c) 1999-2015 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * @@ -54,99 +54,155 @@ * SUCH DAMAGE. */ +#include + #include #include #include #include +#include #include #include #include -#include "asl_private.h" - -#ifdef __STDC__ -#include -#else -#include -#endif +#include +#include +#include +#include #define LOG_NO_NOTIFY 0x1000 extern const char *asl_syslog_faciliy_num_to_name(int n); #ifdef BUILDING_VARIANT __private_extern__ pthread_mutex_t _sl_lock; -__private_extern__ aslclient _sl_asl; +__private_extern__ asl_object_t _sl_asl; __private_extern__ char *_sl_ident; __private_extern__ int _sl_fac; __private_extern__ int _sl_opts; __private_extern__ int _sl_mask; #else /* !BUILDING_VARIANT */ __private_extern__ pthread_mutex_t _sl_lock = PTHREAD_MUTEX_INITIALIZER; -__private_extern__ aslclient _sl_asl = NULL; +__private_extern__ asl_object_t _sl_asl = NULL; __private_extern__ char *_sl_ident = NULL; __private_extern__ int _sl_fac = 0; __private_extern__ int _sl_opts = 0; __private_extern__ int _sl_mask = 0; #endif /* BUILDING_VARIANT */ -/* - * syslog, vsyslog -- - * print message on log file; output is intended for syslogd(8). - */ -void -#ifdef __STDC__ -syslog(int pri, const char *fmt, ...) -#else -syslog(pri, fmt, va_alist) - int pri; - char *fmt; - va_dcl -#endif +#define EVAL_ASL (EVAL_SEND_ASL | EVAL_TEXT_FILE | EVAL_ASL_FILE) + +static const os_log_type_t shim_syslog_to_log_type[8] = { + OS_LOG_TYPE_DEFAULT, // LOG_EMERG + OS_LOG_TYPE_DEFAULT, // LOG_ALERT + OS_LOG_TYPE_DEFAULT, // LOG_CRIT + OS_LOG_TYPE_DEFAULT, // LOG_ERR + OS_LOG_TYPE_DEFAULT, // LOG_WARNING + OS_LOG_TYPE_DEFAULT, // LOG_NOTICE + OS_LOG_TYPE_INFO, // LOG_INFO + OS_LOG_TYPE_DEBUG // LOG_DEBUG +}; + +extern uint32_t _asl_evaluate_send(asl_object_t client, asl_object_t m, int slevel); +extern uint32_t _asl_lib_vlog(asl_object_t obj, uint32_t eval, asl_object_t msg, const char *format, va_list ap); +extern uint32_t _asl_lib_vlog_text(asl_object_t obj, uint32_t eval, asl_object_t msg, const char *format, va_list ap); + +static void +_syslog_asl_client() { - va_list ap; - -#ifdef __STDC__ - va_start(ap, fmt); -#else - va_start(ap); -#endif - vsyslog(pri, fmt, ap); - va_end(ap); + pthread_mutex_lock(&_sl_lock); + if (_sl_asl == NULL) + { + _sl_asl = asl_open(NULL, NULL, ASL_OPT_SYSLOG_LEGACY); + _sl_mask = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG); + asl_set_filter(_sl_asl, _sl_mask); + } + pthread_mutex_unlock(&_sl_lock); } -void -vsyslog(int pri, const char *fmt, va_list ap) +static void +_vsyslog(int pri, const char *fmt, va_list ap, void *addr, bool mirror) { - int fac; - aslmsg facmsg; - const char *facility; + int level = pri & LOG_PRIMASK; + int fac = pri & LOG_FACMASK; + asl_object_t msg; + uint32_t eval; + bool trace; + + _syslog_asl_client(); + + msg = asl_new(ASL_TYPE_MSG); - facmsg = NULL; - fac = pri & LOG_FACMASK; if (fac != 0) { - facility = asl_syslog_faciliy_num_to_name(fac); - if (facility != NULL) + const char *facility = asl_syslog_faciliy_num_to_name(fac); + if (facility != NULL) asl_set(msg, ASL_KEY_FACILITY, facility); + } + + eval = _asl_evaluate_send(_sl_asl, msg, level); + trace = (eval & EVAL_SEND_TRACE) && os_log_shim_enabled(addr); + + if (trace) + { + va_list ap_copy; + os_log_type_t type = shim_syslog_to_log_type[level]; + + va_copy(ap_copy, ap); + os_log_with_args(OS_LOG_DEFAULT, type, fmt, ap_copy, addr); + va_end(ap_copy); + + if ((eval & EVAL_TEXT_FILE) && !mirror) { - facmsg = asl_new(ASL_TYPE_MSG); - asl_set(facmsg, ASL_KEY_FACILITY, facility); + _asl_lib_vlog_text(_sl_asl, eval, msg, fmt, ap); } } - pthread_mutex_lock(&_sl_lock); - - /* open syslog ASL client if required */ - if (_sl_asl == NULL) + if ((eval & EVAL_ASL) && (mirror || !trace)) { - _sl_asl = asl_open(NULL, NULL, ASL_OPT_SYSLOG_LEGACY); - _sl_mask = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG); - asl_set_filter(_sl_asl, _sl_mask); + _asl_lib_vlog(_sl_asl, eval, msg, fmt, ap); } - asl_vlog(_sl_asl, facmsg, LOG_PRI(pri), fmt, ap); + asl_release(msg); +} - pthread_mutex_unlock(&_sl_lock); +#if TARGET_OS_OSX + +extern typeof(syslog) syslog_legacy asm("_syslog"); +extern typeof(syslog) syslog_os_log asm("_syslog" __DARWIN_SUF_EXTSN); + +void +syslog_legacy(int pri, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + _vsyslog(pri, fmt, ap, __builtin_return_address(0), true); + va_end(ap); +} + +void +syslog_os_log(int pri, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + _vsyslog(pri, fmt, ap, __builtin_return_address(0), false); + va_end(ap); +} + +#else /* !TARGET_OS_OSX */ - if (facmsg != NULL) asl_free(facmsg); +void +syslog(int pri, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + _vsyslog(pri, fmt, ap, __builtin_return_address(0), false); + va_end(ap); +} + +#endif /* !TARGET_OS_OSX */ + +void +vsyslog(int pri, const char *fmt, va_list ap) +{ + _vsyslog(pri, fmt, ap, __builtin_return_address(0), false); } #ifndef BUILDING_VARIANT @@ -159,8 +215,7 @@ openlog(const char *ident, int opts, int logfac) pthread_mutex_lock(&_sl_lock); - /* close existing aslclient */ - asl_close(_sl_asl); + if (_sl_asl != NULL) asl_release(_sl_asl); _sl_asl = NULL; free(_sl_ident); @@ -196,9 +251,11 @@ closelog() if (_sl_asl != NULL) asl_close(_sl_asl); _sl_asl = NULL; - if (_sl_ident != NULL) free(_sl_ident); + free(_sl_ident); _sl_ident = NULL; + _sl_fac = 0; + pthread_mutex_unlock(&_sl_lock); } @@ -214,6 +271,7 @@ setlogmask(int mask) _sl_mask = mask; oldmask = asl_set_filter(_sl_asl, mask); + if (_sl_opts & LOG_PERROR) asl_set_output_file_filter(_sl_asl, STDERR_FILENO, mask); pthread_mutex_unlock(&_sl_lock);