logging.ino 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. void sendLog(const char *msg, uint8_t loglevel)
  2. {
  3. char buf[200];
  4. char buf2[221];
  5. bool timeIsValid = false;
  6. strlcpy(buf, msg, sizeof(buf));
  7. #ifdef ENABLE_FEATURE_NTP_TIME
  8. static char tbuf[20];
  9. if (confTime.ntpEnable)
  10. {
  11. updateTime();
  12. if (lt.tm_year > 70)
  13. { // lt.tm_year = years since 1900, before NTP is synced = 70
  14. //strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M:%S", &lt);
  15. strftime(tbuf, sizeof(tbuf), "%H:%M:%S", &lt);
  16. sprintf(buf2, "[%s] %s\r\n", tbuf, buf);
  17. timeIsValid = true;
  18. }
  19. }
  20. #endif
  21. if (!timeIsValid)
  22. {
  23. unsigned long tmpMillis;
  24. unsigned long tmpSecs;
  25. unsigned int restMillis;
  26. tmpMillis = millis();
  27. tmpSecs = tmpMillis / 1000;
  28. restMillis = tmpMillis - (tmpSecs * 1000);
  29. //tmpSecs = millis() / 1000;
  30. if (tmpSecs < 10000)
  31. sprintf(buf2, "[%04lu.%3u] %s\r\n", tmpSecs, restMillis, buf);
  32. else
  33. sprintf(buf2, "[%08lu] %s\r\n", tmpSecs, buf);
  34. }
  35. if (loglevel <= confLog.logLevelSerial)
  36. Serial.print(buf2);
  37. if (confMqtt.mqtt_enable && mqttclient.state() == 0)
  38. {
  39. if (loglevel <= confLog.logLevelMqtt)
  40. mqttclient.publish(confMqtt.mqtt_topic_out, buf, false);
  41. }
  42. #ifdef ENABLE_FEATURE_WEB_CONSOLE_WEBSOCKETS
  43. yield();
  44. if (confWeb.wsConsole && webSocket.connectedClients() > 0)
  45. {
  46. if (loglevel <= confLog.logLevelWeb)
  47. webSocket.broadcastTXT(buf2);
  48. }
  49. #endif
  50. #ifdef ENABLE_FEATURE_WEB_CONSOLE
  51. if (confWeb.wConsole && loglevel <= confLog.logLevelWeb)
  52. {
  53. char buf3[250];
  54. //char webLogBuffer[2000];
  55. //unsigned int webLogBuffer_index=0;
  56. sprintf(buf3, "{%u}%s", webLogBuffer_index, buf2);
  57. //Serial.print("DEV: '");
  58. //Serial.print(buf3);
  59. //Serial.println("'");
  60. //char * posmsg2 = 0;
  61. //posmsg2 = strstr(webLogBuffer,'\n');
  62. //Serial.print("POS=");
  63. //Serial.println((int)posmsg2);
  64. bool done = false;
  65. while (!done)
  66. {
  67. int currSize = strlen(webLogBuffer);
  68. int addSize = strlen(buf3);
  69. int maxSize = sizeof(webLogBuffer) - 10;
  70. //if(addSize < 100) addSize = 100;
  71. //Serial.print(" currSize="); Serial.print(currSize);
  72. //Serial.print(" addSize="); Serial.print(addSize);
  73. //Serial.print(" maxSize="); Serial.print(maxSize);
  74. if ((currSize + addSize) >= maxSize)
  75. {
  76. //Serial.print(" buf FULL, size=");
  77. //Serial.print(currSize);
  78. //Serial.print(" removing 1.msg...");
  79. int posMsg2 = 0;
  80. while (posMsg2 < currSize)
  81. {
  82. if (webLogBuffer[posMsg2] == '\n')
  83. {
  84. posMsg2++;
  85. break;
  86. }
  87. else
  88. posMsg2++;
  89. }
  90. //Serial.print(" posMsg2=");
  91. //Serial.print(posMsg2);
  92. int oldSize = strlen(webLogBuffer);
  93. int c = 0;
  94. while (c < (oldSize - posMsg2))
  95. {
  96. webLogBuffer[c] = webLogBuffer[posMsg2 + c];
  97. //Serial.println();
  98. //Serial.print(webLogBuffer[posMsg2 + c]);
  99. //Serial.print(" [");
  100. //Serial.print(posMsg2 + c);
  101. //Serial.print("] -> [");
  102. //Serial.print(c);
  103. //Serial.print("]");
  104. c++;
  105. }
  106. webLogBuffer[c] = '\0';
  107. //Serial.print(" removed msg 1, new size=");
  108. //Serial.print(strlen(webLogBuffer));
  109. }
  110. else
  111. {
  112. done = true;
  113. //Serial.println(" done");
  114. }
  115. //Serial.println();
  116. }
  117. if ((strlen(webLogBuffer) + strlen(buf3)) < (sizeof(webLogBuffer) - 10))
  118. {
  119. //Serial.print(" add msg to buf");
  120. int c = 0;
  121. int offset = strlen(webLogBuffer);
  122. //Serial.print(" offset=");
  123. //Serial.print(offset);
  124. int size = strlen(buf3);
  125. while (c < size)
  126. {
  127. if ((c + offset) < (int)sizeof(webLogBuffer))
  128. {
  129. webLogBuffer[c + offset] = buf3[c];
  130. c++;
  131. }
  132. else
  133. break;
  134. }
  135. webLogBuffer[c + offset] = '\0';
  136. //Serial.print("AFTER 2: ");
  137. //Serial.println(webLogBuffer);
  138. webLogBuffer_index++;
  139. }
  140. }
  141. #endif
  142. }
  143. void sendLog(const __FlashStringHelper *msg, uint8_t loglevel)
  144. {
  145. char buf[201];
  146. PGM_P p = reinterpret_cast<PGM_P>(msg);
  147. size_t n = 0;
  148. while (1)
  149. {
  150. unsigned char c = pgm_read_byte(p++);
  151. if (c == 0)
  152. {
  153. buf[n] = c;
  154. break;
  155. }
  156. else if (n >= sizeof(buf) - 1)
  157. {
  158. break;
  159. }
  160. else
  161. {
  162. buf[n] = c;
  163. n++;
  164. }
  165. }
  166. sendLog(buf, loglevel);
  167. }