2007-09-16 01:02:06 +00:00
|
|
|
/******************************************************************************
|
2012-01-14 17:12:04 +00:00
|
|
|
* Copyright (c) 2006-2012 Transmission authors and contributors
|
2007-09-16 01:02:06 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2018-09-30 10:37:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/log.h>
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "MessageWindowController.h"
|
2012-05-27 22:31:58 +00:00
|
|
|
#import "Controller.h"
|
2009-10-21 13:01:14 +00:00
|
|
|
#import "NSApplicationAdditions.h"
|
2011-07-21 02:36:27 +00:00
|
|
|
#import "NSMutableArrayAdditions.h"
|
2008-03-15 20:17:19 +00:00
|
|
|
#import "NSStringAdditions.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
#define LEVEL_ERROR 0
|
2021-08-15 09:41:48 +00:00
|
|
|
#define LEVEL_INFO 1
|
2007-09-16 01:02:06 +00:00
|
|
|
#define LEVEL_DEBUG 2
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
#define UPDATE_SECONDS 0.75
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
@interface MessageWindowController (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resizeColumn;
|
|
|
|
- (BOOL)shouldIncludeMessageForFilter:(NSString*)filterString message:(NSDictionary*)message;
|
|
|
|
- (void)updateListForFilter;
|
|
|
|
- (NSString*)stringForMessage:(NSDictionary*)message;
|
2007-09-16 01:02:06 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MessageWindowController
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (instancetype)init
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
return [super initWithWindowNibName:@"MessageWindow"];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSWindow* window = self.window;
|
2021-08-07 07:27:56 +00:00
|
|
|
window.frameAutosaveName = @"MessageWindowFrame";
|
2021-08-15 09:41:48 +00:00
|
|
|
[window setFrameUsingName:@"MessageWindowFrame"];
|
2021-08-07 07:27:56 +00:00
|
|
|
window.restorationClass = [self class];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(resizeColumn)
|
|
|
|
name:NSTableViewColumnDidResizeNotification
|
|
|
|
object:fMessageTable];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[window setContentBorderThickness:NSMinY(fMessageTable.enclosingScrollView.frame) forEdge:NSMinYEdge];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.window.title = NSLocalizedString(@"Message Log", "Message window -> title");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-15 20:17:19 +00:00
|
|
|
//set images and text for popup button items
|
2021-08-15 09:41:48 +00:00
|
|
|
[fLevelButton itemAtIndex:LEVEL_ERROR].title = NSLocalizedString(@"Error", "Message window -> level string");
|
|
|
|
[fLevelButton itemAtIndex:LEVEL_INFO].title = NSLocalizedString(@"Info", "Message window -> level string");
|
|
|
|
[fLevelButton itemAtIndex:LEVEL_DEBUG].title = NSLocalizedString(@"Debug", "Message window -> level string");
|
2017-01-04 21:47:27 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const levelButtonOldWidth = NSWidth(fLevelButton.frame);
|
2009-12-13 21:56:51 +00:00
|
|
|
[fLevelButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-15 20:17:19 +00:00
|
|
|
//set table column text
|
2021-08-15 09:41:48 +00:00
|
|
|
[fMessageTable tableColumnWithIdentifier:@"Date"].headerCell.title = NSLocalizedString(@"Date", "Message window -> table column");
|
|
|
|
[fMessageTable tableColumnWithIdentifier:@"Name"].headerCell.title = NSLocalizedString(@"Process", "Message window -> table column");
|
|
|
|
[fMessageTable tableColumnWithIdentifier:@"Message"].headerCell.title = NSLocalizedString(@"Message", "Message window -> table column");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-03-15 20:17:19 +00:00
|
|
|
//set and size buttons
|
2021-08-07 07:27:56 +00:00
|
|
|
fSaveButton.title = [NSLocalizedString(@"Save", "Message window -> save button") stringByAppendingEllipsis];
|
2008-03-15 20:17:19 +00:00
|
|
|
[fSaveButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect saveButtonFrame = fSaveButton.frame;
|
2008-03-15 20:17:19 +00:00
|
|
|
saveButtonFrame.size.width += 10.0;
|
2021-08-07 07:27:56 +00:00
|
|
|
saveButtonFrame.origin.x += NSWidth(fLevelButton.frame) - levelButtonOldWidth;
|
|
|
|
fSaveButton.frame = saveButtonFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const oldClearButtonWidth = fClearButton.frame.size.width;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
fClearButton.title = NSLocalizedString(@"Clear", "Message window -> save button");
|
2008-03-15 20:17:19 +00:00
|
|
|
[fClearButton sizeToFit];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect clearButtonFrame = fClearButton.frame;
|
2008-03-15 20:17:19 +00:00
|
|
|
clearButtonFrame.size.width = MAX(clearButtonFrame.size.width + 10.0, saveButtonFrame.size.width);
|
2010-02-02 01:13:15 +00:00
|
|
|
clearButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
|
2021-08-07 07:27:56 +00:00
|
|
|
fClearButton.frame = clearButtonFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[fFilterField.cell setPlaceholderString:NSLocalizedString(@"Filter", "Message window -> filter field")];
|
2021-08-07 07:27:56 +00:00
|
|
|
NSRect filterButtonFrame = fFilterField.frame;
|
2010-02-02 01:13:15 +00:00
|
|
|
filterButtonFrame.origin.x -= NSWidth(clearButtonFrame) - oldClearButtonWidth;
|
2021-08-07 07:27:56 +00:00
|
|
|
fFilterField.frame = filterButtonFrame;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
fAttributes = [[[fMessageTable tableColumnWithIdentifier:@"Message"].dataCell attributedStringValue] attributesAtIndex:0
|
|
|
|
effectiveRange:NULL];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
//select proper level in popup button
|
2021-08-15 09:41:48 +00:00
|
|
|
switch ([NSUserDefaults.standardUserDefaults integerForKey:@"MessageLevel"])
|
2007-10-11 12:06:46 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_LOG_ERROR:
|
|
|
|
[fLevelButton selectItemAtIndex:LEVEL_ERROR];
|
|
|
|
break;
|
|
|
|
case TR_LOG_INFO:
|
|
|
|
[fLevelButton selectItemAtIndex:LEVEL_INFO];
|
|
|
|
break;
|
|
|
|
case TR_LOG_DEBUG:
|
|
|
|
[fLevelButton selectItemAtIndex:LEVEL_DEBUG];
|
|
|
|
break;
|
|
|
|
default: //safety
|
|
|
|
[NSUserDefaults.standardUserDefaults setInteger:TR_LOG_ERROR forKey:@"MessageLevel"];
|
|
|
|
[fLevelButton selectItemAtIndex:LEVEL_ERROR];
|
2007-10-11 12:06:46 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-12-24 16:56:43 +00:00
|
|
|
fMessages = [[NSMutableArray alloc] init];
|
2009-11-28 20:39:31 +00:00
|
|
|
fDisplayedMessages = [[NSMutableArray alloc] init];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-28 21:01:27 +00:00
|
|
|
fLock = [[NSLock alloc] init];
|
2007-12-24 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2011-07-21 02:36:27 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSNotificationCenter.defaultCenter removeObserver:self];
|
2011-07-21 02:36:27 +00:00
|
|
|
[fTimer invalidate];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)windowDidBecomeKey:(NSNotification*)notification
|
2007-12-24 16:56:43 +00:00
|
|
|
{
|
|
|
|
if (!fTimer)
|
2009-12-11 04:01:47 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
fTimer = [NSTimer scheduledTimerWithTimeInterval:UPDATE_SECONDS target:self selector:@selector(updateLog:) userInfo:nil
|
|
|
|
repeats:YES];
|
|
|
|
[self updateLog:nil];
|
2009-12-11 04:01:47 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)windowWillClose:(id)sender
|
2007-12-24 16:56:43 +00:00
|
|
|
{
|
|
|
|
[fTimer invalidate];
|
|
|
|
fTimer = nil;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
+ (void)restoreWindowWithIdentifier:(NSString*)identifier
|
|
|
|
state:(NSCoder*)state
|
|
|
|
completionHandler:(void (^)(NSWindow*, NSError*))completionHandler
|
2012-05-27 22:31:58 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAssert1([identifier isEqualToString:@"MessageWindow"], @"Trying to restore unexpected identifier %@", identifier);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSWindow* window = ((Controller*)NSApp.delegate).messageWindowController.window;
|
2012-05-27 22:31:58 +00:00
|
|
|
completionHandler(window, nil);
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)window:(NSWindow*)window didDecodeRestorableState:(NSCoder*)coder
|
2012-05-27 22:31:58 +00:00
|
|
|
{
|
|
|
|
[fTimer invalidate];
|
2021-08-15 09:41:48 +00:00
|
|
|
fTimer = [NSTimer scheduledTimerWithTimeInterval:UPDATE_SECONDS target:self selector:@selector(updateLog:) userInfo:nil
|
|
|
|
repeats:YES];
|
|
|
|
[self updateLog:nil];
|
2012-05-27 22:31:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateLog:(NSTimer*)timer
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_log_message* messages;
|
2013-01-25 23:34:20 +00:00
|
|
|
if ((messages = tr_logGetQueue()) == NULL)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2007-09-16 01:02:06 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-28 21:01:27 +00:00
|
|
|
[fLock lock];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-12-10 13:06:04 +00:00
|
|
|
static NSUInteger currentIndex = 0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSScroller* scroller = fMessageTable.enclosingScrollView.verticalScroller;
|
|
|
|
BOOL const shouldScroll = currentIndex == 0 || scroller.floatValue == 1.0 || scroller.hidden || scroller.knobProportion == 1.0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger const maxLevel = [NSUserDefaults.standardUserDefaults integerForKey:@"MessageLevel"];
|
|
|
|
NSString* filterString = fFilterField.stringValue;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
BOOL changed = NO;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (tr_log_message* currentMessage = messages; currentMessage != NULL; currentMessage = currentMessage->next)
|
2007-12-08 19:59:44 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* name = currentMessage->name != NULL ? @(currentMessage->name) : NSProcessInfo.processInfo.processName;
|
|
|
|
|
|
|
|
NSString* file = [(@(currentMessage->file)).lastPathComponent stringByAppendingFormat:@":%d", currentMessage->line];
|
|
|
|
|
|
|
|
NSDictionary* message = @{
|
|
|
|
@"Message" : @(currentMessage->message),
|
|
|
|
@"Date" : [NSDate dateWithTimeIntervalSince1970:currentMessage->when],
|
|
|
|
@"Index" : @(currentIndex++), //more accurate when sorting by date
|
|
|
|
@"Level" : @(currentMessage->level),
|
|
|
|
@"Name" : name,
|
|
|
|
@"File" : file
|
|
|
|
};
|
|
|
|
[fMessages addObject:message];
|
|
|
|
|
|
|
|
if (currentMessage->level <= maxLevel && [self shouldIncludeMessageForFilter:filterString message:message])
|
2009-11-28 20:39:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[fDisplayedMessages addObject:message];
|
2009-11-28 20:39:31 +00:00
|
|
|
changed = YES;
|
|
|
|
}
|
2007-12-08 19:59:44 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
if (fMessages.count > TR_LOG_MAX_QUEUE_LENGTH)
|
2009-11-28 20:39:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSUInteger const oldCount = fDisplayedMessages.count;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSIndexSet* removeIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fMessages.count - TR_LOG_MAX_QUEUE_LENGTH)];
|
|
|
|
NSArray* itemsToRemove = [fMessages objectsAtIndexes:removeIndexes];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[fMessages removeObjectsAtIndexes:removeIndexes];
|
|
|
|
[fDisplayedMessages removeObjectsInArray:itemsToRemove];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
changed |= oldCount > fDisplayedMessages.count;
|
2009-12-02 01:35:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-12-02 01:35:48 +00:00
|
|
|
if (changed)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[fDisplayedMessages sortUsingDescriptors:fMessageTable.sortDescriptors];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
[fMessageTable reloadData];
|
|
|
|
if (shouldScroll)
|
2021-08-15 09:41:48 +00:00
|
|
|
[fMessageTable scrollRowToVisible:fMessageTable.numberOfRows - 1];
|
2009-11-28 20:39:31 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-28 21:01:27 +00:00
|
|
|
[fLock unlock];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
tr_logFreeQueue(messages);
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSInteger)numberOfRowsInTableView:(NSTableView*)tableView
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
return fDisplayedMessages.count;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (id)tableView:(NSTableView*)tableView objectValueForTableColumn:(NSTableColumn*)column row:(NSInteger)row
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* ident = column.identifier;
|
|
|
|
NSDictionary* message = fDisplayedMessages[row];
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([ident isEqualToString:@"Date"])
|
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
return message[@"Date"];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
|
|
|
else if ([ident isEqualToString:@"Level"])
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger const level = [message[@"Level"] integerValue];
|
2009-10-11 16:25:07 +00:00
|
|
|
switch (level)
|
2007-10-01 21:52:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_LOG_ERROR:
|
|
|
|
return [NSImage imageNamed:@"RedDotFlat"];
|
|
|
|
case TR_LOG_INFO:
|
|
|
|
return [NSImage imageNamed:@"YellowDotFlat"];
|
|
|
|
case TR_LOG_DEBUG:
|
|
|
|
return [NSImage imageNamed:@"PurpleDotFlat"];
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown message log level: %ld", level);
|
|
|
|
return nil;
|
2007-10-01 21:52:31 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
else if ([ident isEqualToString:@"Name"])
|
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
return message[@"Name"];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
else
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2017-07-08 14:38:47 +00:00
|
|
|
return message[@"Message"];
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 04:27:55 +00:00
|
|
|
#warning don't cut off end
|
2021-08-15 09:41:48 +00:00
|
|
|
- (CGFloat)tableView:(NSTableView*)tableView heightOfRow:(NSInteger)row
|
2007-10-28 03:10:11 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* message = fDisplayedMessages[row][@"Message"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSTableColumn* column = [tableView tableColumnWithIdentifier:@"Message"];
|
|
|
|
CGFloat const count = floorf([message sizeWithAttributes:fAttributes].width / column.width);
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
return tableView.rowHeight * (count + 1.0);
|
2007-10-28 03:10:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)tableView:(NSTableView*)tableView sortDescriptorsDidChange:(NSArray*)oldDescriptors
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[fDisplayedMessages sortUsingDescriptors:fMessageTable.sortDescriptors];
|
2007-09-16 01:02:06 +00:00
|
|
|
[fMessageTable reloadData];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)tableView:(NSTableView*)tableView
|
|
|
|
toolTipForCell:(NSCell*)cell
|
|
|
|
rect:(NSRectPointer)rect
|
|
|
|
tableColumn:(NSTableColumn*)column
|
|
|
|
row:(NSInteger)row
|
|
|
|
mouseLocation:(NSPoint)mouseLocation
|
2007-12-08 20:42:34 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSDictionary* message = fDisplayedMessages[row];
|
2017-07-08 14:38:47 +00:00
|
|
|
return message[@"File"];
|
2007-12-08 20:42:34 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)copy:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSIndexSet* indexes = fMessageTable.selectedRowIndexes;
|
|
|
|
NSMutableArray* messageStrings = [NSMutableArray arrayWithCapacity:indexes.count];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSDictionary* message in [fDisplayedMessages objectsAtIndexes:indexes])
|
|
|
|
{
|
|
|
|
[messageStrings addObject:[self stringForMessage:message]];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* messageString = [messageStrings componentsJoinedByString:@"\n"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSPasteboard* pb = NSPasteboard.generalPasteboard;
|
2011-10-06 00:30:40 +00:00
|
|
|
[pb clearContents];
|
2021-08-15 09:41:48 +00:00
|
|
|
[pb writeObjects:@[ messageString ]];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)validateMenuItem:(NSMenuItem*)menuItem
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
SEL action = menuItem.action;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
if (action == @selector(copy:))
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
return fMessageTable.numberOfSelectedRows > 0;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)changeLevel:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2009-10-11 16:25:07 +00:00
|
|
|
NSInteger level;
|
2021-08-07 07:27:56 +00:00
|
|
|
switch (fLevelButton.indexOfSelectedItem)
|
2007-10-01 21:52:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case LEVEL_ERROR:
|
|
|
|
level = TR_LOG_ERROR;
|
|
|
|
break;
|
|
|
|
case LEVEL_INFO:
|
|
|
|
level = TR_LOG_INFO;
|
|
|
|
break;
|
|
|
|
case LEVEL_DEBUG:
|
|
|
|
level = TR_LOG_DEBUG;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown message log level: %ld", [fLevelButton indexOfSelectedItem]);
|
|
|
|
level = TR_LOG_INFO;
|
2007-10-01 21:52:31 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([NSUserDefaults.standardUserDefaults integerForKey:@"MessageLevel"] == level)
|
|
|
|
{
|
2009-11-28 20:39:31 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[NSUserDefaults.standardUserDefaults setInteger:level forKey:@"MessageLevel"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
[fLock lock];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
[self updateListForFilter];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2010-02-02 01:13:15 +00:00
|
|
|
[fLock unlock];
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)changeFilter:(id)sender
|
2010-02-02 01:13:15 +00:00
|
|
|
{
|
|
|
|
[fLock lock];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
[self updateListForFilter];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-11-28 21:01:27 +00:00
|
|
|
[fLock unlock];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)clearLog:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2009-12-07 03:53:52 +00:00
|
|
|
[fLock lock];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
[fMessages removeAllObjects];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
|
|
|
[fMessageTable beginUpdates];
|
2021-08-15 09:41:48 +00:00
|
|
|
[fMessageTable removeRowsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fDisplayedMessages.count)]
|
|
|
|
withAnimation:NSTableViewAnimationSlideLeft];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
2009-11-28 20:39:31 +00:00
|
|
|
[fDisplayedMessages removeAllObjects];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
|
|
|
[fMessageTable endUpdates];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-12-07 03:53:52 +00:00
|
|
|
[fLock unlock];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)writeToFile:(id)sender
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSSavePanel* panel = [NSSavePanel savePanel];
|
|
|
|
panel.allowedFileTypes = @[ @"txt" ];
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.canSelectHiddenExtension = YES;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
panel.nameFieldStringValue = NSLocalizedString(@"untitled", "Save log panel -> default file name");
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
|
2011-12-11 22:31:01 +00:00
|
|
|
if (result == NSFileHandlingPanelOKButton)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2011-12-11 22:31:01 +00:00
|
|
|
//make the array sorted by date
|
2021-08-15 09:41:48 +00:00
|
|
|
NSSortDescriptor* descriptor = [NSSortDescriptor sortDescriptorWithKey:@"Index" ascending:YES];
|
|
|
|
NSArray* descriptors = @[ descriptor ];
|
|
|
|
NSArray* sortedMessages = [fDisplayedMessages sortedArrayUsingDescriptors:descriptors];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-12-11 22:31:01 +00:00
|
|
|
//create the text to output
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableArray* messageStrings = [NSMutableArray arrayWithCapacity:sortedMessages.count];
|
|
|
|
for (NSDictionary* message in sortedMessages)
|
|
|
|
{
|
|
|
|
[messageStrings addObject:[self stringForMessage:message]];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* fileString = [messageStrings componentsJoinedByString:@"\n"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
if (![fileString writeToFile:panel.URL.path atomically:YES encoding:NSUTF8StringEncoding error:nil])
|
2011-12-11 22:31:01 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAlert* alert = [[NSAlert alloc] init];
|
|
|
|
[alert addButtonWithTitle:NSLocalizedString(@"OK", "Save log alert panel -> button")];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.messageText = NSLocalizedString(@"Log Could Not Be Saved", "Save log alert panel -> title");
|
2021-08-15 09:41:48 +00:00
|
|
|
alert.informativeText = [NSString
|
|
|
|
stringWithFormat:NSLocalizedString(@"There was a problem creating the file \"%@\".", "Save log alert panel -> message"),
|
|
|
|
panel.URL.path.lastPathComponent];
|
2021-08-07 07:27:56 +00:00
|
|
|
alert.alertStyle = NSWarningAlertStyle;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-12-11 22:31:01 +00:00
|
|
|
[alert runModal];
|
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2011-12-11 22:31:01 +00:00
|
|
|
}];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MessageWindowController (Private)
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)resizeColumn
|
2007-10-28 03:10:11 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[fMessageTable noteHeightOfRowsWithIndexesChanged:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, fMessageTable.numberOfRows)]];
|
2007-10-28 03:10:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)shouldIncludeMessageForFilter:(NSString*)filterString message:(NSDictionary*)message
|
2010-02-02 01:13:15 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
if ([filterString isEqualToString:@""])
|
|
|
|
{
|
2010-02-02 01:13:15 +00:00
|
|
|
return YES;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSStringCompareOptions const searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
|
|
|
|
return [message[@"Name"] rangeOfString:filterString options:searchOptions].location != NSNotFound ||
|
|
|
|
[message[@"Message"] rangeOfString:filterString options:searchOptions].location != NSNotFound;
|
2010-02-02 01:13:15 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateListForFilter
|
2011-07-21 02:36:27 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSInteger const level = [NSUserDefaults.standardUserDefaults integerForKey:@"MessageLevel"];
|
|
|
|
NSString* filterString = fFilterField.stringValue;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSIndexSet* indexes = [fMessages indexesOfObjectsWithOptions:NSEnumerationConcurrent
|
|
|
|
passingTest:^BOOL(id message, NSUInteger idx, BOOL* stop) {
|
|
|
|
return [((NSDictionary*)message)[@"Level"] integerValue] <= level &&
|
|
|
|
[self shouldIncludeMessageForFilter:filterString message:message];
|
|
|
|
}];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSArray* tempMessages = [[fMessages objectsAtIndexes:indexes] sortedArrayUsingDescriptors:fMessageTable.sortDescriptors];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
|
|
|
[fMessageTable beginUpdates];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
//figure out which rows were added/moved
|
|
|
|
NSUInteger currentIndex = 0, totalCount = 0;
|
2021-08-15 09:41:48 +00:00
|
|
|
NSMutableArray* itemsToAdd = [NSMutableArray array];
|
|
|
|
NSMutableIndexSet* itemsToAddIndexes = [NSMutableIndexSet indexSet];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
for (NSDictionary* message in tempMessages)
|
2011-07-21 02:36:27 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSUInteger const previousIndex = [fDisplayedMessages
|
|
|
|
indexOfObject:message
|
|
|
|
inRange:NSMakeRange(currentIndex, fDisplayedMessages.count - currentIndex)];
|
2011-07-21 02:36:27 +00:00
|
|
|
if (previousIndex == NSNotFound)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[itemsToAdd addObject:message];
|
|
|
|
[itemsToAddIndexes addIndex:totalCount];
|
2011-07-21 02:36:27 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (previousIndex != currentIndex)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
[fDisplayedMessages moveObjectAtIndex:previousIndex toIndex:currentIndex];
|
|
|
|
[fMessageTable moveRowAtIndex:previousIndex toIndex:currentIndex];
|
2011-07-21 02:36:27 +00:00
|
|
|
}
|
|
|
|
++currentIndex;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
++totalCount;
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
//remove trailing items - those are the unused
|
2021-08-07 07:27:56 +00:00
|
|
|
if (currentIndex < fDisplayedMessages.count)
|
2011-07-21 02:36:27 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSRange const removeRange = NSMakeRange(currentIndex, fDisplayedMessages.count - currentIndex);
|
|
|
|
[fDisplayedMessages removeObjectsInRange:removeRange];
|
|
|
|
[fMessageTable removeRowsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:removeRange]
|
|
|
|
withAnimation:NSTableViewAnimationSlideDown];
|
2011-07-21 02:36:27 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2011-07-21 02:36:27 +00:00
|
|
|
//add new items
|
2021-08-15 09:41:48 +00:00
|
|
|
[fDisplayedMessages insertObjects:itemsToAdd atIndexes:itemsToAddIndexes];
|
|
|
|
[fMessageTable insertRowsAtIndexes:itemsToAddIndexes withAnimation:NSTableViewAnimationSlideUp];
|
2016-01-06 11:05:37 +00:00
|
|
|
|
|
|
|
[fMessageTable endUpdates];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSAssert2([fDisplayedMessages isEqualToArray:tempMessages], @"Inconsistency between message arrays! %@ %@", fDisplayedMessages, tempMessages);
|
2011-07-21 02:36:27 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (NSString*)stringForMessage:(NSDictionary*)message
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSString* levelString;
|
|
|
|
NSInteger const level = [message[@"Level"] integerValue];
|
2009-11-29 02:17:11 +00:00
|
|
|
switch (level)
|
2007-10-01 21:52:31 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
case TR_LOG_ERROR:
|
|
|
|
levelString = NSLocalizedString(@"Error", "Message window -> level");
|
|
|
|
break;
|
|
|
|
case TR_LOG_INFO:
|
|
|
|
levelString = NSLocalizedString(@"Info", "Message window -> level");
|
|
|
|
break;
|
|
|
|
case TR_LOG_DEBUG:
|
|
|
|
levelString = NSLocalizedString(@"Debug", "Message window -> level");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NSAssert1(NO, @"Unknown message log level: %ld", level);
|
|
|
|
levelString = @"?";
|
2007-10-01 21:52:31 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
return [NSString
|
|
|
|
stringWithFormat:@"%@ %@ [%@] %@: %@", message[@"Date"], message[@"File"], levelString, message[@"Name"], message[@"Message"], nil];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|