Logger.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include <cstring>
  2. #include "Logger.h"
  3. //==============================================================================
  4. // operator<< [const void*] =
  5. //==============================================================================
  6. Logger& Logger::operator<<(const void* val)
  7. {
  8. return appendUsingLexicalCast(val);
  9. }
  10. //==============================================================================
  11. // operator<< [const char*] =
  12. //==============================================================================
  13. Logger& Logger::operator<<(const char* val)
  14. {
  15. append(val, strlen(val));
  16. return *this;
  17. }
  18. //==============================================================================
  19. // operator<< [std::string] =
  20. //==============================================================================
  21. Logger& Logger::operator<<(const std::string& val)
  22. {
  23. append(val.c_str(), val.length());
  24. return *this;
  25. }
  26. //==============================================================================
  27. // operator<< [Logger& (*funcPtr)(Logger&)] =
  28. //==============================================================================
  29. Logger& Logger::operator<<(Logger& (*funcPtr)(Logger&))
  30. {
  31. if(funcPtr == endl)
  32. {
  33. append("\n", 1);
  34. realFlush();
  35. }
  36. else if(funcPtr == flush)
  37. {
  38. realFlush();
  39. }
  40. return *this;
  41. }
  42. //==============================================================================
  43. // operator<< [LoggerSender] =
  44. //==============================================================================
  45. Logger& Logger::operator<<(const LoggerSender& sender)
  46. {
  47. file = sender.file;
  48. line = sender.line;
  49. func = sender.func;
  50. return *this;
  51. }
  52. //==============================================================================
  53. // write =
  54. //==============================================================================
  55. void Logger::write(const char* file_, int line_, const char* func_,
  56. const char* msg)
  57. {
  58. file = file_;
  59. line = line_;
  60. func = func_;
  61. append(msg, strlen(msg));
  62. realFlush();
  63. }
  64. //==============================================================================
  65. // execCommonConstructionCode =
  66. //==============================================================================
  67. void Logger::execCommonConstructionCode()
  68. {
  69. sptr = &streamBuf[0];
  70. memset(sptr, '?', STREAM_SIZE);
  71. func = file = "error";
  72. line = -1;
  73. }
  74. //==============================================================================
  75. // append =
  76. //==============================================================================
  77. void Logger::append(const char* cstr, int len)
  78. {
  79. if(len > STREAM_SIZE - 1)
  80. {
  81. const char ERR[] = "**logger buffer overflow**";
  82. cstr = ERR;
  83. len = sizeof(ERR);
  84. }
  85. int charsLeft = &streamBuf[STREAM_SIZE - 1] - sptr; // Leaving an extra
  86. // char for the '\0'
  87. // Overflow
  88. if(len > charsLeft)
  89. {
  90. // Handle overflow
  91. memcpy(sptr, cstr, charsLeft);
  92. sptr += charsLeft;
  93. realFlush();
  94. append(cstr + charsLeft, len - charsLeft);
  95. return;
  96. }
  97. memcpy(sptr, cstr, len);
  98. sptr += len;
  99. }
  100. //==============================================================================
  101. // realFlush =
  102. //==============================================================================
  103. void Logger::realFlush()
  104. {
  105. *sptr = '\0';
  106. sig(file, line, func, &streamBuf[0]);
  107. // Reset
  108. sptr = &streamBuf[0];
  109. func = file = "error";
  110. line = -1;
  111. }