endy endy - 1 year ago 41
iOS Question

Dynamically turning on NSLogs

I am currently using the following DebugLog macro

#if defined(DEBUG) && defined(useDebugLogs)
#define DebugLog( s, ... ) NSLog( @"<%s:(%d)> %@", __PRETTY_FUNCTION__, __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#define DebugLog( s, ... )

However, now with test flight allowing remote logging this is a little dated.

Basically what I want to do is add a switch in my settings.plist to allow the user to turn on remote logging.

I've come across this tutorial

However, it's a very poor explanation and doesn't actually seem to work.

Answer Source

well, the best way IMO is to just always send the logs and tweak your implementation so you see exactly what you want. you either want analytics, or you won't use them. there is an error, or there is not. work your way through them and improve your signal to noise ratio. anyways…

you could just create a dedicated function for this purpose. it can conditionally log or not based on some variable in your implementation. note that you may forward your va_list by calling NSLogv rather than NSLog, or you may use -[NSString initWithFormat:arguments:] to forward the message + arguments elsewhere:


extern void MONDebugLog(NSString * format, ... ) NS_FORMAT_FUNCTION(1,2);


void MONDebugLog(NSString * format, ... ) {

    enum {

    static int DoLog = MONLog_Undefined;
    if (MONLog_Undefined == DoLog) {
        DoLog = ...load from plist or defaults and set to Log_Enable or Log_Disable...;

    if (MONLog_Disable == DoLog) {
    va_list arguments;
    va_start(arguments, format);
    NSString * str = [[NSString alloc] initWithFormat:format arguments:arguments];
    ...now log or xmit str...