如何在 Cocoa 中将堆栈跟踪打印到控制台/日志?

发布于 2024-07-07 18:51:38 字数 35 浏览 6 评论 0原文

我想在某些点记录调用跟踪,例如失败的断言或未捕获的异常。

I'd like to log the call trace during certain points, like failed assertions, or uncaught exceptions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

一梦浮鱼 2024-07-14 18:51:38

此代码适用于任何线程:

NSLog(@"%@", NSThread.callStackSymbols);

返回包含调用堆栈符号的数组。 每个元素都是一个 NSString 对象,其值的格式由 backtrace_symbols() 函数确定。

This code works on any thread:

NSLog(@"%@", NSThread.callStackSymbols);

Returns an array containing the call stack symbols. Each element is an NSString object with a value in a format determined by the backtrace_symbols() function.

暮光沉寂 2024-07-14 18:51:38

n13 的答案不太有效 - 我稍微修改了一下就得到了这个

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int retval;
        @try{
            retval = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
        @catch (NSException *exception)
        {
            NSLog(@"Gosh!!! %@", [exception callStackSymbols]);
            @throw;
        }
        return retval;
    }
}

n13's answer didn't quite work - I modified it slightly to come up with this

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        int retval;
        @try{
            retval = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
        }
        @catch (NSException *exception)
        {
            NSLog(@"Gosh!!! %@", [exception callStackSymbols]);
            @throw;
        }
        return retval;
    }
}
π浅易 2024-07-14 18:51:38

Cocoa 已经将未捕获异常的堆栈跟踪记录到控制台,尽管它们只是原始内存地址。 如果您想在控制台中获得符号信息,可以使用一些 来自 Apple 的示例代码

如果您想在代码中的任意点生成堆栈跟踪(并且您使用的是 Leopard),请参阅 backtrace 手册页。 在 Leopard 之前,您实际上必须深入挖掘调用堆栈本身。

Cocoa already logs the stack trace on uncaught exceptions to the console although they're just raw memory addresses. If you want symbolic information in the console there's some sample code from Apple.

If you want to generate a stack trace at an arbitrary point in your code (and you're on Leopard), see the backtrace man page. Before Leopard, you actually had to dig through the call stack itself.

孤君无依 2024-07-14 18:51:38

几乎告诉你什么去做。

本质上,您需要设置要记录的应用程序异常处理,例如:

#import <ExceptionHandling/NSExceptionHandler.h>

[[NSExceptionHandler defaultExceptionHandler] 
                  setExceptionHandlingMask: NSLogUncaughtExceptionMask | 
                                            NSLogUncaughtSystemExceptionMask | 
                                            NSLogUncaughtRuntimeErrorMask]

This pretty much tells you what to do.

Essentially you need to set up the applications exception handling to log, something like:

#import <ExceptionHandling/NSExceptionHandler.h>

[[NSExceptionHandler defaultExceptionHandler] 
                  setExceptionHandlingMask: NSLogUncaughtExceptionMask | 
                                            NSLogUncaughtSystemExceptionMask | 
                                            NSLogUncaughtRuntimeErrorMask]
晨光如昨 2024-07-14 18:51:38

对于异常,您可以使用异常的 userInfo 字典的 NSStackTraceKey 成员来执行此操作。 请参阅控制Apple 网站上的程序对异常的响应

For exceptions, you can use the NSStackTraceKey member of the exception's userInfo dictionary to do this. See Controlling a Program's Response to Exceptions on Apple's website.

〗斷ホ乔殘χμё〖 2024-07-14 18:51:38

以这种方式快速打印:

print("stack trace:\(Thread.callStackSymbols)")

In swift print this way:

print("stack trace:\(Thread.callStackSymbols)")
吃不饱 2024-07-14 18:51:38

如果你想将它作为 NSString 获取。

[NSThread  callStackSymbols].description

If you want to get it as NSString.

[NSThread  callStackSymbols].description
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文