Can SerialLogHandler use AsyncUSBSerialRK?

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.

This may be fixed in Device OS 5.8.0:

Fix USB re-enumeration issues Gen 4 rtl872x 2744 2752 2754

I'm still contemplating the other question. It is easy to create a different LogHandler class that uses the async USB serial, but I'm still considering whether that will have unexpected consequences.

This may be fixed in Device OS 5.8.0:

This did seem to fix it, thanks! Though I had to switch ports on my computer for whatever reason. Even when switching back to the port after it working on a different one wont get it to recognize. I'm just going to blame Windows for that one, maybe a restart would fix it.

Now the same test is even slower though.

Serial connection closed.  Attempting to reconnect...
Serial monitor opened successfully:
0000155356 [app] INFO: test took 3457 us
0000158836 [app] INFO: test took 12868 us
0000162385 [app] INFO: test took 13397 us

Serial connection closed.  Attempting to reconnect...
Serial monitor opened successfully:
0000186749 [app] INFO: test took 12 us 
^ above was from the previous run where USB was removed ^
0000188820 [app] INFO: test took 12574 us
0000204001 [app] INFO: test took 13382 us
0000206076 [app] INFO: test took 13035 us

As far as adding async to the LogHandler, I get that it would change up a lot of things in many places, but I think checking for serial connected before printing would still be hugely beneficial. It could be a optional flag passed into LogHandler on instantiation. Then maybe if i still wanted logging in production I wouldn't have to worry about a bunch of blocking from internal logs when there's no serial connected.