如何在main()中使用NSLog,但在NSApplicationMain之外?

时间:2023-01-24 18:31:15

I have read questions/559482/why-doesnt-an-iphone-apps-main-function-ever-get-a-chance-to-finish, which explains why NSApplicationMain never actually returns. The same thing happens (for the same reason) in a desktop cocoa application, which is what I am working on.

我已经阅读了问题/ 559482 / why-doesnt-an-iphone-apps-main-function-ever-a-chance-to-finish,这解释了为什么NSApplicationMain永远不会真正返回。在桌面可可应用程序中发生同样的事情(出于同样的原因),这正是我正在研究的。

With that in mind, how would I go about using NSLog to output some final debugging messages when my application exits?

考虑到这一点,当我的应用程序退出时,如何使用NSLog输出一些最终的调试消息?

To be specific, I would like to do something like this:

具体来说,我想做这样的事情:

int myDebugVariable = 0;

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CMLog(@"application begin");

    int exitCode = NSApplicationMain(argc,  (const char **) argv);

    CMLog(@"application end. Debugging variable = %d", myDebugVariable);

    [pool release];
    return exitCode;
}

In this example, The "application begin" line is printed to the console, but the "application end." line is not.

在此示例中,“应用程序开始”行打印到控制台,但“应用程序结束”。线不是。

Note #1: In my actual code, I am using something more sophisticated than myDebugVariable. This is a simplified example that illustrates the effect I am trying to achieve.

注意#1:在我的实际代码中,我使用的东西比myDebugVariable更复杂。这是一个简化的例子,说明了我想要实现的效果。

Note #2: I am familiar with the ApplicationWillTerminate method, which gets called when the application is about to quit, but it does not suit my needs. My debugging code relies on the dealloc methods for some custom classes, so it does not come into play until after ApplicationWillTerminate is called.

注意#2:我熟悉ApplicationWillTerminate方法,该方法在应用程序即将退出时被调用,但它不符合我的需要。我的调试代码依赖于某些自定义类的dealloc方法,因此在调用ApplicationWillTerminate之后它才会发挥作用。


update:

Adam Rosenfield's answer did the trick. For the sake of completeness, here is a working solution:

Adam Rosenfield的回答就是这个伎俩。为了完整起见,这是一个有效的解决方案:

int myDebugVariable = 0;

void my_exit_handler(void)
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CMLog(@"application end: Debugging variable = %d", myDebugVariable);

    [pool release];
}

int main(int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CMLog(@"application begin");
    atexit(my_exit_handler);

    int exitCode = NSApplicationMain(argc,  (const char **) argv);

    [pool release];
    return exitCode;
}

1 个解决方案

#1


Use atexit(3) to register an exit handler. It will automatically be invoked when your app exits, either by finishing main or by calling exit(3). For example:

使用atexit(3)注册退出处理程序。当您的应用程序退出时,它将自动调用,方法是完成main或通过调用exit(3)。例如:

void my_exit_handler(void)
{
    NSLog(@"about to exit, x = %d\n", x);
}

// at some point during app initialization...
atexit(&my_exit_handler);

#1


Use atexit(3) to register an exit handler. It will automatically be invoked when your app exits, either by finishing main or by calling exit(3). For example:

使用atexit(3)注册退出处理程序。当您的应用程序退出时,它将自动调用,方法是完成main或通过调用exit(3)。例如:

void my_exit_handler(void)
{
    NSLog(@"about to exit, x = %d\n", x);
}

// at some point during app initialization...
atexit(&my_exit_handler);