From e716f6d35e2e6123687da1f447dede37a5ee7fa8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 4 Mar 2018 12:28:06 +0530 Subject: [PATCH] Replace deprecated ASL with os_log on macOS --- kitty/logging.c | 41 ++++++++++++++++++++++++++++++++--------- linux-launcher.c | 17 ----------------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/kitty/logging.c b/kitty/logging.c index b6ed01003..45c88174a 100644 --- a/kitty/logging.c +++ b/kitty/logging.c @@ -6,28 +6,48 @@ */ #include "data-types.h" +#include #include #include #include +#ifdef __APPLE__ +#include +#endif +static bool use_os_log = false; + void log_error(const char *fmt, ...) { va_list ar; struct timeval tv; - gettimeofday(&tv, NULL); - struct tm *tmp = localtime(&tv.tv_sec); - if (tmp) { - char tbuf[256], buf[256]; - if (strftime(buf, sizeof(buf), "%j %H:%M:%S.%%06u", tmp) != 0) { - snprintf(tbuf, sizeof(tbuf), buf, tv.tv_usec); - fprintf(stderr, "[%s] ", tbuf); +#ifdef __APPLE__ + // Apple does not provide a varargs style os_logv + char logbuf[16 * 1024] = {0}; +#else + char logbuf[4]; +#endif + char *p = logbuf; +#define bufprint(func, ...) { if ((size_t)(p - logbuf) < sizeof(logbuf) - 2) { p += func(p, sizeof(logbuf) - (p - logbuf), __VA_ARGS__); } } + if (!use_os_log) { // Apple's os_log already records timestamps + gettimeofday(&tv, NULL); + struct tm *tmp = localtime(&tv.tv_sec); + if (tmp) { + char tbuf[256] = {0}, buf[256] = {0}; + if (strftime(buf, sizeof(buf), "%j %H:%M:%S.%%06u", tmp) != 0) { + snprintf(tbuf, sizeof(tbuf), buf, tv.tv_usec); + fprintf(stderr, "[%s] ", tbuf); + } } } va_start(ar, fmt); - vfprintf(stderr, fmt, ar); + if (use_os_log) { bufprint(vsnprintf, fmt, ar); } + else vfprintf(stderr, fmt, ar); va_end(ar); - fprintf(stderr, "\n"); +#ifdef __APPLE__ + if (use_os_log) os_log(OS_LOG_DEFAULT, "%{public}s", logbuf); +#endif + if (!use_os_log) fprintf(stderr, "\n"); } static PyObject* @@ -46,5 +66,8 @@ static PyMethodDef module_methods[] = { bool init_logging(PyObject *module) { if (PyModule_AddFunctions(module, module_methods) != 0) return false; +#ifdef __APPLE__ + if (getenv("KITTY_LAUNCHED_BY_LAUNCH_SERVICES") != NULL) use_os_log = true; +#endif return true; } diff --git a/linux-launcher.c b/linux-launcher.c index 9faa9d7e8..b0bde136b 100644 --- a/linux-launcher.c +++ b/linux-launcher.c @@ -14,7 +14,6 @@ #ifdef __APPLE__ #include #include -#include #else #include #endif @@ -24,20 +23,6 @@ #define MIN(x, y) ((x) < (y)) ? (x) : (y) #define MAX_ARGC 1024 -#ifdef __APPLE__ -static inline void -redirect_to_asl() { - if (getenv("KITTY_LAUNCHED_BY_LAUNCH_SERVICES") != NULL) { - // ASL was deprecated by Apple in 10.12, but there is no replacement that - // I can find for asl_log_descriptor, so continue to use it. -_Pragma("clang diagnostic push") _Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") - asl_log_descriptor(NULL, NULL, ASL_LEVEL_INFO, STDOUT_FILENO, ASL_LOG_DESCRIPTOR_WRITE); - asl_log_descriptor(NULL, NULL, ASL_LEVEL_NOTICE, STDERR_FILENO, ASL_LOG_DESCRIPTOR_WRITE); -_Pragma("clang diagnostic pop") - } -} -#endif - #ifdef FOR_BUNDLE static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) { int num; @@ -49,7 +34,6 @@ static int run_embedded(const char* exe_dir_, int argc, wchar_t **argv) { Py_IsolatedFlag = 1; Py_SetProgramName(L"kitty"); - redirect_to_asl(); int ret = 1; wchar_t *exe_dir = Py_DecodeLocale(exe_dir_, NULL); if (exe_dir == NULL) { fprintf(stderr, "Fatal error: cannot decode exe_dir\n"); return 1; } @@ -93,7 +77,6 @@ int main(int argc, char *argv[]) { #ifdef __APPLE__ uint32_t size = PATH_MAX; char apple[PATH_MAX+1] = {0}; - redirect_to_asl(); if (_NSGetExecutablePath(apple, &size) != 0) { fprintf(stderr, "Failed to get path to executable\n"); return 1; } if (realpath(apple, exe) == NULL) { fprintf(stderr, "realpath() failed on the executable's path\n"); return 1; } #else