2006-08-21 22:07:08 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#import "MessageWindowController.h"
|
|
|
|
#import <transmission.h>
|
|
|
|
|
|
|
|
#define LEVEL_ERROR 0
|
|
|
|
#define LEVEL_INFO 1
|
|
|
|
#define LEVEL_DEBUG 2
|
|
|
|
|
2006-08-22 01:08:44 +00:00
|
|
|
#define UPDATE_SECONDS 0.35
|
2006-08-21 23:47:29 +00:00
|
|
|
|
2006-08-21 22:07:08 +00:00
|
|
|
@interface MessageWindowController (Private)
|
|
|
|
|
2006-08-22 00:02:44 +00:00
|
|
|
MessageWindowController * selfReference; //I'm not sure why "self" can't be used directly
|
2006-08-21 22:07:08 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MessageWindowController
|
|
|
|
|
|
|
|
- (id) initWithWindowNibName: (NSString *) name
|
|
|
|
{
|
|
|
|
if ((self = [super initWithWindowNibName: name]))
|
|
|
|
{
|
|
|
|
selfReference = self;
|
|
|
|
|
|
|
|
fLock = [[NSLock alloc] init];
|
2006-08-21 23:47:29 +00:00
|
|
|
fBufferArray = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
|
|
|
|
selector: @selector(updateLog:) userInfo: nil repeats: YES];
|
2006-08-21 22:23:09 +00:00
|
|
|
|
|
|
|
[[self window] update]; //make sure nib is loaded right away
|
2006-08-21 22:07:08 +00:00
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2006-08-21 23:47:29 +00:00
|
|
|
[fTimer invalidate];
|
|
|
|
|
2006-08-22 00:02:44 +00:00
|
|
|
tr_setMessageFunction(NULL);
|
|
|
|
|
2006-08-21 22:07:08 +00:00
|
|
|
[fLock release];
|
2006-08-21 23:47:29 +00:00
|
|
|
[fBufferArray release];
|
|
|
|
|
2006-08-21 22:07:08 +00:00
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) awakeFromNib
|
|
|
|
{
|
|
|
|
int level = [[NSUserDefaults standardUserDefaults] integerForKey: @"MessageLevel"];
|
|
|
|
if (level == TR_MSG_ERR)
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_ERROR];
|
|
|
|
else if (level == TR_MSG_INF)
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_INFO];
|
|
|
|
else if (level == TR_MSG_DBG)
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_DEBUG];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level = TR_MSG_ERR;
|
|
|
|
[fLevelButton selectItemAtIndex: LEVEL_ERROR];
|
|
|
|
[[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_setMessageLevel(level);
|
|
|
|
tr_setMessageFunction(addMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addMessage(int level, const char * message)
|
|
|
|
{
|
2006-08-21 22:23:09 +00:00
|
|
|
[selfReference addMessage: message level: level];
|
2006-08-21 22:07:08 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 22:23:09 +00:00
|
|
|
- (void) addMessage: (const char *) message level: (int) level
|
2006-08-21 22:07:08 +00:00
|
|
|
{
|
|
|
|
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
NSString * levelString;
|
2006-08-22 01:59:46 +00:00
|
|
|
|
2006-08-21 22:07:08 +00:00
|
|
|
if (level == TR_MSG_ERR)
|
|
|
|
levelString = @"ERR";
|
|
|
|
else if (level == TR_MSG_INF)
|
|
|
|
levelString = @"INF";
|
|
|
|
else if (level == TR_MSG_DBG)
|
|
|
|
levelString = @"DBG";
|
|
|
|
else
|
|
|
|
levelString = @"???";
|
|
|
|
|
|
|
|
NSAttributedString * messageString = [[[NSAttributedString alloc] initWithString:
|
2006-08-22 01:59:46 +00:00
|
|
|
[NSString stringWithFormat: @"(%@ %@) %s\n", [[NSDate date] dateWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"
|
|
|
|
timeZone: nil], levelString, message]] autorelease];
|
2006-08-21 22:07:08 +00:00
|
|
|
|
|
|
|
[fLock lock];
|
2006-08-21 23:47:29 +00:00
|
|
|
[fBufferArray addObject: messageString];
|
2006-08-21 22:07:08 +00:00
|
|
|
[fLock unlock];
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2006-08-21 23:47:29 +00:00
|
|
|
- (void) updateLog: (NSTimer *) timer
|
|
|
|
{
|
2006-08-22 00:02:44 +00:00
|
|
|
if ([fBufferArray count] == 0)
|
|
|
|
return;
|
|
|
|
|
2006-08-22 01:59:46 +00:00
|
|
|
[fLock lock];
|
|
|
|
|
2006-08-22 01:08:44 +00:00
|
|
|
//keep scrolled to bottom if already at bottom or there is no scroll bar yet
|
|
|
|
BOOL shouldScroll = NO;
|
|
|
|
NSScroller * scroller = [fScrollView verticalScroller];
|
|
|
|
if ([scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0)
|
|
|
|
shouldScroll = YES;
|
|
|
|
|
2006-08-21 23:47:29 +00:00
|
|
|
NSEnumerator * enumerator = [fBufferArray objectEnumerator];
|
|
|
|
NSAttributedString * messageString;
|
|
|
|
while ((messageString = [enumerator nextObject]))
|
|
|
|
[[fTextView textStorage] appendAttributedString: messageString];
|
|
|
|
[fBufferArray removeAllObjects];
|
|
|
|
|
2006-08-22 01:59:46 +00:00
|
|
|
[fTextView setFont: [NSFont fontWithName: @"Monaco" size: 10]]; //find a way to set this permanently
|
2006-08-22 01:08:44 +00:00
|
|
|
|
|
|
|
if (shouldScroll)
|
|
|
|
[fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
|
2006-08-22 01:59:46 +00:00
|
|
|
|
|
|
|
[fLock unlock];
|
2006-08-21 23:47:29 +00:00
|
|
|
}
|
|
|
|
|
2006-08-21 22:07:08 +00:00
|
|
|
- (void) changeLevel: (id) sender
|
|
|
|
{
|
|
|
|
int selection = [fLevelButton indexOfSelectedItem], level;
|
|
|
|
if (selection == LEVEL_INFO)
|
|
|
|
level = TR_MSG_INF;
|
|
|
|
else if (selection == LEVEL_DEBUG)
|
|
|
|
level = TR_MSG_DBG;
|
|
|
|
else
|
|
|
|
level = TR_MSG_ERR;
|
|
|
|
|
2006-08-22 01:08:44 +00:00
|
|
|
[self updateLog: nil];
|
|
|
|
|
2006-08-21 22:07:08 +00:00
|
|
|
tr_setMessageLevel(level);
|
|
|
|
[[NSUserDefaults standardUserDefaults] setInteger: level forKey: @"MessageLevel"];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) clearLog: (id) sender
|
|
|
|
{
|
2006-08-21 22:10:29 +00:00
|
|
|
[fLock lock];
|
2006-08-21 22:07:08 +00:00
|
|
|
[fTextView setString: @""];
|
2006-08-21 22:10:29 +00:00
|
|
|
[fLock unlock];
|
2006-08-21 22:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|