log.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #pragma once
  6. #if defined(CROWN_DEBUG)
  7. #include "console_server.h"
  8. #include "os.h"
  9. namespace log_internal
  10. {
  11. using namespace crown;
  12. inline void logx(LogSeverity::Enum sev, const char* msg, va_list args)
  13. {
  14. char buf[2048];
  15. int len = vsnprintf(buf, sizeof(buf), msg, args);
  16. if (len > (int)sizeof(buf))
  17. len = sizeof(buf) - 1;
  18. buf[len] = '\0';
  19. console_server_globals::console().log_to_all(buf, sev);
  20. os::log(buf);
  21. }
  22. inline void logx(LogSeverity::Enum sev, const char* msg, ...)
  23. {
  24. va_list args;
  25. va_start(args, msg);
  26. logx(sev, msg, args);
  27. va_end(args);
  28. }
  29. }
  30. #define CE_LOGI(msg, ...) log_internal::logx(crown::LogSeverity::INFO, msg, ##__VA_ARGS__)
  31. #define CE_LOGD(msg, ...) log_internal::logx(crown::LogSeverity::DEBUG, msg, ##__VA_ARGS__)
  32. #define CE_LOGE(msg, ...) log_internal::logx(crown::LogSeverity::ERROR, msg, ##__VA_ARGS__)
  33. #define CE_LOGW(msg, ...) log_internal::logx(crown::LogSeverity::WARN, msg, ##__VA_ARGS__)
  34. #else
  35. #define CE_LOGI(msg, ...) ((void)0)
  36. #define CE_LOGD(msg, ...) ((void)0)
  37. #define CE_LOGE(msg, ...) ((void)0)
  38. #define CE_LOGW(msg, ...) ((void)0)
  39. #endif