博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Improved logging in Objective-C …
阅读量:4072 次
发布时间:2019-05-25

本文共 2113 字,大约阅读时间需要 7 分钟。

Improved logging in Objective-C

Q:  How can I add context information - such as the current method or line number - to my logging statements?

A: The C preprocessor provides a number of standard macros that give you information about the current file, line number, or function. Additionally, Objective-C has the _cmd implicit argument which gives the selector of the current method, and functions for converting selectors and classes to strings. You can use these in your NSLog statements to provide useful context during debugging or error handling.

Listing 1  Example of logging the current method and line number. Paste it into your project, and see what it prints!

NSMutableArray *someObject = [NSMutableArray array];
NSLog(@"%s:%d someObject=%@", __func__, __LINE__, someObject);
[someObject addObject:@"foo"];
NSLog(@"%s:%d someObject=%@", __func__, __LINE__, someObject);

The following tables list the most popular macros and expressions which might be useful in your logging statements.

Table 1 
 
Preprocessor macros and for logging in C/C++/Objective-C.

Macro

Format Specifier

Description

__func__

%s

Current function signature.

__LINE__

%d

Current line number in the source code file.

__FILE__

%s

Full path to the source code file.

__PRETTY_FUNCTION__

%s

Like __func__, but includes verbose type information in C++ code.

Table 2 
 
Expressions for logging in Objective-C.

Expression

Format Specifier

Description

NSStringFromSelector(_cmd)

%@

Name of the current selector.

NSStringFromClass([self class])

%@

Name of the current object's class.

[[NSString stringWithUTF8String:__FILE__] lastPathComponent]

%@

Name of the source code file.

[NSThread callStackSymbols]

%@

NSArray of the current stack trace as programmer-readable strings. For debugging only, do not present it to end users or use to do any logic in your program.


Document Revision History

Date Notes
2011-10-04

Expanded logging examples.

2009-10-27

New document that describes use of preprocessor macros and Objective-C language features to provide better context in log statements.


© 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-10-04)

转载地址:http://djeji.baihongyu.com/

你可能感兴趣的文章
字节跳动后端开发一面
查看>>
CentOS Tensorflow 基础环境配置
查看>>
centOS7安装FTP
查看>>
FTP的命令
查看>>
CentOS操作系统下安装yum的方法
查看>>
ping 报name or service not known
查看>>
FTP 常见问题
查看>>
zookeeper单机集群安装
查看>>
do_generic_file_read()函数
查看>>
Python学习笔记之数据类型
查看>>
Python学习笔记之特点
查看>>
Python学习笔记之安装
查看>>
shell 快捷键
查看>>
VIM滚屏操作
查看>>
EMC 2014存储布局及十大新技术要点
查看>>
linux内核内存管理(zone_dma zone_normal zone_highmem)
查看>>
将file文件内容转成字符串
查看>>
循环队列---数据结构和算法
查看>>
优先级队列-数据结构和算法
查看>>
链接点--数据结构和算法
查看>>