OS X 相当于 OutputDebugString() ?

发布于 2024-07-11 12:02:26 字数 267 浏览 9 评论 0原文

我正在研究将现有 Windows MFC 控件移植到 OS X/Carbon 的可行性。 我的测试床是使用 XCode 3 Wizard 生成的 C++ Carbon 应用程序。

我正在寻找一种快速方法将一些跟踪信息转储到调试器或 OS X 的 DbgView 等效项。 在 Win32 上我会使用 OutputDebugString() - OS X 上有什么用? 有没有办法查看从 Carbon 应用程序写入 std::cout 的测试?

谢谢杰瑞

I'm examining the feasibility of porting an existing Windows MFC control to OS X/Carbon.
My test bed is a C++ Carbon application generated using the XCode 3 Wizard.

I'm looking for a quick way to dump some trace info to the debugger or the OS X equivalent of DbgView. On Win32 I'd use OutputDebugString() - what's the deal on OS X? Is there a way to view test written to std::cout from a Carbon app?

Thanks

Jerry

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

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

发布评论

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

评论(4

尾戒 2024-07-18 12:02:26

没有真正的等价物。 Xcode 在底层使用 GDB,所以你基本上要处理它。 但是,您可以自己实现。 仅当调试器存在时,下面的代码示例才会生成标准输出的输出。 如果编译时存在 NDEBUG,您可以通过将其作为宏包装在预处理器指令中并编译出来(或编译成内联 nil 函数)来进一步保护它。 应用程序生成的任何输出都将定向到 Xcode 中的调试控制台。

extern "C" {

bool IsDebuggerPresent() {
    int mib[4];
    struct kinfo_proc info;
    size_t size;

    info.kp_proc.p_flag = 0;
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getpid();

    size = sizeof(info);
    sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);

    return ((info.kp_proc.p_flag & P_TRACED) != 0);
}

void OutputDebugString(const char *restrict fmt, ...) {
    if( !IsDebuggerPresent() )
        return;

    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

}

There is no real equivalent. Xcode uses GDB under the hood, so you're basically dealing with that. You could, however, implement it yourself. The code sample below will produce output to standard out only when the debugger is present. You could further protect this by wrapping it in preprocessor directives as a macro and compile it out (or into an inline nil function) if NDEBUG is present at compile time. Any output produced by an application will be directed to the debugging console in Xcode.

extern "C" {

bool IsDebuggerPresent() {
    int mib[4];
    struct kinfo_proc info;
    size_t size;

    info.kp_proc.p_flag = 0;
    mib[0] = CTL_KERN;
    mib[1] = KERN_PROC;
    mib[2] = KERN_PROC_PID;
    mib[3] = getpid();

    size = sizeof(info);
    sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0);

    return ((info.kp_proc.p_flag & P_TRACED) != 0);
}

void OutputDebugString(const char *restrict fmt, ...) {
    if( !IsDebuggerPresent() )
        return;

    va_list args;
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

}
撩心不撩汉 2024-07-18 12:02:26

首先,Carbon 现在和将来都不会在 64 位版本中提供。 如果 Apple 放弃 32 位 Mac OS X(可以肯定地认为迟早会发生),您的应用程序将无法运行。 使用可可。

也就是说,有几种方法可以进行日志记录:

  1. NSLog

这是一个 Cocoa 函数,但您也可以在 Carbon 应用程序中使用它。 链接到 Foundation 框架,但不包含标头。 自己声明:

    extern "C" int NSLog(CFStringRef format, ...);

您将传递 CFSTR 文字作为格式:

    NSLog(CFSTR("Count: %u"), count);

NSLog 的优点是您可以使用 %@ 格式化程序打印 CF 属性列表对象(字符串、数据对象、日期、数字、数组和字典)。 例如:

    CFArrayRef array = /*...*/;
    NSLog(CFSTR("Array: %@"), array);
  1. printf/fprintf

旧的 C 标准库备用。 #include 来获取它们。 在 GUI 应用程序中这并不重要,但您应该使用 stderr 来保持整洁: fprintf(stderr, "Count: %u\n", count);

  1. syslog

About as old as f?printf ,我猜,但更强大。 这是一个实际的日志系统,而不仅仅是写入文件。 您可以指定优先级等内容,从而允许您在 Beta 测试人员的系统上抑制调试日志消息,同时仍然能够在您自己的系统上读取它们。 (最终版本根本不应该包含日志记录代码。)

  1. asl_log

Apple System Logger 的一部分,Apple 更通用的 syslog 替代品。 我有一系列关于 ASL 的帖子 在我的博客上。

First off, Carbon isn't and won't be available in 64-bit. If Apple ever drops 32-bit Mac OS X (which it's safe to assume will happen sooner or later), your app will not run. Use Cocoa.

That said, there are several ways to do logging:

  1. NSLog

This is a Cocoa function, but you can use it in Carbon apps, too. Link against the Foundation framework, but don't include the header. Declare it yourself:

    extern "C" int NSLog(CFStringRef format, ...);

You'll pass a CFSTR literal as the format:

    NSLog(CFSTR("Count: %u"), count);

The advantage of NSLog is that you can print CF property-list objects (strings, data objects, dates, numbers, arrays, and dictionaries) using the %@ formatter. For example:

    CFArrayRef array = /*...*/;
    NSLog(CFSTR("Array: %@"), array);
  1. printf/fprintf

The old C standard library standbys. #include <stdio.h> to get them. It doesn't matter much in a GUI app, but you should use stderr for cleanliness: fprintf(stderr, "Count: %u\n", count);

  1. syslog

About as old as f?printf, I'd guess, but more powerful. This is an actual logging system, not just writing to a file. You can specify things like priority, allowing you to suppress your debug log messages on beta testers' systems while still being able to read them on your own system. (Final releases should not contain logging code at all.)

  1. asl_log

Part of Apple System Logger, Apple's more general replacement for syslog. I have a series of posts about ASL on my blog.

人│生佛魔见 2024-07-18 12:02:26

您可能需要查看syslog,因为它是基于 UNIX 的系统上事实上的诊断方法。 例如:

#include <syslog.h>

/* Do this early on in your program like at the beginning of main() */
openlog("MYPROGRAM", 0, LOG_USER);

/* Use this to log something */
syslog(LOG_DEBUG, "%s %s", "Hello", "World");

/* Do this somewhere before you exit if you being are pedantic */
closelog();

Google syslog 了解更多信息。 您还必须调整 syslog.conf 中的一些位以将输出定向到日志或控制台。 然后,您可以在终端窗口中或使用控制台应用程序查看输出。

You might want to look into syslog since it is the de facto diagnostic method on UNIX-based systems. Something like:

#include <syslog.h>

/* Do this early on in your program like at the beginning of main() */
openlog("MYPROGRAM", 0, LOG_USER);

/* Use this to log something */
syslog(LOG_DEBUG, "%s %s", "Hello", "World");

/* Do this somewhere before you exit if you being are pedantic */
closelog();

Google syslog for more information. You will also have to twiddle some bits in syslog.conf to direct the output to a log or console. Then you can view the output in a terminal window or using the Console application.

金橙橙 2024-07-18 12:02:26

在 Xcode 中,您可以在“控制台”窗口(运行 -> 控制台)中看到 std::cout/std::cerr 的输出。

还有 Console.app(位于 /Applications/Utilities 中),它记录从 GUI 应用程序写入 std::cerr 的所有输出。

In Xcode you can see the output of std::cout/std::cerr in the "console" window (Run->Console).

There is also Console.app (in /Applications/Utilities) which logs all output written to std::cerr from GUI applications.

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