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

Allow redefinition of where logging goes #111

Merged
merged 5 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
7 changes: 7 additions & 0 deletions Artnet/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ struct Destination
uint8_t universe;
};

class NoPrint : public Print {
size_t write(uint8_t) {
return 0;
}
};
NoPrint NoLog;
thirstyice marked this conversation as resolved.
Show resolved Hide resolved

inline bool operator<(const Destination &rhs, const Destination &lhs)
{
if (rhs.ip < lhs.ip) {
Expand Down
47 changes: 17 additions & 30 deletions Artnet/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -48,18 +46,14 @@ 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;
}
this->stream->read(this->packet.data(), size);

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;
}

Expand Down Expand Up @@ -120,10 +114,8 @@ class Receiver_
break;
}
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;
}
Expand All @@ -139,21 +131,15 @@ class Receiver_
-> std::enable_if_t<arx::is_callable<Fn>::value>
{
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;
}
uint16_t u = ((uint16_t)net << 8) | ((uint16_t)subnet << 4) | (uint16_t)universe;
Expand Down Expand Up @@ -253,11 +239,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;
Expand Down Expand Up @@ -288,9 +274,8 @@ class Receiver_
this->art_poll_reply_config.node_report = node_report;
}

void verbose(bool b)
{
this->b_verbose = b;
void logOutputTo(Print* dest) {
thirstyice marked this conversation as resolved.
Show resolved Hide resolved
log = dest;
}

protected:
Expand All @@ -299,6 +284,8 @@ class Receiver_
this->stream = &s;
}

Print* log = &NoLog;
thirstyice marked this conversation as resolved.
Show resolved Hide resolved

private:
bool checkID() const
{
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading