zoukankan      html  css  js  c++  java
  • ios自定义NSLog的输出内容

    本文章转发自:http://my.oschina.net/juwenz/blog/178781

    问题

    NSLog输出的信息很少,格式如下

    Date Time OurApp[<processid>] NSLog output
    

    数据可能像这样

    20131111 12:35:35.025 TestApp[460:c07] Hello world
    

    我们希望输出更多的内容,比如文件名,方法名,代码行数等,简单来说就像下面这种格式

    (ClassName MethodName) (SourceFileName:LineNumber NSLog output
    

    解决方法

    步骤

    1. 新建一个类,命名为ExtendNSLogFunctionality;

    2. 打开ExtendNSLogFunctionality.h,加入下面代码

    #ifdef DEBUG
    #define NSLog(args...)  ExtendNSLog(__FILE__,__LINE__,__PRETTY_FUNCTION__,args);
    #else
    #define NSLog(x...)
    #endif
    
    void   ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...);

      3.打开ExtendNSLogFunctionality.m , 加入下面代码

     1 void ExtendNSLog(const char *file, int lineNumber, const char *functionName, NSString *format, ...)
     2 {
     3     // Type to hold information about variable arguments.
     4     va_list ap;
     5     // Initialize a variable argument list.
     6     va_start (ap, format);
     7     // NSLog only adds a newline to the end of the NSLog format if
     8     // one is not already there.
     9     // Here we are utilizing this feature of NSLog()
    10     if (![format hasSuffix: @"
    "])
    11     {
    12         format = [format stringByAppendingString: @"
    "];
    13     }
    14     NSString *body = [[NSString alloc] initWithFormat:format arguments:ap];
    15 // End using variable argument list.
    16     va_end (ap);
    17     NSString *fileName = [[NSString stringWithUTF8String:file] lastPathComponent];
    18     fprintf(stderr, "(%s) (%s:%d) %s",
    19             functionName, [fileName UTF8String],
    20             lineNumber, [body UTF8String]);
    21 }

    4.把ExtendNSLogFunctionality.h 加入到Prefix.pch 的OBJCsection中

    1 #ifdef __OBJC__
    2 #import <UIKit/UIKit.h>
    3 #import <Foundation/Foundation.h>
    4 #import "ExtendNSLogFunctionality.h"
    5 #endif

    测试结果

    在某个文件的某个方法中NSLog了一下,结果如下

    (-[TestNSLogVC resetSize]) (TestNSLogVC.m:78) height : 460.000000 width:320.000000
    IOS开发
  • 相关阅读:
    Vue里用moment.js
    回到顶部|回到底部功能的实现(Vue)
    Vue绑定下拉框型的树
    Excel导入数据库前后端代码
    Windows应用程序开发笔记-控制和获取其他程序窗口控件内容
    SQL Server查询、添加、修改表和字段的备注描述
    windows 共享文件夹,和共享打印机
    Visual Studio 2015安装过程卡住,解决办法
    python绘图 初识Python绘图
    GCC、LLVM、Clang
  • 原文地址:https://www.cnblogs.com/luanmage/p/4607289.html
Copyright © 2011-2022 走看看