Can I shut printf up ? (its an odd question)

21 Jan 2013

As I am standing on the shoulders of giants, I cannot complain.

I am using a library where the original coder has put printf ALL OVER, (and I mean ALL OVER) it, so that they could debug it and get it running.

It's slowing it down and my initial thought was to go through it all taking out all the printfs. (but I might later need them)

I usually add a int value "int debug=1" and then wrap all my printfs in "ifs" so I can turn them off. "if (debug) {"

before I attack all the printfs (a lot of work), is there a way to shut up printf ? could I do a weird override of printf that does nowt ? that I can include at compile time if I want to shut it up ?

any thoughts gratefully received. (including any better way on how I should add debug printfs that can be turned on and off)

21 Jan 2013

Here's a quick and dirty "fix". At the top of each file that used printfs (but AFTER all the #includes), add the following:

#define printf(format, ...)

This will effectively replace all printf calls with empty text.

21 Jan 2013

thankyou... it *almost* worked ;-)

But,.. they've put this in...

void printf(const BD_ADDR* addr)
{
    const u8* a = addr->addr;
    printf("%02X:%02X:%02X:%02X:%02X:%02X",a[5],a[4],a[3],a[2],a[1],a[0]);
}

which disappears, and then I get calls to it that fail. I'll just *have* to sort it out properly I spose.

21 Jan 2013

Do the mbed libraries include a routine which will allow you to re-direct 'stdout' to some sort of 'null' device?

21 Jan 2013

Hi David, Fred, you could write something like this:

#include "mbed.h"

class DevNull : public Stream {

public:
    DevNull(const char *name=NULL) : Stream(name) {}

protected:
    virtual int _getc()      {return 0;}
    virtual int _putc(int c) {return c;}
};

DevNull null("null");

int main() {
    printf("re-routing stdout to /null\n");
    
    freopen("/null", "w", stdout);
    printf("printf redirected to /null\n");
    [...]
}

HTH, Emilio