zoukankan      html  css  js  c++  java
  • 用以替换系统NSLog的YouXianMingLog

    用以替换系统NSLog的YouXianMingLog

    这是本人自己使用并改良的用以替换系统NSLog的类,非常好用,以下是使用示例,现在开源出来并提供源码,好用的话顶一下吧^_^

    效果:

    YouXianMingLog.h 与 YouXianMingLog.m

    //
    //  YouXianMingLog.h
    //
    //  http://home.cnblogs.com/u/YouXianMing/
    //  https://github.com/YouXianMing
    //
    //  Created by YouXianMing on 15/1/3.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    #define ON   1
    #define OFF  0
    
    ///////////////   CONFIG   /////////////////
    
    #define NO_LOG          OFF   // 禁用Log
    #define SWITCH_LOG      ON    // 切换打印
    
    #define TIME_STAMP      OFF   // 时间戳
    #define FILE_NAME       OFF   // 文件名
    
    ////////////////////////////////////////////
    
    #if SWITCH_LOG
    #if NO_LOG
    #define NSLog(args...)
    #else
    #define NSLog(args...) ExtendNSLog(__FILE__,__LINE__,__PRETTY_FUNCTION__,args);
    #endif
    #endif
    
    void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...);
    //
    //  YouXianMingLog.m
    //
    //  http://home.cnblogs.com/u/YouXianMing/
    //  https://github.com/YouXianMing
    //
    //  Created by YouXianMing on 15/1/3.
    //  Copyright (c) 2015年 YouXianMing. All rights reserved.
    //
    
    #import "YouXianMingLog.h"
    
    #ifndef GCDExecOnce
    #define GCDExecOnce(block) 
    { 
    static dispatch_once_t predicate = 0; 
    dispatch_once(&predicate, block); 
    }
    #endif
    
    #define DMDEBUG_DEFAULT_DATEFORMAT @"HH:mm:ss.SSS"
    
    static NSDateFormatter* _DMLogDateFormatter = nil;
    
    void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...)
    {
        // 获取时间格式
        GCDExecOnce(^{
            _DMLogDateFormatter = [[NSDateFormatter alloc] init];
            [_DMLogDateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
            [_DMLogDateFormatter setDateFormat:DMDEBUG_DEFAULT_DATEFORMAT];
        });
        
    #if TIME_STAMP
        // 获取当前时间戳
        const char *nowCString = [[_DMLogDateFormatter stringFromDate:[NSDate date]] cStringUsingEncoding:NSUTF8StringEncoding];
        
        // 处理format
        va_list ap;
        va_start (ap, format);
        if (![format hasSuffix: @"
    "]) {
            format = [format stringByAppendingString: @"
    "];
        }
        NSString *body = [[NSString alloc] initWithFormat:format arguments:ap];
        va_end (ap);
        
    #if FILE_NAME
        // 获取一些参数
        NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent];
        
        // 打印
        fprintf(stderr, "%s %s:%d %s", nowCString, [fileName UTF8String], lineNumber, [body UTF8String]);
    #else
        fprintf(stderr, "%s %s", nowCString, [body UTF8String]);
    #endif
        
    #else
        // 处理format
        va_list ap;
        va_start (ap, format);
        if (![format hasSuffix: @"
    "]) {
            format = [format stringByAppendingString: @"
    "];
        }
        
        NSString *body = [[NSString alloc] initWithFormat:format arguments:ap];
        va_end (ap);
        
    
        
    #if FILE_NAME
        // 获取一些参数
        NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent];
        
        // 打印
        fprintf(stderr, "%s:%d %s", [fileName UTF8String], lineNumber, [body UTF8String]);
    #else 
        fprintf(stderr, "%s", [body UTF8String]);
    #endif
        
    #endif
    }

    以下是配置的地方:

  • 相关阅读:
    if __name__ == '__main__' 该如何理解
    Github下载慢和下载过程中断等情况的解决方案
    Git下载安装及设置详细教程
    冒烟测试是什么?
    100道MySQL数据库经典面试题解析
    Linux 下ZooKeeper安装
    运用Docker+Jenkins+Nginx+Spring Boot 自动化部署项目
    linux本地远程上传&下载阿里云oss的shell脚本实现方法
    运行java项目shell简洁脚本
    阿里云oss利用工具上传图片文件
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/4199172.html
Copyright © 2011-2022 走看看