Skip to content

Commit

Permalink
Polishing logger
Browse files Browse the repository at this point in the history
  • Loading branch information
thirstyice committed Jul 6, 2024
1 parent e07c98b commit e4dbe53
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 23 deletions.
6 changes: 0 additions & 6 deletions Artnet/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
41 changes: 25 additions & 16 deletions Artnet/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

namespace art_net {

class NoPrint : public Print {
size_t write(uint8_t) {
return 0;
}
};
static NoPrint no_log;

template <typename S>
class Receiver_
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -131,15 +138,15 @@ class Receiver_
-> std::enable_if_t<arx::is_callable<Fn>::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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand All @@ -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<const char*>(this->packet.data());
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e4dbe53

Please sign in to comment.