experiments with Pylontech/GroWatt PV tech
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.

95 lines
1.8 KiB

  1. #ifndef circ_log_h
  2. #define circ_log_h
  3. #include <TimeLib.h> //https://github.com/PaulStoffregen/Time
  4. #include <stdlib.h>
  5. template <int elementCnt> class circular_log
  6. {
  7. private:
  8. char m_log[elementCnt];
  9. bool removeLastFromLog()
  10. {
  11. char* nextLine = strstr(m_log+1, "<BR>");
  12. if(nextLine == NULL)
  13. {
  14. return false;
  15. }
  16. int newLineLen = strlen(nextLine);
  17. memmove(m_log, nextLine, newLineLen);
  18. m_log[newLineLen] = '\0';
  19. return true;
  20. }
  21. public:
  22. circular_log()
  23. {
  24. memset(m_log, 0, sizeof(m_log));
  25. }
  26. const char* c_str() const { return m_log; }
  27. int freeSpace() const { return elementCnt - strlen(m_log) - 1; }
  28. void LogXml(const char* msg)
  29. {
  30. char szNew[256] = "";
  31. snprintf(szNew, sizeof(szNew)-1, "<BR>%02d - %02d:%02d | ", day(), hour(), minute());
  32. int ix = strlen(szNew);
  33. while(*msg != '\0' && ix < 250)
  34. {
  35. if(*msg == '<')
  36. {
  37. szNew[ix++] = '&';
  38. szNew[ix++] = 'l';
  39. szNew[ix++] = 't';
  40. szNew[ix++] = ';';
  41. }
  42. else if(*msg == '>')
  43. {
  44. szNew[ix++] = '&';
  45. szNew[ix++] = 'g';
  46. szNew[ix++] = 't';
  47. szNew[ix++] = ';';
  48. }
  49. else
  50. {
  51. szNew[ix++] = *msg;
  52. }
  53. msg++;
  54. }
  55. const int newLen = strlen(szNew);
  56. while(freeSpace() < newLen)
  57. {
  58. if(removeLastFromLog() == false)
  59. {
  60. return;
  61. }
  62. }
  63. strcat(m_log, szNew);
  64. }
  65. void Log(const char* msg)
  66. {
  67. char szNew[256] = "";
  68. snprintf(szNew, sizeof(szNew)-1, "<BR>%02d - %02d:%02d | %s", day(), hour(), minute(), msg);
  69. const int newLen = strlen(szNew);
  70. while(freeSpace() < newLen)
  71. {
  72. if(removeLastFromLog() == false)
  73. {
  74. return;
  75. }
  76. }
  77. strcat(m_log, szNew);
  78. }
  79. };
  80. #endif //circular_log_h