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.
This commit is contained in:
Zorg committed 2016-05-18 18:48:01 -04:00
1 parent 3b05179ccf
commit 346f996942
2 files changed
+52 -90

No files matched your search

+7 -23
View File
@@ -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 <Foundation/Foundation.h>
// -----------------------------------------------------------------------------
// Prototypes:
// -----------------------------------------------------------------------------
void SUClearLog(void);
void SULog(NSString *format, ...) NS_FORMAT_FUNCTION(1, 2);
#endif
+45 -67
View File
@@ -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 <asl.h>
#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);
});
}