OS X equivalent to OutputDebugString()?

后端 未结 4 1380
隐瞒了意图╮
隐瞒了意图╮ 2021-02-07 14:47

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\

4条回答
  •  醉酒成梦
    2021-02-07 15:25

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

提交回复
热议问题