Log.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "Log.h"
  24. #include "LinearAllocator.h"
  25. #include "StringStream.h"
  26. #include "Device.h"
  27. #include "RPCServer.h"
  28. namespace crown
  29. {
  30. static char g_buffer[16 * 1024];
  31. static LinearAllocator g_allocator(g_buffer, 16 * 1024);
  32. static StringStream g_stream(g_allocator);
  33. LogLevel Log::m_threshold = LL_DEBUG;
  34. //-----------------------------------------------------------------------------
  35. LogLevel Log::threshold()
  36. {
  37. return m_threshold;
  38. }
  39. //-----------------------------------------------------------------------------
  40. void Log::set_threshold(LogLevel threshold)
  41. {
  42. m_threshold = threshold;
  43. }
  44. //-----------------------------------------------------------------------------
  45. void Log::log_message(LogLevel level, const char* message, ::va_list arg)
  46. {
  47. if (level > m_threshold)
  48. {
  49. return;
  50. }
  51. switch (level)
  52. {
  53. case LL_DEBUG: g_stream << "D: "; break;
  54. case LL_ERROR: g_stream << "E: "; break;
  55. case LL_WARN: g_stream << "W: "; break;
  56. case LL_INFO: g_stream << "I: "; break;
  57. default: break;
  58. }
  59. char buf[1024];
  60. int len = vsnprintf(buf, 1024 - 2, message, arg);
  61. buf[len] = '\n';
  62. buf[len + 1] = '\0';
  63. g_stream << buf;
  64. }
  65. //-----------------------------------------------------------------------------
  66. void Log::d(const char* message, ...)
  67. {
  68. va_list args;
  69. va_start (args, message);
  70. log_message(LL_DEBUG, message, args);
  71. va_end (args);
  72. }
  73. //-----------------------------------------------------------------------------
  74. void Log::e(const char* message, ...)
  75. {
  76. va_list args;
  77. va_start (args, message);
  78. log_message(LL_ERROR, message, args);
  79. va_end (args);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void Log::w(const char* message, ...)
  83. {
  84. va_list args;
  85. va_start (args, message);
  86. log_message(LL_WARN, message, args);
  87. va_end (args);
  88. }
  89. //-----------------------------------------------------------------------------
  90. void Log::i(const char* message, ...)
  91. {
  92. va_list args;
  93. va_start (args, message);
  94. log_message(LL_INFO, message, args);
  95. va_end (args);
  96. }
  97. //-----------------------------------------------------------------------------
  98. void Log::flush()
  99. {
  100. os::printf(g_stream.c_str());
  101. ::fflush(stdout);
  102. if (device()->rpc() != NULL)
  103. {
  104. device()->rpc()->send_message_to_all(g_stream.c_str());
  105. }
  106. g_stream.clear();
  107. g_allocator.clear();
  108. }
  109. } // namespace crown