Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

125 строки
2.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2017 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #if defined(_WIN32)
  16. # define WIN32_LEAN_AND_MEAN
  17. # include <windows.h>
  18. # undef WIN32_LEAN_AND_MEAN
  19. #endif
  20. #if defined(__ANDROID__)
  21. # include <android/log.h>
  22. # include <unistd.h> /* for gettid() */
  23. #else
  24. # include <cstdarg>
  25. #endif
  26. namespace lol
  27. {
  28. /*
  29. * Public log class
  30. */
  31. void msg::debug(char const *fmt, ...)
  32. {
  33. va_list ap;
  34. va_start(ap, fmt);
  35. helper(MessageType::Debug, fmt, ap);
  36. va_end(ap);
  37. }
  38. void msg::info(char const *fmt, ...)
  39. {
  40. va_list ap;
  41. va_start(ap, fmt);
  42. helper(MessageType::Info, fmt, ap);
  43. va_end(ap);
  44. }
  45. void msg::warn(char const *fmt, ...)
  46. {
  47. va_list ap;
  48. va_start(ap, fmt);
  49. helper(MessageType::Warning, fmt, ap);
  50. va_end(ap);
  51. }
  52. void msg::error(char const *fmt, ...)
  53. {
  54. va_list ap;
  55. va_start(ap, fmt);
  56. helper(MessageType::Error, fmt, ap);
  57. va_end(ap);
  58. }
  59. /*
  60. * Private helper function
  61. */
  62. void msg::helper(MessageType type, char const *fmt, va_list ap)
  63. {
  64. /* Unless this is a debug build, ignore debug messages unless
  65. * the LOL_DEBUG environment variable is set. */
  66. #if !defined LOL_BUILD_DEBUG
  67. if (type == MessageType::Debug)
  68. {
  69. static char const *var = getenv("LOL_DEBUG");
  70. static bool const disable_debug = !var || !var[0];
  71. if (disable_debug)
  72. return;
  73. }
  74. #endif
  75. #if defined __ANDROID__
  76. static int const prio[] =
  77. {
  78. ANDROID_LOG_DEBUG,
  79. ANDROID_LOG_INFO,
  80. ANDROID_LOG_WARN,
  81. ANDROID_LOG_ERROR
  82. };
  83. std::string buf = vformat(fmt, ap);
  84. __android_log_print(prio[(int)type], "LOL", "[%d] %s", (int)gettid(), &buf[0]);
  85. #else
  86. static char const * const prefix[] =
  87. {
  88. "DEBUG",
  89. "INFO",
  90. "WARN",
  91. "ERROR",
  92. };
  93. # if defined _WIN32
  94. std::string buf = std::string(prefix[(int)type]) + ": " + vformat(fmt, ap);
  95. array<WCHAR> widechar;
  96. widechar.resize(buf.length() + 1);
  97. MultiByteToWideChar(CP_UTF8, 0, buf.c_str(), buf.length() + 1, widechar.data(), widechar.count());
  98. OutputDebugStringW(widechar.data());
  99. # else
  100. fprintf(stderr, "%s: ", prefix[(int)type]);
  101. vfprintf(stderr, fmt, ap);
  102. # endif
  103. #endif
  104. }
  105. } /* namespace lol */