From 346f996942277a24af037bb5c2f0268e95e67b67 Mon Sep 17 00:00:00 2001 From: Zorg Date: Wed, 18 May 2016 18:48:01 -0400 Subject: [PATCH] Re-write logging functionality 1. Use Apple's logging system (ASL) - don't invent our own inefficient mechanism. 2. Logging is not dependent on a global file that any process and thread using Sparkle can write to now, which could also be cleared at any point before. 3. Don't write to stderr on non-debug builds. This is ideal for sparkle-cli utility. --- Sparkle/SULog.h | 30 +++---------- Sparkle/SULog.m | 112 +++++++++++++++++++----------------------------- 2 files changed, 52 insertions(+), 90 deletions(-) diff --git a/Sparkle/SULog.h b/Sparkle/SULog.h index 1693f535..15d835b7 100644 --- a/Sparkle/SULog.h +++ b/Sparkle/SULog.h @@ -1,32 +1,16 @@ -/* - * SULog.h - * EyeTV - * - * Created by Uli Kusterer on 12/03/2009. - * Copyright 2008 Elgato Systems GmbH. All rights reserved. - * - */ - -/* - Log output for troubleshooting Sparkle failures on end-user machines. - Your tech support will hug you if you tell them about this. -*/ +// +// SULog.h +// Sparkle +// +// Created by Mayur Pawashe on 5/18/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// #ifndef SULOG_H #define SULOG_H -// ----------------------------------------------------------------------------- -// Headers: -// ----------------------------------------------------------------------------- - #include - -// ----------------------------------------------------------------------------- -// Prototypes: -// ----------------------------------------------------------------------------- - -void SUClearLog(void); void SULog(NSString *format, ...) NS_FORMAT_FUNCTION(1, 2); #endif diff --git a/Sparkle/SULog.m b/Sparkle/SULog.m index f886a07f..25063ce8 100644 --- a/Sparkle/SULog.m +++ b/Sparkle/SULog.m @@ -1,82 +1,60 @@ -/* - * SULog.m - * EyeTV - * - * Created by Uli Kusterer on 12/03/2009. - * Copyright 2009 Elgato Systems GmbH. All rights reserved. - * - */ - -// ----------------------------------------------------------------------------- -// Headers: -// ----------------------------------------------------------------------------- +// +// SULog.m +// Sparkle +// +// Created by Mayur Pawashe on 5/18/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// #include "SULog.h" +#include #ifdef _APPKITDEFINES_H #error This is a "core" implementation and should NOT import AppKit #endif - -// ----------------------------------------------------------------------------- -// Constants: -// ----------------------------------------------------------------------------- - -static NSString *const SULogFilePath = @"~/Library/Logs/SparkleUpdateLog.log"; - - -// ----------------------------------------------------------------------------- -// SUClearLog: -// Erase the log at the start of an update. We don't want to litter the -// user's hard disk with logging data that's mostly unused, so each app -// should clear the log before it starts updating, so only the most recent -// update is kept around. -// -// TAKES: -// sender - Object that sent this message, typically of type X. -// ----------------------------------------------------------------------------- - -void SUClearLog(void) -{ - FILE *logfile = fopen([[SULogFilePath stringByExpandingTildeInPath] fileSystemRepresentation], "w"); - if (logfile) { - fclose(logfile); - SULog(@"===== %@ =====", [[NSFileManager defaultManager] displayNameAtPath:[[NSBundle mainBundle] bundlePath]]); - } -} - - -// ----------------------------------------------------------------------------- -// SULog: -// Like NSLog, but logs to one specific log file. Each line is prefixed -// with the current date and time, to help in regressing issues. -// -// TAKES: -// format - NSLog/printf-style format string. -// ... - More parameters depending on format string's contents. -// ----------------------------------------------------------------------------- - void SULog(NSString *format, ...) { - static BOOL loggedYet = NO; - if (!loggedYet) { - loggedYet = YES; - SUClearLog(); + static aslclient client; + static dispatch_queue_t queue; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + NSBundle *mainBundle = [NSBundle mainBundle]; + NSString *displayName = [[NSFileManager defaultManager] displayNameAtPath:mainBundle.bundlePath]; +#ifdef DEBUG + uint32_t options = ASL_OPT_NO_DELAY | ASL_OPT_STDERR; +#else + uint32_t options = ASL_OPT_NO_DELAY; +#endif + client = asl_open([displayName stringByAppendingString:@" [Sparkle]"].UTF8String, mainBundle.bundleIdentifier.UTF8String, options); + queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); + }); + + if (client == NULL) { + return; } - + va_list ap; va_start(ap, format); - NSString *theStr = [[NSString alloc] initWithFormat:format arguments:ap]; - NSLog(@"Sparkle: %@", theStr); - - FILE *logfile = fopen([[SULogFilePath stringByExpandingTildeInPath] fileSystemRepresentation], "a"); - if (logfile) { - theStr = [NSString stringWithFormat:@"%@: %@\n", [NSDate date], theStr]; - NSData *theData = [theStr dataUsingEncoding:NSUTF8StringEncoding]; - fwrite([theData bytes], 1, [theData length], logfile); - fclose(logfile); - } + NSString *logMessage = [[NSString alloc] initWithFormat:format arguments:ap]; va_end(ap); + + dispatch_async(queue, ^{ + aslmsg message = asl_new(ASL_TYPE_MSG); + if (message == NULL) { + return; + } + + if (asl_set(message, ASL_KEY_MSG, logMessage.UTF8String) != 0) { + return; + } + + // In the future, we could possibly have different logging functions using different levels + if (asl_set(message, ASL_KEY_LEVEL, "3" /* ASL_LEVEL_ERR */) != 0) { + return; + } + + asl_send(client, message); + }); } -