Printing in the console: printf() std::cerr / std::cout

Venetian's icon

hi

for debugging purposes I often use printf or std::cout statements and this tends to print out info in Xcode (for openframeworks/juce) or in the console.

however, I find that the same classes in Max no longer print information.

two questions:
1. has this changed? I think it may have used to print this stuff. Is there a way to print out console information? within a class, if you wanted to use object_post() you'd need to include the "ext.h" so this is not as straightforward as one might like.

2.does it take up cpu time? if you have a load of printf() statements, does the call to a blocked statement take time? I guess I should be running some kind of test on this, maybe I will!, but am interested generally on how this should be arranged.

Also, I'll put Chris Cannam's solution for debugging below which I've found very elegant. you call DSTREAM and have a flag for debug mode

Venetian's icon

attached is the version I use. You just add -DNDEBUG to the Apple LLVM 6.0, Custom compiler flags, other c++ flags.

one question I had:
> is it: -DNDEBUG for a reason?
> i.e. -NDEBUG is saying DSTREAM uses std:cerr

-Dfoo means define the symbol foo (as above). Here we define NDEBUG if
debug is not intended, so that the default is to have debug output. When
NDEBUG is defined the NoDebug class gets used instead of the normal cerr
stream.

---
// DebugLogger.h

#ifndef _DebugLogger_h
#define _DebugLogger_h

#include

//using namespace std;
class NoDebug
{
public:
template
NoDebug &operator<
return *this;
}

NoDebug &operator<
return *this;
}

};

#ifndef NDEBUG
#define DSTREAM std::cerr
#else
#define DSTREAM (NoDebug())
#endif

#endif

--- in mycode.cpp

#include "debug.h"

void somefunction(int n)
{
DSTREAM << "in somefunction, n = " << n << std::endl;
}

Then compiling with -DNDEBUG suppresses the output.

andrea agostini's icon

Hi.

When I need to print stuff to the debugging console, I use fprintf to stderr and it works flawlessly.

Hope this helps
aa

andrea agostini's icon

(I forgot to mention: I had the feeling that fprintf-ing had an impact on performance - but I never measured it)

Venetian's icon

I don't find that works for me. Even within max external, something like
fprintf(stderr, "print this");
won't show

I'm on OS 10.9 Mavericks, tested on Max 5 and on Max 6.1.7

I might not be using right

speed test I'll try to check

Timothy Place's icon

You can also use cpost() from the Max API which is like post() except that it goes to the console.