IOS:定义自己的Log函数

本文主要是为了表示怎么实现自己的Log方法,方便在Release版本删除log信息。

代码主要来自:

iOS Recipes: Tips and Tricks for Awesome iPhone and iPad Apps

1.  PRPDebug.m
//
//  PRPDebug.m
//
//  Created by andy on 7/3/13.
//
//

#import "PRPDebug.h"

void PRPDebug(const char *fileName, int lineNumber, NSString *fmt, ...)
{
    va_list args;

    va_start(args, fmt);

    static NSDateFormatter *debugFormatter = nil;

    if (debugFormatter == nil) {
        debugFormatter = [[NSDateFormatter alloc] init];
        [debugFormatter setDateFormat:@"yyyyMMdd.HH:mm:ss"];
    }

    NSString *msg = [[NSString alloc] initWithFormat:fmt arguments:args];

    NSString        *filePath = [[NSString alloc] initWithUTF8String:fileName];
    NSString        *timestamp = [debugFormatter stringFromDate:[NSDate date]];
    NSDictionary    *info = [[NSBundle mainBundle] infoDictionary];
    NSString        *appName = [info objectForKey:(NSString *)kCFBundleNameKey];
    fprintf(stdout, "%s %s[%s:%d] %s\n", [timestamp UTF8String], [appName UTF8String], [[filePath lastPathComponent] UTF8String], lineNumber, [msg UTF8String]);

    va_end(args);
    [msg release];
    [filePath release];
}

2. PRPDebug.h

//
//  PRPDebug.h
//
//  Created by andy on 7/3/13.
//
//

#ifdef PRPDEBUG
#define DLog(format...) PRPDebug(__FILE__,__LINE__,format)
#else
#define DLog(format...)
#endif

#import <Foundation/Foundation.h>

void PRPDebug(const char *fileName, int lineNumber, NSString *fmt, ...);

3. 在Debug配置中增加PRPDEBUG 选项

Project Navigator选择项目,Build Settings中搜索 Other C Flags,在Debug 部分增加 -DPRPDEBUG 选项。



4. 包含头文件

#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#import "PRPDebug.h" 

#endif
5. 调用方法
DLog(@"Hello");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值