Use os_log for logging

This commit is contained in:
Ariel Abreu 2021-05-05 15:38:42 -04:00
parent deaf74952c
commit b71a0e24cd
No known key found for this signature in database
GPG Key ID: BB20848279B910AC
3 changed files with 33 additions and 53 deletions

View File

@ -88,6 +88,7 @@ add_circular(xpc
unwind unwind
compiler_rt compiler_rt
system_pthread system_pthread
system_trace
UPWARD UPWARD
# break an upward dependency on libobjc # break an upward dependency on libobjc
# --- # ---

View File

@ -26,7 +26,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <mach/mach.h> #include <mach/mach.h>
#include <sys/syslog.h> #include <os/log.h>
/** /**
* Expands to an expression that evaluates to `true` if the given expression is an object of the given XPC class, or `false` otherwise. * Expands to an expression that evaluates to `true` if the given expression is an object of the given XPC class, or `false` otherwise.
@ -253,35 +253,32 @@ void _xpc_assertion_failed(const char* function, const char* file, size_t line,
#define xpc_assert(x) #define xpc_assert(x)
#endif #endif
#define XPC_LOG_DEBUG LOG_DEBUG #define XPC_LOG_DEBUG OS_LOG_TYPE_DEBUG
#define XPC_LOG_WARNING LOG_WARNING #define XPC_LOG_WARNING OS_LOG_TYPE_INFO
#define XPC_LOG_ERROR LOG_ERR #define XPC_LOG_ERROR OS_LOG_TYPE_ERROR
#define XPC_LOG_NOTICE LOG_NOTICE #define XPC_LOG_NOTICE OS_LOG_TYPE_INFO
#define XPC_LOG_INFO LOG_INFO #define XPC_LOG_INFO OS_LOG_TYPE_INFO
typedef int xpc_log_priority_t; /**
* Determines whether logging should be allowed right now.
*
* This is necessary to avoid logging in places that could cause a deadlock.
*/
bool xpc_should_log(void);
/**
* Returns a logger object that can be used for logging XPC messages with os_log
*/
os_log_t xpc_get_log(void);
/** /**
* Logs a message with the given priority. * Logs a message with the given priority.
*/ */
XPC_PRINTF(5, 6) #define xpc_log(type, format, ...) ({ \
void _xpc_log(const char* function, const char* file, size_t line, xpc_log_priority_t priority, const char* format, ...); if (xpc_should_log()) { \
os_log_with_type(xpc_get_log(), type, "%s:%lu: %s: " format, __FILE__, (long unsigned)__LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
/** } \
* @see _xpc_log })
*/
XPC_PRINTF(5, 0)
void _xpc_logv(const char* function, const char* file, size_t line, xpc_log_priority_t priority, const char* format, va_list args);
/**
* Logs a message with the given priority. This macro automatically fills in most of the arguments to `_xpc_log`.
*/
#define xpc_log(...) _xpc_log(__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
/**
* @see xpc_log
*/
#define xpc_logv(...) _xpc_logv(__PRETTY_FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
/** /**
* Prints a message indicating that a stub was called. * Prints a message indicating that a stub was called.

View File

@ -21,19 +21,14 @@
#import <objc/runtime.h> #import <objc/runtime.h>
#import <xpc/serialization.h> #import <xpc/serialization.h>
#include <sys/reason.h> #include <sys/reason.h>
#include <sys/syslog.h>
#import <xpc/objects/connection.h> #import <xpc/objects/connection.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdio.h> #include <stdio.h>
#include <mach-o/dyld.h> #include <mach-o/dyld.h>
#ifndef XPC_LOG_TO_STDOUT_TOO
#define XPC_LOG_TO_STDOUT_TOO 0
#endif
static bool enable_stub_messages = false; static bool enable_stub_messages = false;
static bool enable_syslog(void) { bool xpc_should_log(void) {
static bool enabled = true; static bool enabled = true;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
@ -251,28 +246,6 @@ void _xpc_assertion_failed(const char* function, const char* file, size_t line,
_xpc_abort(function, file, line, "assertion failed: %s", expression); _xpc_abort(function, file, line, "assertion failed: %s", expression);
}; };
void _xpc_logv(const char* function, const char* file, size_t line, xpc_log_priority_t priority, const char* format, va_list args) {
char* message = NULL;
char* reason = NULL;
vasprintf(&reason, format, args);
#if XPC_LOG_TO_STDOUT_TOO
// also log to stdout
printf("libxpc: %s:%zu: %s: %s\n", file, line, function, reason);
fflush(stdout);
#endif
if (enable_syslog()) {
syslog(priority, "libxpc: %s:%zu: %s: %s", file, line, function, reason);
}
free(reason);
};
void _xpc_log(const char* function, const char* file, size_t line, xpc_log_priority_t priority, const char* format, ...) {
va_list args;
va_start(args, format);
_xpc_logv(function, file, line, priority, format, args);
va_end(args);
};
void xpc_stub_init(void) { void xpc_stub_init(void) {
enable_stub_messages = getenv("STUB_VERBOSE") != NULL; enable_stub_messages = getenv("STUB_VERBOSE") != NULL;
}; };
@ -310,3 +283,12 @@ retry:
out: out:
return result; return result;
}; };
os_log_t xpc_get_log(void) {
static os_log_t logger = NULL;
static dispatch_once_t token;
dispatch_once(&token, ^{
logger = os_log_create("org.darlinghq.libxpc", "general");
});
return logger;
};