Custom LogHandler to switch between publish and Serial

Anyone have any example code for creating a custom logHandler?

I currently have Serial.println (for debugging with usb) and Particle.publish (which I have an IFTTT printing to dropbox file for when I’m not connected) throughout my code. Both have the same messages and I’d like to clean up/consolidate code by having single Log commands with two different handlers that I can switch between.

I’ve tried searching through the relevant source code:

But must admit my c++ knowledge is limited and I couldn’t really figure out how to implement my own handler. I couldn’t find anything in the docs except how to use SerialLogHandler.

Thanks in advance!

There is an OO way of doing this (as I think you’re suggesting) where you’d use polymorphism to pull this off. In normal applications, where one would have a configuration file that the app could read at runtime could determine which logger to use, this would be simple. Or if “data” determined which one you’d use polymorphism would make sense.


You could also have a procedural (rather than OO) solution. Where you abstract all of your logging to a method. This method in turns calls either one of two other methods. All 3 methods have the same signature. Of course you could turn this into a Functional solution if you used a function pointer instead and the function pointed to at the time determines whether logUsingSerial or logUsingIfttt is called.

In the code below the log method currently logs to Serial. This decision can be a simple commented line or driven by data, hardware input etc.

void log(char[] data)

void logUsingSerial(char[] data)
	// Use Serial to log

void logUsingIfttt(char[] data)
	// publish to IFTTT to log