From 36e7cf263c01bbaf901eedf7bac4101da7dedaa5 Mon Sep 17 00:00:00 2001 From: Zorg Date: Thu, 5 Jan 2017 21:23:16 -0500 Subject: [PATCH] Use os_log API for errors instead of ASL if it's available --- Sparkle.xcodeproj/project.pbxproj | 6 ++++ Sparkle/SULog.m | 50 ++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 79043fa4..3ce4e46f 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -159,6 +159,9 @@ 723B5DA71CF7AB0100365F95 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 723B5DA01CF7AB0100365F95 /* main.m */; }; 723B5DA91CF7AB0100365F95 /* SPUDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 723B5DA31CF7AB0100365F95 /* SPUDownloader.m */; }; 723B5DAA1CF7AB6A00365F95 /* SPUDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 723B5DA31CF7AB0100365F95 /* SPUDownloader.m */; }; + 72464F701E1F31E000FB341C /* SUOperatingSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */; }; + 72464F711E1F326A00FB341C /* SUOperatingSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */; }; + 72464F721E1F327C00FB341C /* SUOperatingSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 726F2CE41BC9C33D001971A4 /* SUOperatingSystem.m */; }; 7246E0A31C83B685003B4E75 /* SPUStandardUpdaterController.h in Headers */ = {isa = PBXBuildFile; fileRef = 7246E0A11C83B685003B4E75 /* SPUStandardUpdaterController.h */; settings = {ATTRIBUTES = (Public, ); }; }; 7246E0A41C83B685003B4E75 /* SPUStandardUpdaterController.m in Sources */ = {isa = PBXBuildFile; fileRef = 7246E0A21C83B685003B4E75 /* SPUStandardUpdaterController.m */; }; 724BB3711D31D0B7005D534A /* SUInstallerConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 724BB3701D31D0B7005D534A /* SUInstallerConnection.m */; }; @@ -3042,6 +3045,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72464F711E1F326A00FB341C /* SUOperatingSystem.m in Sources */, 726E07B41CAF08D6001A286B /* main.m in Sources */, 721D8A7B1D4963190032E472 /* SPULocalCacheDirectory.m in Sources */, 7267E5E11D3D901600D1BF90 /* SPUMessageTypes.m in Sources */, @@ -3058,6 +3062,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72464F721E1F327C00FB341C /* SUOperatingSystem.m in Sources */, 723B5DA71CF7AB0100365F95 /* main.m in Sources */, 72E539121D68C3FA0092CE5E /* SPUDownloadData.m in Sources */, 723B5DA91CF7AB0100365F95 /* SPUDownloader.m in Sources */, @@ -3135,6 +3140,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72464F701E1F31E000FB341C /* SUOperatingSystem.m in Sources */, 7267E5A81D3D8A9900D1BF90 /* AgentConnection.m in Sources */, 7267E5A21D3D8A7E00D1BF90 /* AppInstaller.m in Sources */, 7267E5D31D3D8CF200D1BF90 /* bscommon.c in Sources */, diff --git a/Sparkle/SULog.m b/Sparkle/SULog.m index c850eb02..438368d2 100644 --- a/Sparkle/SULog.m +++ b/Sparkle/SULog.m @@ -9,6 +9,8 @@ #include "SULog.h" #include #include "SUExport.h" +#import "SUOperatingSystem.h" +#import #ifdef _APPKITDEFINES_H #error This is a "core" implementation and should NOT import AppKit @@ -24,29 +26,49 @@ static BOOL gDisableStandardErrorStream; // Private API for disable logging to standard error stream // We don't want to do this normally unless eg: releasing a command line utility, // because it may be useful for error output to show up in an immediately visible terminal/panel +// Note this is only necessary for the older ASL API. This is effectively a no-op when os_log is available (10.12+) SU_EXPORT void _SULogDisableStandardErrorStream(void) { gDisableStandardErrorStream = YES; } +// In the future, we could possibly support different logging levels void SULog(NSString *format, ...) { static aslclient client; static dispatch_queue_t queue; static dispatch_once_t onceToken; + + static os_log_t logger; + static BOOL hasOSLogging; + dispatch_once(&onceToken, ^{ NSBundle *mainBundle = [NSBundle mainBundle]; - NSString *displayName = [[NSFileManager defaultManager] displayNameAtPath:mainBundle.bundlePath]; - uint32_t options = ASL_OPT_NO_DELAY; - if (!gDisableStandardErrorStream) { - options |= ASL_OPT_STDERR; + hasOSLogging = [SUOperatingSystem isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10, 12, 0}]; + + NSString *mainBundleIdentifier = mainBundle.bundleIdentifier; + NSString *bundleIdentifier = (mainBundleIdentifier != nil) ? mainBundleIdentifier : @""SPARKLE_BUNDLE_IDENTIFIER; + + if (hasOSLogging) { + const char *subsystem = [[bundleIdentifier stringByAppendingString:@".Sparkle"] UTF8String]; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpartial-availability" + logger = os_log_create(subsystem, "Sparkle"); +#pragma clang diagnostic pop + } else { + uint32_t options = ASL_OPT_NO_DELAY; + if (!gDisableStandardErrorStream) { + options |= ASL_OPT_STDERR; + } + + NSString *displayName = [[NSFileManager defaultManager] displayNameAtPath:mainBundle.bundlePath]; + client = asl_open([displayName stringByAppendingString:@" [Sparkle]"].UTF8String, bundleIdentifier.UTF8String, options); + queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); } - client = asl_open([displayName stringByAppendingString:@" [Sparkle]"].UTF8String, mainBundle.bundleIdentifier.UTF8String, options); - queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); }); - if (client == NULL) { + if (!hasOSLogging && client == NULL) { return; } @@ -55,6 +77,19 @@ void SULog(NSString *format, ...) NSString *logMessage = [[NSString alloc] initWithFormat:format arguments:ap]; va_end(ap); + // Use os_log if available (on 10.12+) + if (hasOSLogging) { + // We'll make all of our messages formatted as public; just don't log sensitive information. + // Note we don't take advantage of info like the source line number because we wrap this macro inside our own function + // And we don't really leverage of os_log's deferred formatting processing because we format the string before passing it in +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpartial-availability" + os_log_error(logger, "%{public}@", logMessage); +#pragma clang diagnostic pop + return; + } + + // Otherwise use ASL // Make sure we do not async, because if we async, the log may not be delivered deterministically dispatch_sync(queue, ^{ aslmsg message = asl_new(ASL_TYPE_MSG); @@ -66,7 +101,6 @@ void SULog(NSString *format, ...) return; } - // In the future, we could possibly have different logging functions using different levels if (asl_set(message, ASL_KEY_LEVEL, TO_STRING(ASL_LEVEL_ERR)) != 0) { return; }