OS X相当于OutputDebugString()? [英] OS X equivalent to OutputDebugString()?

查看:81
本文介绍了OS X相当于OutputDebugString()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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?

感谢

Jerry

推荐答案

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

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);
}

}

这篇关于OS X相当于OutputDebugString()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆