|
34 | 34 | // Uncomment to enable logging in interrupt handlers
|
35 | 35 | // #define LOG_FROM_ISR
|
36 | 36 |
|
| 37 | +// Uncomment to enable logging to the USB serial while in listening mode |
| 38 | +// #define LOG_IN_LISTENING_MODE |
| 39 | + |
37 | 40 | #if defined(LOG_FROM_ISR) && PLATFORM_ID != PLATFORM_GCC
|
38 | 41 | // When compiled with LOG_FROM_ISR defined use ATOMIC_BLOCK
|
39 | 42 | #define LOG_WITH_LOCK(x) ATOMIC_BLOCK()
|
@@ -472,12 +475,6 @@ int spark::detail::LogFilter::nodeIndex(const Vector<Node> &nodes, const char *n
|
472 | 475 |
|
473 | 476 | // spark::StreamLogHandler
|
474 | 477 | void spark::StreamLogHandler::logMessage(const char *msg, LogLevel level, const char *category, const LogAttributes &attr) {
|
475 |
| - // TODO: Move this check to a base class (see also JSONStreamLogHandler::logMessage()) |
476 |
| -#if PLATFORM_ID != PLATFORM_GCC |
477 |
| - if (stream_ == &Serial && Network.listening()) { |
478 |
| - return; // Do not mix logging and serial console output |
479 |
| - } |
480 |
| -#endif // PLATFORM_ID != PLATFORM_GCC |
481 | 478 | const char *s = nullptr;
|
482 | 479 | // Timestamp
|
483 | 480 | if (attr.has_time) {
|
@@ -539,10 +536,39 @@ void spark::StreamLogHandler::logMessage(const char *msg, LogLevel level, const
|
539 | 536 | write("\r\n", 2);
|
540 | 537 | }
|
541 | 538 |
|
| 539 | +void spark::StreamLogHandler::write(const char *data, size_t size) { |
| 540 | + // TODO: Move this check to a base class (see also JSONStreamLogHandler::logMessage()) |
| 541 | +#if PLATFORM_ID != PLATFORM_GCC && !defined(LOG_IN_LISTENING_MODE) |
| 542 | + if (stream_ == &Serial && Network.listening()) { |
| 543 | + return; // Do not mix logging and serial console output |
| 544 | + } |
| 545 | +#endif |
| 546 | + stream_->write((const uint8_t*)data, size); |
| 547 | +} |
| 548 | + |
| 549 | +void spark::StreamLogHandler::printf(const char *fmt, ...) { |
| 550 | + char buf[32]; |
| 551 | + va_list args; |
| 552 | + va_start(args, fmt); |
| 553 | + int n = vsnprintf(buf, sizeof(buf), fmt, args); |
| 554 | + va_end(args); |
| 555 | + if ((size_t)n >= sizeof(buf)) { |
| 556 | + char buf[n + 1]; // Use a larger buffer |
| 557 | + va_start(args, fmt); |
| 558 | + n = vsnprintf(buf, sizeof(buf), fmt, args); |
| 559 | + va_end(args); |
| 560 | + if (n > 0) { |
| 561 | + write(buf, n); |
| 562 | + } |
| 563 | + } else if (n > 0) { |
| 564 | + write(buf, n); |
| 565 | + } |
| 566 | +} |
| 567 | + |
542 | 568 | // spark::JSONStreamLogHandler
|
543 | 569 | void spark::JSONStreamLogHandler::logMessage(const char *msg, LogLevel level, const char *category, const LogAttributes &attr) {
|
544 |
| - // TODO: Move this check to a base class (see also StreamLogHandler::logMessage()) |
545 |
| -#if PLATFORM_ID != PLATFORM_GCC |
| 570 | + // TODO: Move this check to a base class (see also StreamLogHandler::write()) |
| 571 | +#if PLATFORM_ID != PLATFORM_GCC && !defined(LOG_IN_LISTENING_MODE) |
546 | 572 | if (this->stream() == &Serial && Network.listening()) {
|
547 | 573 | return; // Do not mix logging and serial console output
|
548 | 574 | }
|
|
0 commit comments