本文介绍了iPhone:在生产中抑制NSLog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一堆NSLog语句。无论如何,我可以禁止他们在生产中执行,但在我开发时执行?我不想评论它们,并且在开发时将它们放回去,因为它很麻烦。

I got a bunch of NSLog statements in my code. Is there anyway I can suppress them from executing in production, but execute when I'm developing? I want to not comment them out, and them put them back in when developing, as it's a hassle.

推荐答案

DEBUG 是一个仅定义的宏(xcode默认值)当您正在进行项目的调试/开发构建时。执行<$ c时,这将导致 #ifdef DEBUG #endif 行之间的任何代码都不包括在内$ c> release build但它们将被编译并包含在 debug / dev / test 版本中。

DEBUG is a macro that is only defined (xcode default) when you are doing a debug/development build of your project. This will cause any code between the #ifdef DEBUG and #endif lines to not be included when doing a release build but they will be compiled and included with a debug/dev/test build.

示例:

#ifdef DEBUG
    NSLog(@"Hello World!");
#endif






这就是我的意思在我的 Project-Prefix.pch 文件中使用(使用 DEBUGLOG(@abc%@,someStr)调用; ):


This is what I use in my Project-Prefix.pch file (called with DEBUGLOG(@"abc %@", someStr);):

#ifdef DEBUG
    extern void DBGQuietLog(NSString *format, ...);
    #define DEBUGLOG(fmt, ...) DBGQuietLog((@"[Line: %d] %s: " fmt), __LINE__, __FUNCTION__, ##__VA_ARGS__);
#else
    #define DEBUGLOG(fmt, ...)
#endif

#ifdef DEBUG
    void DBGQuietLog(NSString *format, ...) {
        if (format == nil) {
            printf("nil\n");
            return;
        }
        va_list argList;
        va_start(argList, format);
        NSString *s = [[NSString alloc] initWithFormat:format arguments:argList];
        printf("%s\n", [[s stringByReplacingOccurrencesOfString:@"%%" withString:@"%%%%"] UTF8String]);
        [s release];
        va_end(argList);
    }
#endif

打印控制台线(仅在调试时)行号,类名和方法名如下:

This prints console lines (only when debugging) with line number, class name, and method name like so:

[Line: 36] -[iPhoneAppDelegate application:didFinishLaunchingWithOptions:]: Hello World!

这篇关于iPhone:在生产中抑制NSLog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 23:09
查看更多