You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

111 lines
2.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 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. #if defined(_WIN32)
  15. # define WIN32_LEAN_AND_MEAN
  16. # include <windows.h>
  17. #endif
  18. #if defined(__ANDROID__)
  19. # include <android/log.h>
  20. # include <unistd.h> /* for gettid() */
  21. #else
  22. # include <cstdarg>
  23. #endif
  24. namespace lol
  25. {
  26. /*
  27. * Public log class
  28. */
  29. void msg::debug(char const *fmt, ...)
  30. {
  31. va_list ap;
  32. va_start(ap, fmt);
  33. helper(MessageType::Debug, fmt, ap);
  34. va_end(ap);
  35. }
  36. void msg::info(char const *fmt, ...)
  37. {
  38. va_list ap;
  39. va_start(ap, fmt);
  40. helper(MessageType::Info, fmt, ap);
  41. va_end(ap);
  42. }
  43. void msg::warn(char const *fmt, ...)
  44. {
  45. va_list ap;
  46. va_start(ap, fmt);
  47. helper(MessageType::Warning, fmt, ap);
  48. va_end(ap);
  49. }
  50. void msg::error(char const *fmt, ...)
  51. {
  52. va_list ap;
  53. va_start(ap, fmt);
  54. helper(MessageType::Error, fmt, ap);
  55. va_end(ap);
  56. }
  57. /*
  58. * Private helper function
  59. */
  60. void msg::helper(MessageType type, char const *fmt, va_list ap)
  61. {
  62. #if defined __ANDROID__
  63. static int const prio[] =
  64. {
  65. ANDROID_LOG_DEBUG,
  66. ANDROID_LOG_INFO,
  67. ANDROID_LOG_WARN,
  68. ANDROID_LOG_ERROR
  69. };
  70. String buf = String::vformat(fmt, ap);
  71. __android_log_print(prio[(int)type], "LOL", "[%d] %s", (int)gettid(), &buf[0]);
  72. #else
  73. static char const * const prefix[] =
  74. {
  75. "DEBUG",
  76. "INFO",
  77. "WARN",
  78. "ERROR",
  79. };
  80. # if defined _WIN32
  81. String buf = String(prefix[(int)type]) + ": " + String::vformat(fmt, ap);
  82. array<WCHAR> widechar;
  83. widechar.resize(buf.count() + 1);
  84. MultiByteToWideChar(CP_UTF8, 0, buf.C(), buf.count() + 1, widechar.data(), widechar.count());
  85. OutputDebugStringW(widechar.data());
  86. # else
  87. fprintf(stderr, "%s: ", prefix[(int)type]);
  88. vfprintf(stderr, fmt, ap);
  89. # endif
  90. #endif
  91. }
  92. } /* namespace lol */