【iOS】XcodeColors插件与CocoaLumberjack工具

工欲善其事必先利其器,好的开发者一定是懂得利用工具来提高自己的效率的,Xcode有很多第三方插件可以使用,最近发现一个可以给控制台着色的工具XcodeColors,结合CocoaLumberjack一起使用可以让控制台有选择的输出信息,可以把输出到控制台的信息分为多种类型,在不同场景下输出不同的信息

下面记录一下如何使用XcodeColors / CocoaLumberjack 和过程中遇到的问题

 

一、XcodeColors

  用于控制台着色的工具,地址在https://github.com/robbiehanson/XcodeColors,下载完直接安装运行工程 XcodeColors 即可,然后运行 TestXcodeColors 检查是否安装成功,如果在控制台打印出有颜色的文本,说明安装成功,如下图

  插件安装完成后可以在Xcode的插件目录下找到,目录如下

~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/XcodeColors.xcplugin



  自带的project只有一个mac工程,下面我们自己验证一下在iOS项目下是否也能正常使用,新建一个Target:TestiOSXCodeColors,依葫芦画瓢测试代码也写在Appdelegate.m文件里面

#import "AppDelegate.h"

#define XCODE_COLORS_ESCAPE_MAC @"\033["
#define XCODE_COLORS_ESCAPE_IOS @"\xC2\xA0["

#if TARGET_OS_IPHONE
#define XCODE_COLORS_ESCAPE  XCODE_COLORS_ESCAPE_IOS
#else
#define XCODE_COLORS_ESCAPE  XCODE_COLORS_ESCAPE_MAC
#endif

#define XCODE_COLORS_RESET_FG  XCODE_COLORS_ESCAPE @"fg;" // Clear any foreground color
#define XCODE_COLORS_RESET_BG  XCODE_COLORS_ESCAPE @"bg;" // Clear any background color
#define XCODE_COLORS_RESET     XCODE_COLORS_ESCAPE @";"   // Clear any foreground or background color

#define LogBlue(frmt, ...) NSLog((XCODE_COLORS_ESCAPE @"fg0,0,255;" frmt XCODE_COLORS_RESET), ##__VA_ARGS__)


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    
    NSLog(@"After building the XcodeColors plugin for the first time, you MUST RESTART XCODE.");
    NSLog(@"If you still don't see colors below, please consult the README.");
    
    NSLog(XCODE_COLORS_ESCAPE @"fg0,0,255;" @"Blue text" XCODE_COLORS_RESET);
    
    NSLog(XCODE_COLORS_ESCAPE @"bg220,0,0;" @"Red background" XCODE_COLORS_RESET);
    
    NSLog(XCODE_COLORS_ESCAPE @"fg0,0,255;"
          XCODE_COLORS_ESCAPE @"bg220,0,0;"
          @"Blue text on red background"
          XCODE_COLORS_RESET);
    
    NSLog(XCODE_COLORS_ESCAPE @"fg209,57,168;" @"You can supply your own RGB values!" XCODE_COLORS_RESET);
    
    LogBlue(@"Blue text via macro");

    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
AppDelegate.m

  结果发现在iOS项目下颜色不但没有奏效,并且把其他部分的信息页打印出来了

  观察代码发现,不同地方在与

#define XCODE_COLORS_ESCAPE_MAC @"\033["
#define XCODE_COLORS_ESCAPE_IOS @"\xC2\xA0["

  尝试把 XCODE_COLORS_ESCAPE_IOS 也换成 XCODE_COLORS_ESCAPE_MAC 结果就可以正常显示

#define XCODE_COLORS_ESCAPE_MAC @"\033["
#define XCODE_COLORS_ESCAPE_IOS XCODE_COLORS_ESCAPE_MAC

  不知道为何 XCODE_COLORS_ESCAPE_IOS 的值不能生效,如果要使用的就直接用MAC那个宏 

  还有一种更为简单的方法安装XcodeColors,通过 Alcatraz 包管理器,直接搜索安装

 

 

二、CocoaLumberjack

  CocoaLumberjack是Mac和iOS上一个集快捷、简单、强大和灵活于一身的日志框架,有点像java的log4j,CocoaLumberjack 对提供了很强大的输出日志的功能,不仅可以输出信息到控制台,还可以输出到苹果的日志系统,文件,关于CocoaLumberjack的详细使用,参见官网的说明

  下面我们看看控制台着色是否正常:

  1、新建一个工程

  2、通过 cocoapods 加入 CocoaLumberjack

  3、测试

    //添加控制台输出Logger
    [DDLog addLogger:[DDTTYLogger sharedInstance] withLevel:DDLogLevelAll];
//允许着色
    [[DDTTYLogger sharedInstance] setColorsEnabled:YES];
    
    //输出信息
    DDLogError(@"错误信息");    //红色
    DDLogWarn(@"警告");        //橙色
    DDLogInfo(@"提示信息");     //默认颜色
    DDLogDebug(@"调试信息");    //默认颜色
    DDLogVerbose(@"详细信息");  //默认颜色

  运行发现,控制台信息并没有着色

  一番搜索发现,需要配置Xcode环境 XcodeColors才会生效

添加环境变量 XcodeColorsYES

  重新运行,成功

  我们把输出等级设为 DDLogLevelWarning,只输出警告信息和错误信息

 

  CocoaLumberjack 还支持输出不同类型的信息,还支持配置输出到控制台的颜色,这样我们就可以只在控制台输出错误信息,只输出警告信息等,更多功能参见网站

 

  Demo:https://files.cnblogs.com/files/bomo/CocoaLumberjackTest.zip

posted @ 2015-07-23 20:20  bomo  阅读(1428)  评论(0编辑  收藏  举报