Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable direct logging in listening mode #2512

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions wiring/inc/spark_wiring_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,10 +702,6 @@ inline Print* spark::StreamLogHandler::stream() const {
return stream_;
}

inline void spark::StreamLogHandler::write(const char *data, size_t size) {
stream_->write((const uint8_t*)data, size);
}

inline void spark::StreamLogHandler::write(const char *str) {
write(str, strlen(str));
}
Expand All @@ -714,13 +710,6 @@ inline void spark::StreamLogHandler::write(char c) {
write(&c, 1);
}

inline void spark::StreamLogHandler::printf(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
stream_->vprintf(false, fmt, args);
va_end(args);
}

// spark::JSONStreamLogHandler
inline void spark::JSONStreamLogHandler::write(const char *data, size_t size) {
// This handler doesn't support direct logging
Expand Down
41 changes: 35 additions & 6 deletions wiring/src/spark_wiring_logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
// Uncomment to enable logging in interrupt handlers
// #define LOG_FROM_ISR

// Uncomment to enable logging to the USB serial while in listening mode
// #define LOG_IN_LISTENING_MODE

#if defined(LOG_FROM_ISR) && PLATFORM_ID != PLATFORM_GCC
// When compiled with LOG_FROM_ISR defined use ATOMIC_BLOCK
#define LOG_WITH_LOCK(x) ATOMIC_BLOCK()
Expand Down Expand Up @@ -472,12 +475,11 @@ int spark::detail::LogFilter::nodeIndex(const Vector<Node> &nodes, const char *n

// spark::StreamLogHandler
void spark::StreamLogHandler::logMessage(const char *msg, LogLevel level, const char *category, const LogAttributes &attr) {
// TODO: Move this check to a base class (see also JSONStreamLogHandler::logMessage())
#if PLATFORM_ID != PLATFORM_GCC
#if PLATFORM_ID != PLATFORM_GCC && !defined(LOG_IN_LISTENING_MODE)
if (stream_ == &Serial && Network.listening()) {
return; // Do not mix logging and serial console output
}
#endif // PLATFORM_ID != PLATFORM_GCC
#endif
const char *s = nullptr;
// Timestamp
if (attr.has_time) {
Expand Down Expand Up @@ -539,14 +541,41 @@ void spark::StreamLogHandler::logMessage(const char *msg, LogLevel level, const
write("\r\n", 2);
}

void spark::StreamLogHandler::write(const char *data, size_t size) {
#if PLATFORM_ID != PLATFORM_GCC && !defined(LOG_IN_LISTENING_MODE)
if (stream_ == &Serial && Network.listening()) {
return; // Do not mix logging and serial console output
}
#endif
stream_->write((const uint8_t*)data, size);
}

void spark::StreamLogHandler::printf(const char *fmt, ...) {
char buf[32];
va_list args;
va_start(args, fmt);
int n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if ((size_t)n >= sizeof(buf)) {
char buf[n + 1]; // Use a larger buffer
va_start(args, fmt);
n = vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
if (n > 0) {
write(buf, n);
}
} else if (n > 0) {
write(buf, n);
}
}

// spark::JSONStreamLogHandler
void spark::JSONStreamLogHandler::logMessage(const char *msg, LogLevel level, const char *category, const LogAttributes &attr) {
// TODO: Move this check to a base class (see also StreamLogHandler::logMessage())
#if PLATFORM_ID != PLATFORM_GCC
#if PLATFORM_ID != PLATFORM_GCC && !defined(LOG_IN_LISTENING_MODE)
if (this->stream() == &Serial && Network.listening()) {
return; // Do not mix logging and serial console output
}
#endif // PLATFORM_ID != PLATFORM_GCC
#endif
JSONStreamWriter json(*this->stream());
json.beginObject();
// Level
Expand Down