From 8df4eeda26f37cc0c0e61e7a28b876f7571efe00 Mon Sep 17 00:00:00 2001 From: thirstyice Date: Sat, 29 Jun 2024 19:39:39 -0600 Subject: [PATCH 1/5] Allow redefinition of where logging goes --- Artnet/Receiver.h | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/Artnet/Receiver.h b/Artnet/Receiver.h index 004efe8..673178a 100644 --- a/Artnet/Receiver.h +++ b/Artnet/Receiver.h @@ -49,8 +49,8 @@ class Receiver_ if (size > PACKET_SIZE) { if (this->b_verbose) { - Serial.print(F("Packet size is unexpectedly too large: ")); - Serial.println(size); + log->print(F("Packet size is unexpectedly too large: ")); + log->println(size); } size = PACKET_SIZE; } @@ -58,7 +58,7 @@ class Receiver_ if (!checkID()) { if (this->b_verbose) { - Serial.println(F("Packet ID is not Art-Net")); + log->println(F("Packet ID is not Art-Net")); } return OpCode::ParseFailed; } @@ -121,8 +121,8 @@ class Receiver_ } default: { if (this->b_verbose) { - Serial.print(F("Unsupported OpCode: ")); - Serial.println(this->getOpCode(), HEX); + log->print(F("Unsupported OpCode: ")); + log->println(this->getOpCode(), HEX); } op_code = OpCode::Unsupported; break; @@ -140,19 +140,19 @@ class Receiver_ { if (net > 0x7F) { if (this->b_verbose) { - Serial.println(F("net should be less than 0x7F")); + log->println(F("net should be less than 0x7F")); } return; } if (subnet > 0xF) { if (this->b_verbose) { - Serial.println(F("subnet should be less than 0xF")); + log->println(F("subnet should be less than 0xF")); } return; } if (universe > 0xF) { if (this->b_verbose) { - Serial.println(F("universe should be less than 0xF")); + log->println(F("universe should be less than 0xF")); } return; } @@ -253,11 +253,11 @@ class Receiver_ n = num; } else { n = size / 3; - Serial.println(F("WARN: ArtNet packet size is less than requested LED numbers to forward")); - Serial.print(F(" requested: ")); - Serial.print(num * 3); - Serial.print(F(" received : ")); - Serial.println(size); + log->println(F("WARN: ArtNet packet size is less than requested LED numbers to forward")); + log->print(F(" requested: ")); + log->print(num * 3); + log->print(F(" received : ")); + log->println(size); } for (size_t pixel = 0; pixel < n; ++pixel) { size_t idx = pixel * 3; @@ -293,12 +293,22 @@ class Receiver_ this->b_verbose = b; } + void logOutputTo(Print* dest) { + log = dest; + } + protected: void attach(S& s) { this->stream = &s; } +#if defined(NO_GLOBAL_INSTANCES) || defined(NO_GLOBAL_SERIAL) + Print* log = nullptr; // No Serial -> no output +#else + Print* log = &Serial; // Use Serial by default +#endif + private: bool checkID() const { From 16a2d4654ee5606e29b26c58a904692fc1845f89 Mon Sep 17 00:00:00 2001 From: thirstyice Date: Sun, 30 Jun 2024 16:29:08 -0600 Subject: [PATCH 2/5] Log to nowhere by default --- Artnet/Common.h | 7 +++++++ Artnet/Receiver.h | 6 +----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Artnet/Common.h b/Artnet/Common.h index e5b2967..24d088e 100644 --- a/Artnet/Common.h +++ b/Artnet/Common.h @@ -85,6 +85,13 @@ struct Destination uint8_t universe; }; +class NoPrint : public Print { + size_t write(uint8_t) { + return 0; + } +}; +NoPrint NoLog; + inline bool operator<(const Destination &rhs, const Destination &lhs) { if (rhs.ip < lhs.ip) { diff --git a/Artnet/Receiver.h b/Artnet/Receiver.h index 673178a..e1ae4fe 100644 --- a/Artnet/Receiver.h +++ b/Artnet/Receiver.h @@ -303,11 +303,7 @@ class Receiver_ this->stream = &s; } -#if defined(NO_GLOBAL_INSTANCES) || defined(NO_GLOBAL_SERIAL) - Print* log = nullptr; // No Serial -> no output -#else - Print* log = &Serial; // Use Serial by default -#endif + Print* log = &NoLog; private: bool checkID() const From 470f12e8da0f927ff8b50316c986dce8ae534250 Mon Sep 17 00:00:00 2001 From: thirstyice Date: Sun, 30 Jun 2024 16:36:32 -0600 Subject: [PATCH 3/5] Remove b_verbose option With logging disabled by default via the log variable it's somewhat redundant --- Artnet/Receiver.h | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/Artnet/Receiver.h b/Artnet/Receiver.h index e1ae4fe..fd56b83 100644 --- a/Artnet/Receiver.h +++ b/Artnet/Receiver.h @@ -24,8 +24,6 @@ class Receiver_ art_trigger::CallbackType callback_art_trigger; ArtPollReplyConfig art_poll_reply_config; - bool b_verbose {false}; - public: #if ARX_HAVE_LIBSTDCPLUSPLUS >= 201103L // Have libstdc++11 #else @@ -48,18 +46,14 @@ class Receiver_ } if (size > PACKET_SIZE) { - if (this->b_verbose) { - log->print(F("Packet size is unexpectedly too large: ")); - log->println(size); - } + log->print(F("Packet size is unexpectedly too large: ")); + log->println(size); size = PACKET_SIZE; } this->stream->read(this->packet.data(), size); if (!checkID()) { - if (this->b_verbose) { - log->println(F("Packet ID is not Art-Net")); - } + log->println(F("Packet ID is not Art-Net")); return OpCode::ParseFailed; } @@ -120,10 +114,8 @@ class Receiver_ break; } default: { - if (this->b_verbose) { - log->print(F("Unsupported OpCode: ")); - log->println(this->getOpCode(), HEX); - } + log->print(F("Unsupported OpCode: ")); + log->println(this->getOpCode(), HEX); op_code = OpCode::Unsupported; break; } @@ -139,21 +131,15 @@ class Receiver_ -> std::enable_if_t::value> { if (net > 0x7F) { - if (this->b_verbose) { - log->println(F("net should be less than 0x7F")); - } + log->println(F("net should be less than 0x7F")); return; } if (subnet > 0xF) { - if (this->b_verbose) { - log->println(F("subnet should be less than 0xF")); - } + log->println(F("subnet should be less than 0xF")); return; } if (universe > 0xF) { - if (this->b_verbose) { - log->println(F("universe should be less than 0xF")); - } + log->println(F("universe should be less than 0xF")); return; } uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe; @@ -288,11 +274,6 @@ class Receiver_ this->art_poll_reply_config.node_report = node_report; } - void verbose(bool b) - { - this->b_verbose = b; - } - void logOutputTo(Print* dest) { log = dest; } From e07c98bbab5a6563cee84a1f768e38339949433c Mon Sep 17 00:00:00 2001 From: thirstyice Date: Sun, 30 Jun 2024 16:56:56 -0600 Subject: [PATCH 4/5] Update docs to reflect logging changes --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c2f6c8a..5b25797 100644 --- a/README.md +++ b/README.md @@ -355,8 +355,8 @@ void forwardArtDmxDataToFastLED(uint16_t universe, CRGB* leds, uint16_t num); // set information for artpollreply // https://art-net.org.uk/how-it-works/discovery-packets/artpollreply/ void setArtPollReplyConfig(uint16_t oem, uint16_t esta_man, uint8_t status1, uint8_t status2, const String &short_name, const String &long_name, const String &node_report); -// others -void verbose(bool b); +// Set where debug output should go (default is nowhere) +void logOutputTo(Print*); ``` ### Note From e4dbe53004cddb9e02836e2d8371887d07a02af9 Mon Sep 17 00:00:00 2001 From: thirstyice Date: Fri, 5 Jul 2024 18:37:14 -0600 Subject: [PATCH 5/5] Polishing logger --- Artnet/Common.h | 6 ------ Artnet/Receiver.h | 41 +++++++++++++++++++++++++---------------- README.md | 2 +- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/Artnet/Common.h b/Artnet/Common.h index 24d088e..3d16276 100644 --- a/Artnet/Common.h +++ b/Artnet/Common.h @@ -85,12 +85,6 @@ struct Destination uint8_t universe; }; -class NoPrint : public Print { - size_t write(uint8_t) { - return 0; - } -}; -NoPrint NoLog; inline bool operator<(const Destination &rhs, const Destination &lhs) { diff --git a/Artnet/Receiver.h b/Artnet/Receiver.h index fd56b83..4838372 100644 --- a/Artnet/Receiver.h +++ b/Artnet/Receiver.h @@ -11,6 +11,13 @@ namespace art_net { +class NoPrint : public Print { + size_t write(uint8_t) { + return 0; + } +}; +static NoPrint no_log; + template class Receiver_ { @@ -46,14 +53,14 @@ class Receiver_ } if (size > PACKET_SIZE) { - log->print(F("Packet size is unexpectedly too large: ")); - log->println(size); + logger->print(F("Packet size is unexpectedly too large: ")); + logger->println(size); size = PACKET_SIZE; } this->stream->read(this->packet.data(), size); if (!checkID()) { - log->println(F("Packet ID is not Art-Net")); + logger->println(F("Packet ID is not Art-Net")); return OpCode::ParseFailed; } @@ -114,8 +121,8 @@ class Receiver_ break; } default: { - log->print(F("Unsupported OpCode: ")); - log->println(this->getOpCode(), HEX); + logger->print(F("Unsupported OpCode: ")); + logger->println(this->getOpCode(), HEX); op_code = OpCode::Unsupported; break; } @@ -131,15 +138,15 @@ class Receiver_ -> std::enable_if_t::value> { if (net > 0x7F) { - log->println(F("net should be less than 0x7F")); + logger->println(F("net should be less than 0x7F")); return; } if (subnet > 0xF) { - log->println(F("subnet should be less than 0xF")); + logger->println(F("subnet should be less than 0xF")); return; } if (universe > 0xF) { - log->println(F("universe should be less than 0xF")); + logger->println(F("universe should be less than 0xF")); return; } uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe; @@ -239,11 +246,11 @@ class Receiver_ n = num; } else { n = size / 3; - log->println(F("WARN: ArtNet packet size is less than requested LED numbers to forward")); - log->print(F(" requested: ")); - log->print(num * 3); - log->print(F(" received : ")); - log->println(size); + logger->println(F("WARN: ArtNet packet size is less than requested LED numbers to forward")); + logger->print(F(" requested: ")); + logger->print(num * 3); + logger->print(F(" received : ")); + logger->println(size); } for (size_t pixel = 0; pixel < n; ++pixel) { size_t idx = pixel * 3; @@ -274,8 +281,8 @@ class Receiver_ this->art_poll_reply_config.node_report = node_report; } - void logOutputTo(Print* dest) { - log = dest; + void setLogger(Print* dest) { + logger = dest; } protected: @@ -284,9 +291,11 @@ class Receiver_ this->stream = &s; } - Print* log = &NoLog; private: + + Print* logger = &no_log; + bool checkID() const { const char* idptr = reinterpret_cast(this->packet.data()); diff --git a/README.md b/README.md index 5b25797..ec47b97 100644 --- a/README.md +++ b/README.md @@ -356,7 +356,7 @@ void forwardArtDmxDataToFastLED(uint16_t universe, CRGB* leds, uint16_t num); // https://art-net.org.uk/how-it-works/discovery-packets/artpollreply/ void setArtPollReplyConfig(uint16_t oem, uint16_t esta_man, uint8_t status1, uint8_t status2, const String &short_name, const String &long_name, const String &node_report); // Set where debug output should go (default is nowhere) -void logOutputTo(Print*); +void setLogger(Print*); ``` ### Note