How to write my own debug class

I want to use a debug class that can be used over all my custom defined classes, without worrying which serial is used.

My debug class looks like this:

#ifndef Debuggable_h
#define Debuggable_h

#define PRINTLN(...) {if(_monitor) _monitor->println(__VA_ARGS__); }
#define PRINT(...) {if(_monitor) _monitor->print(__VA_ARGS__); }

class Debuggable
{
	public:
		Debuggable(Stream* monitor)
		{
			_monitor = monitor;
		}
		
	private:
		Stream* _monitor;
};

#endif

In my other class I want to do something like this:

#include <Debuggable.h>

ABPCredentials::ABPCredentials(uint8_t* deviceAddr, uint8_t* appSessionKey, uint8_t* netwSessionKey)
{
	if (!hasKeys(deviceAddr, appSessionKey, netwSessionKey))
	{
		PRINTLN("Please fill in deviceAddr, appSessionKey and netwSessionKey");
	}
	else
	{
		_deviceAddr = deviceAddr;
		_appSessionKey = appSessionKey;
		_netwSessionKey = netwSessionKey;
	}
}

And In my sketch I have this:

#define debugSerial Serial

Debuggable debug(&debugSerial);

I'm getting compilation errors, like _monitor is not declared in this scope.

Any ideas how to implement such a functionality?

Please edit you're post to have code tags. see How to use the forum.

Next, why make nice classes but still mess with macro's? I'm against macro's for most stuff (because C++ has better solutions for it) but for making switchable/flexible debugging it's quite good. I once made a proff-of-concept library for it DebugLib.

Your debuglib is using Serial, but I don't want to be depended on Serial, it can also be SerialUSB or something like that.
I want to have the option to initialize it in the sketch.

No it's not :wink: That's the beauty.

If you do

 #define DEBUG_SKETCH

it will indeed default to Serial.

But if you specify DEBUG_SKETCH with a value that is used:

#define DEBUG_SKETCH SomethingElse

will use that. As long as 'SomethingElse' is derived from the Print-class (or just supports .print() and .println()) it works :slight_smile:

@septillion : it seems very interesting, can you please explain how to use your lib? Please give a few examples for people who (like me) don't understand the code...
Thanks

Yes, but I want that SomethingElse is coming from the sketch and not be fixed.
How do I have to pass SomethingElse this via the sketch program?

Maybe the solution is working with Static methods, but then I don't now how to structure my code.

Any ideas?

You can indeed make it fixed. Don't even need a class for that even. But than there is no excuse to use macro's! The beauty of the macro trick is completely disappears from the code one disabled. And it's weird to switch debug device on the fly ::slight_smile: If you're debugging you know where you're debugging in...

But yeah, you could make a function (which you can wrap in a library). The most flexible way if you always debug to Print-classes would be to have a pointer to that print class which you can set. Or wrap it in a class.

But really, I see no use in run time switching debug device....