Changeset 241

Show
Ignore:
Timestamp:
19-03-2008 20:14:06 (5 years ago)
Author:
jppavao
Message:

The console now automatically keeps track of the most recently exchanged XML stanzas in the background when logging is disabled. When logging is enabled, all the messages that were stored in that buffer are dumped to the console window before live logging begins. This background buffer is currently capable of holding up to 1000 stanzas. Closes #206.

Location:
trunk/lilypad
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/lilypad/Lilypad.xcodeproj/jpp.pbxuser

    r240 r241  
    301301                        D4073F450D919B8300BC4391 /* PBXTextBookmark */ = D4073F450D919B8300BC4391 /* PBXTextBookmark */; 
    302302                        D4073F460D919B8400BC4391 /* PBXTextBookmark */ = D4073F460D919B8400BC4391 /* PBXTextBookmark */; 
     303                        D4073F510D919F5100BC4391 /* PBXTextBookmark */ = D4073F510D919F5100BC4391 /* PBXTextBookmark */; 
     304                        D4073F5F0D91A49C00BC4391 /* PBXTextBookmark */ = D4073F5F0D91A49C00BC4391 /* PBXTextBookmark */; 
    303305                        D4C717A90D90402100E36F08 = D4C717A90D90402100E36F08 /* PBXTextBookmark */; 
    304306                        D4C7182B0D91315A00E36F08 = D4C7182B0D91315A00E36F08 /* PBXTextBookmark */; 
     
    347349        9606FEDD09198DD800785217 /* LPXmlConsoleController.h */ = { 
    348350                uiCtxt = { 
    349                         sepNavIntBoundsRect = "{{0, 0}, {748, 752}}"; 
    350                         sepNavSelRange = "{579, 0}"; 
    351                         sepNavVisRange = "{0, 982}"; 
     351                        sepNavIntBoundsRect = "{{0, 0}, {786, 769}}"; 
     352                        sepNavSelRange = "{683, 24}"; 
     353                        sepNavVisRange = "{0, 1178}"; 
    352354                        sepNavVisRect = "{{0, 0}, {762, 813}}"; 
    353355                        sepNavWindowFrame = "{{70, 69}, {807, 809}}"; 
     
    356358        9606FEDE09198DD800785217 /* LPXmlConsoleController.m */ = { 
    357359                uiCtxt = { 
    358                         sepNavIntBoundsRect = "{{0, 0}, {748, 3738}}"; 
    359                         sepNavSelRange = "{4439, 0}"; 
    360                         sepNavVisRange = "{2514, 2127}"; 
     360                        sepNavIntBoundsRect = "{{0, 0}, {786, 4410}}"; 
     361                        sepNavSelRange = "{8087, 5}"; 
     362                        sepNavVisRange = "{6881, 1765}"; 
    361363                        sepNavVisRect = "{{0, 394}, {963, 346}}"; 
    362364                        sepNavWindowFrame = "{{573, 69}, {807, 809}}"; 
     
    373375        9606FF7C0919967B00785217 /* NSAttributedString+FactoryAdditions.m */ = { 
    374376                uiCtxt = { 
    375                         sepNavIntBoundsRect = "{{0, 0}, {802, 720}}"; 
    376                         sepNavSelRange = "{0, 0}"; 
     377                        sepNavIntBoundsRect = "{{0, 0}, {801, 821}}"; 
     378                        sepNavSelRange = "{444, 113}"; 
     379                        sepNavVisRange = "{0, 965}"; 
    377380                        sepNavVisRect = "{{0, 0}, {802, 720}}"; 
     381                        sepNavWindowFrame = "{{15, 0}, {860, 878}}"; 
    378382                }; 
    379383        }; 
     
    27232727                vrLen = 0; 
    27242728                vrLoc = 0; 
     2729        }; 
     2730        D4073F510D919F5100BC4391 /* PBXTextBookmark */ = { 
     2731                isa = PBXTextBookmark; 
     2732                fRef = D40CB1170C45288B0003859D /* LPUIController.m */; 
     2733                name = "LPUIController.m: 1907"; 
     2734                rLen = 13; 
     2735                rLoc = 65829; 
     2736                rType = 0; 
     2737                vrLen = 1498; 
     2738                vrLoc = 64811; 
     2739        }; 
     2740        D4073F5F0D91A49C00BC4391 /* PBXTextBookmark */ = { 
     2741                isa = PBXTextBookmark; 
     2742                fRef = D40CB1170C45288B0003859D /* LPUIController.m */; 
     2743                name = "LPUIController.m: 1907"; 
     2744                rLen = 13; 
     2745                rLoc = 65829; 
     2746                rType = 0; 
     2747                vrLen = 1498; 
     2748                vrLoc = 64811; 
    27252749        }; 
    27262750        D407712B0A513EEA004C5BD8 /* LPSapoAgentsDebugWinCtrl.h */ = { 
  • trunk/lilypad/Sources/LPXmlConsoleController.h

    r239 r241  
    2424        IBOutlet NSButton       *m_enableCheckbox; 
    2525         
    26         BOOL            m_enabled; 
    27         LPAccount       *m_account; 
     26        BOOL                            m_enabled; 
     27        LPAccount                       *m_account; 
     28         
     29        // Keeps the most recent XML Stanzas 
     30        NSMutableArray          *m_recentXMLStanzasBuffer; 
    2831} 
    2932 
     
    3942- (void)setLoggingEnabled:(BOOL)flag; 
    4043 
    41 - (void)appendXmlString:(NSString *)string inbound:(BOOL)isInbound; 
     44- (NSAttributedString *)attributedStringForConsoleFromXMLString:(NSString *)string inbound:(BOOL)isInbound; 
     45- (void)appendXMLString:(NSString *)string inbound:(BOOL)isInbound; 
    4246 
    4347@end 
  • trunk/lilypad/Sources/LPXmlConsoleController.m

    r239 r241  
    2323        if (self = [self initWithWindowNibName:@"XMLConsole"]) { 
    2424                m_account = [account retain]; 
     25                m_recentXMLStanzasBuffer = [[NSMutableArray alloc] init]; 
    2526 
    2627                [[NSNotificationCenter defaultCenter] addObserver:self 
     
    4142        [[NSNotificationCenter defaultCenter] removeObserver:self]; 
    4243         
     44        [m_recentXMLStanzasBuffer release]; 
    4345        [m_account release]; 
    4446        [super dealloc]; 
     
    206208- (void)setLoggingEnabled:(BOOL)flag 
    207209{ 
    208         m_enabled = flag; 
    209 } 
    210  
    211 - (void)appendXmlString:(NSString *)string inbound:(BOOL)isInbound 
    212 { 
    213         if (m_enabled) { 
    214                 NSTextStorage   *textStorage = [m_xmlTextView textStorage]; 
    215                 BOOL                    wasScrolledToBottom = (NSMaxY([m_xmlTextView visibleRect]) >= 
    216                                                                                            (NSMaxY([m_xmlTextView bounds]) - 30.0)); 
    217                  
    218                 NSFont          *font = [NSFont userFixedPitchFontOfSize:10.0]; 
    219                 NSString        *colorKey = (isInbound ? @"ChatFriendColor" : @"ChatMyColor"); 
    220                 NSData          *colorData = [[NSUserDefaults standardUserDefaults] dataForKey:colorKey]; 
    221                 NSColor         *color = [NSUnarchiver unarchiveObjectWithData:colorData]; 
    222                  
    223                 if (![string isEqualToString:@""]) { 
    224                         // Create the attributed string. 
    225                         NSAttributedString *attributedXml = [NSAttributedString attributedStringFromString:string  
    226                                                                                                                                                                                   font:font  
    227                                                                                                                                                                                  color:color]; 
    228                         // Append the string, with a linebreak or two. 
    229                         [textStorage beginEditing]; 
    230                         [textStorage appendAttributedString:[NSAttributedString attributedStringFromString:@"\n\n"]]; 
    231                         [textStorage appendAttributedString:attributedXml]; 
    232                         [textStorage endEditing]; 
    233                 } 
    234                 else { 
    235                         NSLog(@"WARNING: Console encountered empty string."); 
    236                 } 
     210        if (m_enabled != flag) { 
     211                m_enabled = flag; 
     212                 
     213                 
     214                NSTextStorage *textStorage = [m_xmlTextView textStorage]; 
     215                BOOL wasScrolledToBottom = (NSMaxY([m_xmlTextView visibleRect]) >= (NSMaxY([m_xmlTextView bounds]) - 30.0)); 
     216                NSFont *font = [NSFont userFixedPitchFontOfSize:10.0]; 
     217                 
     218                [textStorage beginEditing]; 
     219                { 
     220                        // Dump the contents of the recent XML stanzas buffer to the console 
     221                        if (m_enabled && [m_recentXMLStanzasBuffer count] > 0) { 
     222                                NSString *bufferDumpHeaderStr = @"\n\n<!-- ***** Dumping some saved recent XML messages: ***** -->\n"; 
     223                                [textStorage appendAttributedString:[NSAttributedString attributedStringFromString:bufferDumpHeaderStr 
     224                                                                                                                                                                                          font:font 
     225                                                                                                                                                                                         color:[NSColor darkGrayColor]]]; 
     226                                 
     227                                NSEnumerator *attribStrEnum = [m_recentXMLStanzasBuffer objectEnumerator]; 
     228                                NSAttributedString *attribString = nil; 
     229                                while (attribString = [attribStrEnum nextObject]) { 
     230                                        [textStorage appendAttributedString:attribString]; 
     231                                } 
     232                        } 
     233                         
     234                        NSString *liveDumpHeaderStr = [NSString stringWithFormat:@"\n\n<!-- ***** LIVE dump %@ at %@ ***** -->\n", 
     235                                                                                   (m_enabled ? @"STARTED" : @"STOPPED"), [NSDate date]]; 
     236                         
     237                        [textStorage appendAttributedString:[NSAttributedString attributedStringFromString:liveDumpHeaderStr 
     238                                                                                                                                                                                  font:font 
     239                                                                                                                                                                                 color:[NSColor darkGrayColor]]]; 
     240                } 
     241                [textStorage endEditing]; 
    237242                 
    238243                // Auto-Scroll to the bottom of the content view, but only if we were already at the bottom. 
     
    240245                        [m_xmlTextView scrollRangeToVisible:NSMakeRange([textStorage length], 0)]; 
    241246                } 
     247                 
     248                if (m_enabled) { 
     249                        [m_recentXMLStanzasBuffer removeAllObjects]; 
     250                } 
     251        } 
     252} 
     253 
     254- (NSAttributedString *)attributedStringForConsoleFromXMLString:(NSString *)xmlString inbound:(BOOL)isInbound 
     255{ 
     256        NSMutableAttributedString *resultingAttribStr = [[NSMutableAttributedString alloc] init]; 
     257         
     258        NSFont          *font = [NSFont userFixedPitchFontOfSize:10.0]; 
     259        NSString        *colorKey = (isInbound ? @"ChatFriendColor" : @"ChatMyColor"); 
     260        NSData          *colorData = [[NSUserDefaults standardUserDefaults] dataForKey:colorKey]; 
     261        NSColor         *color = [NSUnarchiver unarchiveObjectWithData:colorData]; 
     262         
     263        if ([xmlString length] > 0) { 
     264                // Create the attributed strings 
     265                NSString *dateTagStr = [NSString stringWithFormat:@"\n\n<!-- %@ at %@ -->\n", 
     266                                                                (isInbound ? @"Received" : @"Sent"), [NSDate date]]; 
     267                NSAttributedString *attributedDateTagStr = [NSAttributedString attributedStringFromString:dateTagStr 
     268                                                                                                                                                                                         font:font 
     269                                                                                                                                                                                        color:[NSColor grayColor]]; 
     270                NSAttributedString *attributedXMLStr = [NSAttributedString attributedStringFromString:xmlString 
     271                                                                                                                                                                                 font:font 
     272                                                                                                                                                                                color:color]; 
     273                 
     274                [resultingAttribStr beginEditing]; 
     275                [resultingAttribStr appendAttributedString:attributedDateTagStr]; 
     276                [resultingAttribStr appendAttributedString:attributedXMLStr]; 
     277                [resultingAttribStr endEditing]; 
     278        } 
     279         
     280        return [resultingAttribStr autorelease]; 
     281} 
     282 
     283#define RECENT_XML_STANZAS_BUFFER_MAX_COUNT             1000 
     284 
     285- (void)appendXMLString:(NSString *)xmlString inbound:(BOOL)isInbound 
     286{ 
     287        NSAttributedString *attributedXMLStr = [self attributedStringForConsoleFromXMLString:xmlString inbound:isInbound]; 
     288         
     289        if (m_enabled) { 
     290                NSTextStorage *textStorage = [m_xmlTextView textStorage]; 
     291                BOOL wasScrolledToBottom = (NSMaxY([m_xmlTextView visibleRect]) >= (NSMaxY([m_xmlTextView bounds]) - 30.0)); 
     292                 
     293                [textStorage appendAttributedString:attributedXMLStr]; 
     294                 
     295                // Auto-Scroll to the bottom of the content view, but only if we were already at the bottom. 
     296                if (wasScrolledToBottom) { 
     297                        [m_xmlTextView scrollRangeToVisible:NSMakeRange([textStorage length], 0)]; 
     298                } 
     299        } 
     300        else { 
     301                // Save it on our buffer for the most recent XML stanzas 
     302                [m_recentXMLStanzasBuffer addObject:attributedXMLStr]; 
     303                 
     304                NSUInteger newCount = [m_recentXMLStanzasBuffer count]; 
     305                 
     306                if (newCount > RECENT_XML_STANZAS_BUFFER_MAX_COUNT) { 
     307                        [m_recentXMLStanzasBuffer removeObjectsInRange:NSMakeRange(0, newCount - RECENT_XML_STANZAS_BUFFER_MAX_COUNT)]; 
     308                } 
    242309        } 
    243310} 
     
    265332- (void)accountDidSendOrReceiveXMLString:(NSNotification *)notification 
    266333{ 
    267         [self appendXmlString:[[notification userInfo] objectForKey:LPXMLString] 
     334        [self appendXMLString:[[notification userInfo] objectForKey:LPXMLString] 
    268335                                  inbound:[[notification name] isEqualToString:LPAccountDidReceiveXMLStringNotification]]; 
    269336}