Hi,
I have 2 questions related to the SerialLogHandler
class.
The first was mentioned in this thread, but was being asked for a different purpose. Is it possible for SerialLogHandler
to make use of use AsyncUSBSerialRK? It would be easy enough for me to disable logging and directly write to Serial
for my application logs, but that won't help if I leave the logger enabled for non-application messages.
Secondly, is there a built-in way for log prints to be ignored when serial isn't connected?
For example, if I have SerialLogHandler enabled and have this in my loop,
if (!logOnce) {
logOnce = true;
unsigned long start = micros();
Log.info("test took %lu us", lastRun);
lastRun = micros() - start;
}
When lastRun
is returned from a cloud function, it will return times similar to how long it would be if it were printed to the console while USB was connected, in the neighborhood of 5-9 ms. Since all of our devices in the field won't be connected over USB, it's a lot of wasted blocking time.
I can reduce this time by wrapping each of my logs with Serial.isConnected()
but I would have to do that for a lot of lines, it would look kind of ugly, and I assume it wouldn't quicken internally generated logs. If i did so anyway,
if (!logOnce) {
logOnce = true;
unsigned long start = micros();
if(Serial.isConnected()) {
Log.info("test took %lu us", lastRun);
}
lastRun = micros() - start;
}
Returns in around 30 µs or more than 2 ms with the USB disconnected, presumably due to the following branches in usb_hal.cpp
.
bool HAL_USB_USART_Is_Connected(HAL_USB_USART_Serial serial) {
if (serial != HAL_USB_USART_SERIAL) {
return false;
}
return getCdcClassDriver().isConnected();
}
After a couple of prints it steadily returns < 30 µs. I think it would be nice for the log handler to internally check if serial is connected to improve efficiency when USB is disconnected, as it would also make internally generated logging faster in that scenario (if it doesn't already but I don't have a great way to test that)
Bonus question - After disconnecting USB and reconnecting it, the Photon 2 won't be accessible unless I power cycle it. Not sure if that is intentional or not or if there's a way around it in case someone else might need that.