jens@0: // jens@0: // Logging.h jens@0: // MYUtilities jens@0: // jens@0: // Created by Jens Alfke on 1/5/08. jens@0: // Copyright 2008 Jens Alfke. All rights reserved. jens@0: // jens@0: jens@11: #import jens@0: jens@0: jens@0: NSString* LOC( NSString *key ); // Localized string lookup jens@0: jens@0: jens@11: // To enable IN_SEGMENT (which breaks rarely-called logging code out of your main code segment, jens@11: // improving locality of reference) you must define MY_USE_NESTED_FNS in your prefix file or jens@11: // target settings, and add the GCC flag "-fnested-functions" to your target's C flags. jens@11: #if defined(MY_USE_NESTED_FNS) && ! defined(__cplusplus) jens@8: #define IN_SEGMENT(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) void _outofband_(void);\ jens@8: _outofband_();\ jens@8: void _outofband_(void) jens@11: #define IN_SEGMENT_NORETURN(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) __attribute__((noreturn)) void _assertfailure_(void);\ jens@11: _assertfailure_();\ jens@11: void _assertfailure_(void) jens@11: #else jens@11: #define IN_SEGMENT(SEG) jens@11: #define IN_SEGMENT_NORETURN(SEG) jens@8: #endif jens@8: jens@8: #define Log(FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\ jens@8: IN_SEGMENT(Logging){_Log(FMT,##ARGS);}\ jens@8: } }while(0) jens@8: #define LogTo(DOMAIN,FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\ jens@11: IN_SEGMENT(Logging) {if(_WillLogTo(@""#DOMAIN)) _LogTo(@""#DOMAIN,FMT,##ARGS);}\ jens@8: } }while(0) jens@0: #define Warn Warn jens@0: jens@0: void AlwaysLog( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2))); jens@1: BOOL EnableLog( BOOL enable ); jens@1: #define EnableLogTo( DOMAIN, VALUE ) _EnableLogTo(@""#DOMAIN, VALUE) snej@19: #define WillLog() _WillLogTo(nil) jens@1: #define WillLogTo( DOMAIN ) _WillLogTo(@""#DOMAIN) jens@1: jens@1: jens@1: // internals; don't use directly jens@1: extern int _gShouldLog; jens@0: void _Log( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2))); jens@0: void Warn( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2))); jens@1: void _LogTo( NSString *domain, NSString *msg, ... ) __attribute__((format(__NSString__, 2, 3))); jens@1: BOOL _WillLogTo( NSString *domain ); jens@1: BOOL _EnableLogTo( NSString *domain, BOOL enable );