arduino / ArduinoCore-avr

The Official Arduino AVR core
https://www.arduino.cc
1.22k stars 1.04k forks source link

Add printing to NULL in the Print class? #525

Closed m-elias closed 1 year ago

m-elias commented 1 year ago

I'm working on a project where I tried Stream* stream = NULL when I didn't want output messages. While this compiled, my Teensy went into a boot loop. I've solved my issue by using overloaded functions with an if (stream != NULL) check but I wondered if adding NULL handling to the core Print class would be a interesting idea.

facchinm commented 1 year ago

Hi @m-elias , the thing you are proposing is not very feasible at a platform level; however, adding a default NullStream object (that does nothing) is definitely a good idea. The code would then need to be modified as Stream* stream = &NullStream and that instance would simply "do nothing"

m-elias commented 1 year ago

Is that something that needs to be added to the core or just to my individual sketch?

matthijskooijman commented 1 year ago

I think it is too specific for the core, better for a library.

In fact, it seems this library implements exactly that: https://github.com/RobTillaart/DEVNULL Also, see https://github.com/bblanchon/ArduinoStreamUtils which has a bunch of stream utilities (but seems to lack a null stream, even though it would fit in nicely I think).