OS X equivalent to OutputDebugString()?

后端 未结 4 1375
隐瞒了意图╮
隐瞒了意图╮ 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:24

    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:

      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);
      
    2. printf/fprintf

      The old C standard library standbys. #include 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);

    3. 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.)

    4. 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.

提交回复
热议问题