logging.ino 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. void sendLog(const char *msg, uint8_t loglevel)
  2. {
  3. char buf[101];
  4. char buf2[121];
  5. static char tbuf[20];
  6. bool timeIsValid=false;
  7. strlcpy(buf, msg, sizeof(buf));
  8. if (confTime.ntpEnable)
  9. {
  10. updateTime();
  11. if(lt.tm_year > 70) { // lt.tm_year = years since 1900, before NTP is synced = 70
  12. //strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &lt);
  13. strftime(tbuf, sizeof(tbuf), "%H:%M:%S", &lt);
  14. sprintf(buf2, "[%s] %s\r\n", tbuf, buf);
  15. timeIsValid = true;
  16. }
  17. }
  18. if(!timeIsValid)
  19. {
  20. //unsigned long tmpMillis;
  21. unsigned long tmpSecs;
  22. //unsigned int restMillis;
  23. //tmpMillis = millis();
  24. //tmpSecs = tmpMillis / 1000;
  25. //restMillis = tmpMillis - (tmpSecs * 1000);
  26. tmpSecs = millis() / 1000;
  27. //sprintf(buf2, "[%u.%u] %s\r\n", tmpSecs, restMillis, buf);
  28. sprintf(buf2, "[%07lu] %s\r\n", tmpSecs, buf);
  29. }
  30. if(loglevel <= confLog.logLevelSerial) Serial.print(buf2);
  31. if (confMqtt.mqtt_enable && mqttclient.state() == 0)
  32. {
  33. if(loglevel <= confLog.logLevelMqtt) mqttclient.publish(confMqtt.mqtt_topic_out, buf, confMqtt.mqtt_outRetain);
  34. }
  35. yield();
  36. if (confWeb.enableConsole && webSocket.connectedClients() > 0)
  37. {
  38. if(loglevel <= confLog.logLevelWeb) webSocket.broadcastTXT(buf2);
  39. }
  40. }
  41. void sendLog(const __FlashStringHelper *msg, uint8_t loglevel)
  42. {
  43. char buf[501];
  44. PGM_P p = reinterpret_cast<PGM_P>(msg);
  45. size_t n = 0;
  46. while (1)
  47. {
  48. unsigned char c = pgm_read_byte(p++);
  49. if (c == 0) {
  50. buf[n] = c;
  51. break;
  52. }
  53. else if (n >= sizeof(buf)-1) {
  54. break;
  55. }
  56. else {
  57. buf[n] = c;
  58. n++;
  59. }
  60. }
  61. sendLog(buf, loglevel);
  62. }