Skip to content

Instantly share code, notes, and snippets.

@itsjustcon
Created May 14, 2014 18:05
Show Gist options
  • Save itsjustcon/e896f0accb27aa63a1a1 to your computer and use it in GitHub Desktop.
Save itsjustcon/e896f0accb27aa63a1a1 to your computer and use it in GitHub Desktop.
View all your DDLog messages in a nice terminal-style view controller! Don't forget to call: `[DDLog addLogger:myLogViewController]`
//
// SRLogViewController.h
//
// Created by Connor Grady on 4/25/14.
// Copyright (c) 2014 Stadium Runner. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <CocoaLumberjack/DDLog.h>
@interface SRLogViewController : UIViewController <DDLogger>
@property (nonatomic, weak) UITextView *textView;
@property (nonatomic, strong) id<DDLogFormatter> logFormatter;
@property (nonatomic, assign) BOOL autoScrollsToBottom;
@end
//
// SRLogViewController.m
//
// Created by Connor Grady on 4/25/14.
// Copyright (c) 2014 Stadium Runner. All rights reserved.
//
#import "SRLogViewController.h"
#define Red [UIColor colorWithRed:1 green:0 blue:0 alpha:0.75]
#define Green [UIColor colorWithRed:0 green:1 blue:0 alpha:1]
#define Blue [UIColor colorWithRed:0 green:1 blue:1 alpha:1]
#define Yellow [UIColor colorWithRed:1 green:1 blue:0 alpha:0.75]
@implementation SRLogViewController
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (instancetype)init {
self = [super init];
if (self) {
_autoScrollsToBottom = YES;
}
return self;
}
- (void)loadView {
[super loadView];
self.textView.backgroundColor = [UIColor colorWithRed:0.01 green:0.01 blue:0.01 alpha:1];
}
#pragma mark - DDLogger
- (void)logMessage:(DDLogMessage *)logMessage {
NSString *logMsg;
if (self.logFormatter) logMsg = [self.logFormatter formatLogMessage:logMessage];
else logMsg = logMessage->logMsg;
if (logMessage->logLevel == LOG_LEVEL_ERROR)
DDLogError(@"[SRLogViewController] LOG LEVEL ERROR!!!");
logMsg = [NSString stringWithFormat:@"\n%@", logMsg];
NSMutableAttributedString *msg = [[NSMutableAttributedString alloc] initWithString:logMsg];
if (logMessage->logFlag == LOG_FLAG_ERROR) {
[msg insertAttributedString:[[NSAttributedString alloc] initWithString:@"ERROR: "] atIndex:1];
[msg addAttribute:NSForegroundColorAttributeName value:Red range:NSMakeRange(1, msg.length-1)];
[msg addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(1, msg.length-1)];
}
else if (logMessage->logFlag == LOG_FLAG_WARN) {
[msg insertAttributedString:[[NSAttributedString alloc] initWithString:@"WARNING: "] atIndex:1];
[msg addAttribute:NSForegroundColorAttributeName value:Yellow range:NSMakeRange(1, msg.length-1)];
[msg addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSMakeRange(1, msg.length-1)];
}
else if (logMessage->logFlag == LOG_FLAG_INFO)
[msg addAttribute:NSForegroundColorAttributeName value:Blue range:NSMakeRange(1, msg.length-1)];
else if (logMessage->logFlag == LOG_FLAG_DEBUG)
[msg addAttribute:NSForegroundColorAttributeName value:Yellow range:NSMakeRange(1, msg.length-1)];
else
[msg addAttribute:NSForegroundColorAttributeName value:Green range:NSMakeRange(1, msg.length-1)];
ExecuteOnMainThread(^{
NSMutableAttributedString *text = [self.textView.attributedText mutableCopy];
[text appendAttributedString:msg];
self.textView.attributedText = text;
// Gets reset each time... annoying
self.textView.font = [UIFont fontWithName:@"Courier" size:12];
if (self.autoScrollsToBottom)
[self.textView scrollRangeToVisible:NSMakeRange(self.textView.text.length, 0)];
});
}
#pragma mark - Getters
- (UITextView *)textView {
if (!_textView) {
UITextView *textView = [UITextView new];
textView.alwaysBounceVertical = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
textView.editable = NO;
textView.frame = self.view.bounds;
//textView.textColor = Green;
[self.view addSubview: _textView = textView ];
}
return _textView;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment