From 477b456f4389ff59706cf74435ce053ef3495497 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 31 Oct 2010 10:13:22 -0400 Subject: [PATCH] Make logging threadsafe. --- Source/DebuggerConnection.m | 26 ++++++++++++++------------ Source/LoggingController.h | 17 ++++++++--------- Source/LoggingController.m | 34 ++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Source/DebuggerConnection.m b/Source/DebuggerConnection.m index 7908bc9..f17697d 100644 --- a/Source/DebuggerConnection.m +++ b/Source/DebuggerConnection.m @@ -426,20 +426,26 @@ void SocketAcceptCallback(CFSocketRef socket, - (LogEntry*)recordSend:(NSString*)command { LoggingController* logger = [(AppDelegate*)[NSApp delegate] loggingController]; - [logger performSelectorOnMainThread:@selector(recordSend:) - withObject:command + LogEntry* entry = [LogEntry newSendEntry:command]; + entry.lastReadTransactionID = lastReadTransaction_; + entry.lastWrittenTransactionID = lastWrittenTransaction_; + [logger performSelectorOnMainThread:@selector(recordEntry:) + withObject:entry waitUntilDone:NO]; - return [logger.logEntries lastObject]; + return [entry autorelease]; } - (LogEntry*)recordReceive:(NSString*)command { LoggingController* logger = [(AppDelegate*)[NSApp delegate] loggingController]; - [logger performSelectorOnMainThread:@selector(recordReceive:) - withObject:command + LogEntry* entry = [LogEntry newReceiveEntry:command]; + entry.lastReadTransactionID = lastReadTransaction_; + entry.lastWrittenTransactionID = lastWrittenTransaction_; + [logger performSelectorOnMainThread:@selector(recordEntry:) + withObject:entry waitUntilDone:NO]; - return [logger.logEntries lastObject]; -} + return [entry autorelease]; +} // Stream Managers ///////////////////////////////////////////////////////////// @@ -577,8 +583,6 @@ void SocketAcceptCallback(CFSocketRef socket, // Log this receive event. LogEntry* log = [self recordReceive:currentPacket_]; log.error = error; - log.lastWrittenTransactionID = lastWrittenTransaction_; - log.lastReadTransactionID = lastReadTransaction_; // Finally, dispatch the handler for this response. [self handleResponse:[xmlTest autorelease]]; @@ -662,9 +666,7 @@ void SocketAcceptCallback(CFSocketRef socket, } // Log this trancation. - LogEntry* log = [self recordSend:command]; - log.lastWrittenTransactionID = lastWrittenTransaction_; - log.lastReadTransactionID = lastReadTransaction_; + [self recordSend:command]; } /** diff --git a/Source/LoggingController.h b/Source/LoggingController.h index 658716a..cb104de 100644 --- a/Source/LoggingController.h +++ b/Source/LoggingController.h @@ -34,15 +34,9 @@ // Designated initializer. - (id)init; -// Creates a new log entry with the specified command, adds it to the entries -// list, and returns the new entry as a weak pointer. Callers can then set -// additional properties. -- (LogEntry*)recordSend:(NSString*)command; - -// Creates a new log entry with the specified response data, adds it to the list -// of entries, and returns the new entry as a weak pointer. Callers can then set -// additional properties. -- (LogEntry*)recordReceive:(NSString*)response; +// Records a log entry. This will add it to the list and will update the UI. +// This will take ownership of |entry|. +- (void)recordEntry:(LogEntry*)entry; @end @@ -74,5 +68,10 @@ typedef enum _LogEntryDirection { @property (retain) NSError* error; @property (assign) NSUInteger lastWrittenTransactionID; @property (assign) NSUInteger lastReadTransactionID; + - (NSString*)directionName; + ++ (LogEntry*)newSendEntry:(NSString*)command; ++ (LogEntry*)newReceiveEntry:(NSString*)command; + @end diff --git a/Source/LoggingController.m b/Source/LoggingController.m index 730cfbf..a8099a3 100644 --- a/Source/LoggingController.m +++ b/Source/LoggingController.m @@ -36,24 +36,10 @@ [super dealloc]; } -- (LogEntry*)recordSend:(NSString*)command +- (void)recordEntry:(LogEntry*)entry { - LogEntry* entry = [LogEntry new]; - entry.direction = kLogEntrySending; - entry.contents = command; [logEntries_ addObject:entry]; - [logEntriesController_ rearrangeObjects]; - return [entry autorelease]; -} - -- (LogEntry*)recordReceive:(NSString*)response -{ - LogEntry* entry = [LogEntry new]; - entry.direction = kLogEntryReceiving; - entry.contents = response; - [logEntries_ addObject:entry]; - [logEntriesController_ rearrangeObjects]; - return [entry autorelease]; + [logEntriesController_ rearrangeObjects]; } @end @@ -68,6 +54,22 @@ @synthesize lastWrittenTransactionID = lastWrittenTransactionID_; @synthesize lastReadTransactionID = lastReadTransactionID_; ++ (LogEntry*)newSendEntry:(NSString*)command +{ + LogEntry* entry = [LogEntry new]; + entry.direction = kLogEntrySending; + entry.contents = command; + return entry; +} + ++ (LogEntry*)newReceiveEntry:(NSString*)command +{ + LogEntry* entry = [LogEntry new]; + entry.direction = kLogEntryReceiving; + entry.contents = command; + return entry; +} + - (void)dealloc { self.contents = nil; -- 2.43.5