arcao / Syslog

An Arduino library for logging to Syslog server in IETF format (RFC 5424) and BSD format (RFC 3164)
MIT License
118 stars 48 forks source link

Documentation or warning that Syslog constructor doesn't make deep copies #17

Open hydrogen18 opened 6 years ago

hydrogen18 commented 6 years ago

I just spent 1 or 2 hours trying to debug why this simple usage case does not work at all


Syslog *mySyslog;
WiFiUDP myUdp;
void start_syslog(void){
char hostname[64];
read_hostname_from_config(&hostname); // read NULL terminated string from EEPROM, SD-card, etc.
mySyslog = new Syslog(
        myUdp
        hostname,
        2222, 
        "myhostname",
        "appName"
        LOG_LOCAL3, 
        SYSLOG_PROTO_IETF);
}

Basically this code would run but when I try and use mySyslog->log I get false as a return value. I spent a while troubleshooting my code before I looked at your implementation of Syslog.cpp. It was pretty obvious that my problem was the constructor just stores the pointer, not copies it. If you read the configuration from some dynamic source it means you can't pass in a stack variable.

I switched to using a global array for my hostname and it worked fine.

I think this implementation is fine, but it might save others some headache if you added notes warning them the constructor does not make deep copies and the caller must keep the memory segment around and unaltered.

devbar commented 1 year ago

Great idea! I ran into same issue, thanks for comment