Logger.cpp 3.9 KB

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