Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.0 KiB

7 years ago
6 years ago
6 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. /*
  2. DEBUG MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DEBUG_SUPPORT
  6. #include "libs/DebugSend.h"
  7. #if DEBUG_UDP_SUPPORT
  8. #include <WiFiUdp.h>
  9. WiFiUDP _udp_debug;
  10. #if DEBUG_UDP_PORT == 514
  11. char _udp_syslog_header[40] = {0};
  12. #endif
  13. #endif
  14. #if DEBUG_SERIAL_SUPPORT
  15. void _debugSendSerial(const char* prefix, const char* data) {
  16. if (prefix && (prefix[0] != '\0')) {
  17. DEBUG_PORT.print(prefix);
  18. }
  19. DEBUG_PORT.print(data);
  20. }
  21. #endif
  22. #if DEBUG_TELNET_SUPPORT
  23. void _debugSendTelnet(const char* prefix, const char* data) {
  24. if (prefix && (prefix[0] != '\0')) {
  25. _telnetWrite(prefix);
  26. }
  27. _telnetWrite(data);
  28. }
  29. #endif
  30. void debugSendImpl(const char * message) {
  31. const size_t msg_len = strlen(message);
  32. bool pause = false;
  33. char timestamp[10] = {0};
  34. #if DEBUG_ADD_TIMESTAMP
  35. static bool add_timestamp = true;
  36. if (add_timestamp) {
  37. snprintf(timestamp, sizeof(timestamp), "[%06lu] ", millis() % 1000000);
  38. }
  39. add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13);
  40. #endif
  41. #if DEBUG_SERIAL_SUPPORT
  42. _debugSendSerial(timestamp, message);
  43. #endif
  44. #if DEBUG_UDP_SUPPORT
  45. #if SYSTEM_CHECK_ENABLED
  46. if (systemCheck()) {
  47. #endif
  48. _udp_debug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  49. #if DEBUG_UDP_PORT == 514
  50. _udp_debug.write(_udp_syslog_header);
  51. #endif
  52. _udp_debug.write(message);
  53. _udp_debug.endPacket();
  54. pause = true;
  55. #if SYSTEM_CHECK_ENABLED
  56. }
  57. #endif
  58. #endif
  59. #if DEBUG_TELNET_SUPPORT
  60. _debugSendTelnet(timestamp, message);
  61. pause = true;
  62. #endif
  63. #if DEBUG_WEB_SUPPORT
  64. wsDebugSend(timestamp, message);
  65. pause = true;
  66. #endif
  67. if (pause) optimistic_yield(100);
  68. }
  69. #if DEBUG_WEB_SUPPORT
  70. void debugWebSetup() {
  71. wsOnSendRegister([](JsonObject& root) {
  72. root["dbgVisible"] = 1;
  73. });
  74. wsOnActionRegister([](uint32_t client_id, const char * action, JsonObject& data) {
  75. #if TERMINAL_SUPPORT
  76. if (strcmp(action, "dbgcmd") == 0) {
  77. const char* command = data.get<const char*>("command");
  78. char buffer[strlen(command) + 2];
  79. snprintf(buffer, sizeof(buffer), "%s\n", command);
  80. terminalInject((void*) buffer, strlen(buffer));
  81. }
  82. #endif
  83. });
  84. #if DEBUG_UDP_SUPPORT
  85. #if DEBUG_UDP_PORT == 514
  86. snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
  87. #endif
  88. #endif
  89. }
  90. #endif // DEBUG_WEB_SUPPORT
  91. // -----------------------------------------------------------------------------
  92. void debugSetup() {
  93. #if DEBUG_SERIAL_SUPPORT
  94. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  95. #if DEBUG_ESP_WIFI
  96. DEBUG_PORT.setDebugOutput(true);
  97. #endif
  98. #endif
  99. }
  100. #endif // DEBUG_SUPPORT