This should be a safer implementation of the message log.

This commit is contained in:
Mitchell Livingston 2006-08-21 23:47:29 +00:00
parent 4ecb698720
commit 8ab5e78ec2
2 changed files with 28 additions and 1 deletions

View File

@ -30,10 +30,13 @@
IBOutlet NSPopUpButton * fLevelButton;
NSLock * fLock;
NSMutableArray * fBufferArray;
NSTimer * fTimer;
}
void addMessage(int level, const char * message);
- (void) addMessage: (const char *) message level: (int) level;
- (void) updateLog: (NSTimer *) timer;
- (void) changeLevel: (id) sender;
- (void) clearLog: (id) sender;

View File

@ -29,6 +29,8 @@
#define LEVEL_INFO 1
#define LEVEL_DEBUG 2
#define UPDATE_SECONDS 1.0
@interface MessageWindowController (Private)
MessageWindowController * selfReference; //I'm not sure why I can't use self directly
@ -44,6 +46,10 @@ MessageWindowController * selfReference; //I'm not sure why I can't use self dir
selfReference = self;
fLock = [[NSLock alloc] init];
fBufferArray = [[NSMutableArray alloc] init];
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
selector: @selector(updateLog:) userInfo: nil repeats: YES];
[[self window] update]; //make sure nib is loaded right away
}
@ -52,7 +58,11 @@ MessageWindowController * selfReference; //I'm not sure why I can't use self dir
- (void) dealloc
{
[fTimer invalidate];
[fLock release];
[fBufferArray release];
[super dealloc];
}
@ -100,12 +110,26 @@ void addMessage(int level, const char * message)
[NSString stringWithFormat: @"%@: %s\n", levelString, message]] autorelease];
[fLock lock];
[[fTextView textStorage] appendAttributedString: messageString];
[fBufferArray addObject: messageString];
[fLock unlock];
[pool release];
}
- (void) updateLog: (NSTimer *) timer
{
[fLock lock];
NSEnumerator * enumerator = [fBufferArray objectEnumerator];
NSAttributedString * messageString;
while ((messageString = [enumerator nextObject]))
[[fTextView textStorage] appendAttributedString: messageString];
[fBufferArray removeAllObjects];
[fLock unlock];
}
- (void) changeLevel: (id) sender
{
int selection = [fLevelButton indexOfSelectedItem], level;