Logging.h
author Jens Alfke <jens@mooseyard.com>
Sun Jun 01 14:01:44 2008 -0700 (2008-06-01)
changeset 13 c59dec088990
parent 8 5588347dfcbd
child 19 5ade3e09a827
permissions -rw-r--r--
* Made Test.h work with regular C99 without GCC extensions.
* Copied the necessary Google Toolbox source files into the project, so users don't have to download an additional library.
     1 //
     2 //  Logging.h
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 1/5/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 
    12 NSString* LOC( NSString *key );     // Localized string lookup
    13 
    14 
    15 // To enable IN_SEGMENT (which breaks rarely-called logging code out of your main code segment,
    16 // improving locality of reference) you must define MY_USE_NESTED_FNS in your prefix file or
    17 // target settings, and add the GCC flag "-fnested-functions" to your target's C flags.
    18 #if defined(MY_USE_NESTED_FNS) && ! defined(__cplusplus)
    19     #define IN_SEGMENT(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) void _outofband_(void);\
    20                             _outofband_();\
    21                             void _outofband_(void)
    22     #define IN_SEGMENT_NORETURN(SEG) auto __attribute__ ((section ("__TEXT, "#SEG))) __attribute__ ((noinline)) __attribute__((noreturn)) void _assertfailure_(void);\
    23                             _assertfailure_();\
    24                             void _assertfailure_(void)
    25 #else
    26     #define IN_SEGMENT(SEG)
    27     #define IN_SEGMENT_NORETURN(SEG)
    28 #endif
    29 
    30 #define Log(FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\
    31                             IN_SEGMENT(Logging){_Log(FMT,##ARGS);}\
    32                          } }while(0)
    33 #define LogTo(DOMAIN,FMT,ARGS...) do{if(__builtin_expect(_gShouldLog,0)) {\
    34                                     IN_SEGMENT(Logging) {if(_WillLogTo(@""#DOMAIN)) _LogTo(@""#DOMAIN,FMT,##ARGS);}\
    35                                   } }while(0)
    36 #define Warn Warn
    37 
    38 void AlwaysLog( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2)));
    39 BOOL _WillLogTo( NSString *domain );
    40 BOOL EnableLog( BOOL enable );
    41 #define EnableLogTo( DOMAIN, VALUE )  _EnableLogTo(@""#DOMAIN, VALUE)
    42 #define WillLogTo( DOMAIN )  _WillLogTo(@""#DOMAIN)
    43 
    44 
    45 // internals; don't use directly
    46 extern int _gShouldLog;
    47 void _Log( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2)));
    48 void Warn( NSString *msg, ... ) __attribute__((format(__NSString__, 1, 2)));
    49 void _LogTo( NSString *domain, NSString *msg, ... ) __attribute__((format(__NSString__, 2, 3)));
    50 BOOL _WillLogTo( NSString *domain );
    51 BOOL _EnableLogTo( NSString *domain, BOOL enable );