Logger.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. flush();
  35. }
  36. else if(funcPtr == ::flush)
  37. {
  38. flush();
  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_, const char* msg)
  56. {
  57. file = file_;
  58. line = line_;
  59. func = func_;
  60. append(msg, strlen(msg));
  61. flush();
  62. }
  63. //======================================================================================================================
  64. // execCommonConstructionCode =
  65. //======================================================================================================================
  66. void Logger::execCommonConstructionCode()
  67. {
  68. sptr = &streamBuf[0];
  69. memset(sptr, '?', STREAM_SIZE);
  70. func = file = "error";
  71. line = -1;
  72. }
  73. //======================================================================================================================
  74. // append =
  75. //======================================================================================================================
  76. void Logger::append(const char* cstr, int len)
  77. {
  78. if(len > STREAM_SIZE - 1)
  79. {
  80. return;
  81. }
  82. int charsLeft = &streamBuf[STREAM_SIZE - 1] - sptr; // Leaving an extra char for the '\0'
  83. // Overflow
  84. if(len > charsLeft)
  85. {
  86. // Handle overflow
  87. memcpy(sptr, cstr, charsLeft);
  88. sptr += charsLeft;
  89. flush();
  90. append(cstr + charsLeft, len - charsLeft);
  91. return;
  92. }
  93. memcpy(sptr, cstr, len);
  94. sptr += len;
  95. }
  96. //======================================================================================================================
  97. // flush =
  98. //======================================================================================================================
  99. void Logger::flush()
  100. {
  101. *sptr = '\0';
  102. sig(file, line, func, &streamBuf[0]);
  103. // Reset
  104. sptr = &streamBuf[0];
  105. func = file = "error";
  106. line = -1;
  107. }