NSLog()是否有不带时间戳和日期戳的打印变体,以及自动换行?
谢谢.现在使用以下代码,我可以打印NSString,cString或对象:
#import <Foundation/Foundation.h>
#import <stdio.h>
int main (int argc, const char * argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  NSString *s = @"Hello, World!";
  NSDate *today = [NSDate date];
  NSLog(@"%@", s);
  printf("%s at %s", [s UTF8String], [[today description] UTF8String]);
  [pool drain];
  return 0;
}  
Neb*_*Fox 19
用printf()而不是NSLog()
Ita*_*ber 14
它也困扰我,所以我写了一个替换函数,NSLog()并且printf():
void IFPrint (NSString *format, ...) {
    va_list args;
    va_start(args, format);
    fputs([[[[NSString alloc] initWithFormat:format arguments:args] autorelease] UTF8String], stdout);
    va_end(args);
}
然后,您可以使用它而不是NSLog()(例如IFPrint(@"Current date: %@", [NSDate date])),但它不会打印出任何时间戳或换行符,并且您不必乱用C字符串和数组,以及诸如此类的东西.我会说,它非常方便.
如果你想,检查出我的全部代码(我也写了fprintf中,scanf函数,和的fscanf更换)这里.
(还有一个关于它的SO话题在这里).
Rod*_*igo 14
这段代码可行
#ifdef DEBUG
    #define NSLog(FORMAT, ...) fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
    #define NSLog(...) {}
#endif
定义宏
#if __has_feature(objc_arc)
  #define DLog(format, ...) CFShow((__bridge CFStringRef)[NSString stringWithFormat:format, ## __VA_ARGS__]);
#else
  #define DLog(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
#endif
并在代码中使用这个宏,例如
NSLog(@"Content with time stamp");
DLog(@"Content without time stamp");
这是控制台的输出
NSLog-> 2014-01-28 10:43:17.873 TestApp[452:60b] Content with time stamp
DLog->Content without time stamp
如果有人想要自定义日志,它可以为您提供更多信息,例如方法名称/行号等,可以下载开源代码MLog.h on GitHub。
| 归档时间: | 
 | 
| 查看次数: | 14643 次 | 
| 最近记录: |