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
Changes from 1 commit
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
36 changes: 23 additions & 13 deletions Artnet/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ 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 @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -293,12 +293,22 @@ class Receiver_
this->b_verbose = b;
}

void logOutputTo(Print* dest) {
thirstyice marked this conversation as resolved.
Show resolved Hide resolved
log = dest;
}

protected:
void attach(S& s)
{
this->stream = &s;
}

#if defined(NO_GLOBAL_INSTANCES) || defined(NO_GLOBAL_SERIAL)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you let me know what these preprocessors are?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're the same macros that allow disabling Serialet al in the esp32 arduino core (see here).
I had assumed when I was developing this that they were standard across all arduino cores, but it seems that they are not.

Print* log = nullptr; // No Serial -> no output
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if the user doesn’t set this, the program crashes. (nullptr->print() will be called)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further research: this is actually undefined behaviour (not neccesarily a crash), which is why I didn't catch it earlier.
Solution coming soon.

#else
Print* log = &Serial; // Use Serial by default
#endif

private:
bool checkID() const
{
Expand Down