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.

170 lines
3.8 KiB

7 years ago
6 years ago
6 years ago
7 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. #if DEBUG_UDP_SUPPORT
  7. #include <WiFiUdp.h>
  8. WiFiUDP _udp_debug;
  9. #if DEBUG_UDP_PORT == 514
  10. char _udp_syslog_header[40] = {0};
  11. #endif
  12. #endif
  13. #if DEBUG_SERIAL_SUPPORT
  14. void _debugSendSerial(const char* prefix, const char* data) {
  15. if (prefix && (prefix[0] != '\0')) {
  16. Serial.print(prefix);
  17. }
  18. Serial.print(data);
  19. }
  20. #endif
  21. #if DEBUG_TELNET_SUPPORT
  22. void _debugSendTelnet(const char* prefix, const char* data) {
  23. if (prefix && (prefix[0] != '\0')) {
  24. _telnetWrite(prefix);
  25. }
  26. _telnetWrite(data);
  27. }
  28. #endif
  29. void _debugSend(const char * message) {
  30. const size_t msg_len = strlen(message);
  31. bool pause = false;
  32. char timestamp[10] = {0};
  33. #if DEBUG_ADD_TIMESTAMP
  34. static bool add_timestamp = true;
  35. if (add_timestamp) {
  36. snprintf(timestamp, sizeof(timestamp), "[%06lu] ", millis() % 1000000);
  37. }
  38. add_timestamp = (message[msg_len - 1] == 10) || (message[msg_len - 1] == 13);
  39. #endif
  40. #if DEBUG_SERIAL_SUPPORT
  41. _debugSendSerial(timestamp, message);
  42. #endif
  43. #if DEBUG_UDP_SUPPORT
  44. #if SYSTEM_CHECK_ENABLED
  45. if (systemCheck()) {
  46. #endif
  47. _udp_debug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  48. #if DEBUG_UDP_PORT == 514
  49. _udp_debug.write(_udp_syslog_header);
  50. #endif
  51. _udp_debug.write(message);
  52. _udp_debug.endPacket();
  53. pause = true;
  54. #if SYSTEM_CHECK_ENABLED
  55. }
  56. #endif
  57. #endif
  58. #if DEBUG_TELNET_SUPPORT
  59. _debugSendTelnet(timestamp, message);
  60. pause = true;
  61. #endif
  62. #if DEBUG_WEB_SUPPORT
  63. wsDebugSend(timestamp, message);
  64. pause = true;
  65. #endif
  66. if (pause) optimistic_yield(100);
  67. }
  68. // -----------------------------------------------------------------------------
  69. void debugSend(const char * format, ...) {
  70. va_list args;
  71. va_start(args, format);
  72. char test[1];
  73. int len = ets_vsnprintf(test, 1, format, args) + 1;
  74. char * buffer = new char[len];
  75. ets_vsnprintf(buffer, len, format, args);
  76. va_end(args);
  77. _debugSend(buffer);
  78. delete[] buffer;
  79. }
  80. void debugSend_P(PGM_P format_P, ...) {
  81. char format[strlen_P(format_P)+1];
  82. memcpy_P(format, format_P, sizeof(format));
  83. va_list args;
  84. va_start(args, format_P);
  85. char test[1];
  86. int len = ets_vsnprintf(test, 1, format, args) + 1;
  87. char * buffer = new char[len];
  88. ets_vsnprintf(buffer, len, format, args);
  89. va_end(args);
  90. _debugSend(buffer);
  91. delete[] buffer;
  92. }
  93. #if DEBUG_WEB_SUPPORT
  94. void debugWebSetup() {
  95. wsOnSendRegister([](JsonObject& root) {
  96. root["dbgVisible"] = 1;
  97. });
  98. wsOnActionRegister([](uint32_t client_id, const char * action, JsonObject& data) {
  99. #if TERMINAL_SUPPORT
  100. if (strcmp(action, "dbgcmd") == 0) {
  101. const char* command = data.get<const char*>("command");
  102. char buffer[strlen(command) + 2];
  103. snprintf(buffer, sizeof(buffer), "%s\n", command);
  104. terminalInject((void*) buffer, strlen(buffer));
  105. }
  106. #endif
  107. });
  108. #if DEBUG_UDP_SUPPORT
  109. #if DEBUG_UDP_PORT == 514
  110. snprintf_P(_udp_syslog_header, sizeof(_udp_syslog_header), PSTR("<%u>%s ESPurna[0]: "), DEBUG_UDP_FAC_PRI, getSetting("hostname").c_str());
  111. #endif
  112. #endif
  113. }
  114. #endif // DEBUG_WEB_SUPPORT
  115. // -----------------------------------------------------------------------------
  116. void debugSetup() {
  117. #if DEBUG_SERIAL_SUPPORT
  118. DEBUG_PORT.begin(SERIAL_BAUDRATE);
  119. #if DEBUG_ESP_WIFI
  120. DEBUG_PORT.setDebugOutput(true);
  121. #endif
  122. #endif
  123. }
  124. #endif // DEBUG_SUPPORT