Changeset 818


Ignore:
Timestamp:
Aug 22, 2006, 2:35:01 AM (15 years ago)
Author:
livings124
Message:

Work with new way of logging messages.

Location:
trunk/macosx
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/macosx/MessageWindowController.h

    r813 r818  
    3232    IBOutlet NSPopUpButton * fLevelButton;
    3333   
    34     NSLock * fLock;
    35     NSMutableArray * fBufferArray;
    3634    NSTimer * fTimer;
    3735}
    3836
    39 void addMessage(int level, const char * message);
    40 - (void) addMessage: (const char *) message level: (int) level;
    4137- (void) updateLog: (NSTimer *) timer;
    4238
  • trunk/macosx/MessageWindowController.m

    r814 r818  
    3232#define UPDATE_SECONDS 0.35
    3333
    34 @interface MessageWindowController (Private)
    35 
    36 MessageWindowController * selfReference; //I'm not sure why "self" can't be used directly
    37 
    38 @end
    39 
    4034@implementation MessageWindowController
    4135
     
    4438    if ((self = [super initWithWindowNibName: name]))
    4539    {
    46         selfReference = self;
    47        
    48         fLock = [[NSLock alloc] init];
    49         fBufferArray = [[NSMutableArray alloc] init];
    50        
    5140        fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
    5241                    selector: @selector(updateLog:) userInfo: nil repeats: YES];
     
    6049{
    6150    [fTimer invalidate];
    62    
    63     tr_setMessageFunction(NULL);
    64    
    65     [fLock release];
    66     [fBufferArray release];
    67    
    6851    [super dealloc];
    6952}
     
    8669   
    8770    tr_setMessageLevel(level);
    88     tr_setMessageFunction(addMessage);
    89 }
    90 
    91 void addMessage(int level, const char * message)
    92 {
    93     [selfReference addMessage: message level: level];
    94 }
    95 
    96 - (void) addMessage: (const char *) message level: (int) level
    97 {
    98     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    99 
    100     NSString * levelString;
    101 
    102     if (level == TR_MSG_ERR)
    103         levelString = @"ERR";
    104     else if (level == TR_MSG_INF)
    105         levelString = @"INF";
    106     else if (level == TR_MSG_DBG)
    107         levelString = @"DBG";
    108     else
    109         levelString = @"???";
    110    
    111     NSAttributedString * messageString = [[[NSAttributedString alloc] initWithString:
    112             [NSString stringWithFormat: @"(%@ %@) %s\n", [[NSDate date] dateWithCalendarFormat: @"%Y-%m-%d %H:%M:%S.%F"
    113                                 timeZone: nil], levelString, message]] autorelease];
    114    
    115     [fLock lock];
    116     [fBufferArray addObject: messageString];
    117     [fLock unlock];
    118    
    119     [pool release];
     71    tr_setMessageQueuing(1);
    12072}
    12173
    12274- (void) updateLog: (NSTimer *) timer
    12375{
    124     if ([fBufferArray count] == 0)
     76    tr_msg_list_t * messages = tr_getQueuedMessages(), * currentMessage;
     77    if (!messages)
    12578        return;
    12679   
    127     [fLock lock];
     80    //keep scrolled to bottom if already at bottom or there is no scroll bar yet
     81    NSScroller * scroller = [fScrollView verticalScroller];
     82    BOOL shouldScroll = [scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0;
    12883   
    129     //keep scrolled to bottom if already at bottom or there is no scroll bar yet
    130     BOOL shouldScroll = NO;
    131     NSScroller * scroller = [fScrollView verticalScroller];
    132     if ([scroller floatValue] == 1.0 || [scroller isHidden] || [scroller knobProportion] == 1.0)
    133         shouldScroll = YES;
     84    NSAttributedString * messageString;
     85    NSString * levelString;
     86    for (currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
     87    {
     88        int level = currentMessage->level;
     89        if (level == TR_MSG_ERR)
     90            levelString = @"ERR";
     91        else if (level == TR_MSG_INF)
     92            levelString = @"INF";
     93        else if (level == TR_MSG_DBG)
     94            levelString = @"DBG";
     95        else
     96            levelString = @"???";
     97       
     98        messageString = [[[NSAttributedString alloc] initWithString: [NSString stringWithFormat: @"%@ %s\n",
     99                                                            levelString, currentMessage->message]] autorelease];
     100        [[fTextView textStorage] appendAttributedString: messageString];
     101    }
    134102   
    135     NSEnumerator * enumerator = [fBufferArray objectEnumerator];
    136     NSAttributedString * messageString;
    137     while ((messageString = [enumerator nextObject]))
    138         [[fTextView textStorage] appendAttributedString: messageString];
    139     [fBufferArray removeAllObjects];
     103    tr_freeMessageList(messages);
    140104   
    141105    [fTextView setFont: [NSFont fontWithName: @"Monaco" size: 10]]; //find a way to set this permanently
     
    143107    if (shouldScroll)
    144108        [fTextView scrollRangeToVisible: NSMakeRange([[fTextView string] length], 0)];
    145    
    146     [fLock unlock];
    147109}
    148110
     
    165127- (void) clearLog: (id) sender
    166128{
    167     [fLock lock];
    168129    [fTextView setString: @""];
    169     [fLock unlock];
    170130}
    171131
Note: See TracChangeset for help on using the changeset viewer.