arcao / Syslog

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

How can we change const char* arguments during the application running? #25

Open kamotsuru opened 3 years ago

kamotsuru commented 3 years ago

I want to change the value of some const char* arguments of syslog() constructor during the application running.

The following is some parts of my code. Inside loop(), I copied a string derived from the serial input to std::string object. If I create const char* variable "testapp" and put it to syslog.appName() like the following, it works. However, If I put the copied std::string object with the same string "testapp" replacing with the commented out // line, the library sends other value than "testapp". Even if I put the same string "testapp" multiple times, it changes. It seems to refer the different memory address's value.

This may be c++ programing issue, but could you let me know how to solve this if possible?

//global scope Syslog syslog(udpClient, server, SYSLOG_PORT, "esp32", LOG_KERN);

void loop() { ... if (Serial.available() > 0) { String str = Serial.readStringUntil('\n'); if (str[0] == 'R') { ... } else if (str[0] == 'A') { std::string para = myOffset(str, 2); const char param = "testapp"; Serial.println(strcmp(para.c_str(), param)); // syslog.appName((const char)(para.c_str())); syslog.appName(param);
} } ... syslog.logf(LOG_INFO, "%s %d %f %f", d.getAddress().toString().c_str(), id, current, voltage); }

std::string myOffset (String string, int offset) { std::string para = ""; for (int i = offset; i < string.length(); i++) { para += string[i]; } return para; }