c++macoscross-platformmacos-carbon

OS X equivalent to OutputDebugString() ?


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


Solution

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