开发者

How to output warnings to the console during a build in Xcode?

How do I output custom warnings to the console in Xcode? I want to do this to remind myself that I'm in a particular build mode.

I tried #warning "Hello World" but I don't see it in the console.

Any suggestions?

Edit:

开发者_如何学编程

I don't want to use NSLog because I am already using it to log a bunch of stuff so when I use NSLog for warnings, it is difficult to see all the warnings.


#warning directive

The #warning directive is to see the message on the compiler. This is very handy to know that something must be changed before deploying or simply to remember to change a piece of code that can be improved (but you don't have the time to do so now). (go to View -> Navigators -> Show Issue Navigator on your project and you will the see the list of warnings). These messages will not appear on the console.

The Apple System Log facility

What you want is to show a warning to the console while the app is running and here is where The Apple System Log facility comes to the rescue.

You have 8 logging levels:

  • 0 Emergency.
  • 1 Alert
  • 2 Critical
  • 3 Error
  • 4 Warning
  • 5 Notice
  • 6 Info
  • 7 Debug

Code sample:

#include <asl.h>
...
asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");

Since I was in the same situation than you once, and to make things simple, I use this wrapper https://github.com/MikeWeller/MWLogging in all my projects so my debug errors won't show when I send my App to the App Store but other critical error will.

Update: Now with Swift I use this debug log framework https://github.com/DaveWoodCom/XCGLogger


You can use NSLog

In state of your warning use something like:

NSLog(@"Hello World");

Or you can use the standard C printf function:

printf("Hello World");

If you want to be printed on stderr you can use fprintf redirected to stderr.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜