Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

113 rader
2.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2014 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://www.wtfpl.net/ for more details.
  9. //
  10. #if defined(HAVE_CONFIG_H)
  11. # include "config.h"
  12. #endif
  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. #include "core.h"
  25. namespace lol
  26. {
  27. /*
  28. * Public Log class
  29. */
  30. void Log::Debug(char const *fmt, ...)
  31. {
  32. va_list ap;
  33. va_start(ap, fmt);
  34. Helper(DebugMessage, fmt, ap);
  35. va_end(ap);
  36. }
  37. void Log::Info(char const *fmt, ...)
  38. {
  39. va_list ap;
  40. va_start(ap, fmt);
  41. Helper(InfoMessage, fmt, ap);
  42. va_end(ap);
  43. }
  44. void Log::Warn(char const *fmt, ...)
  45. {
  46. va_list ap;
  47. va_start(ap, fmt);
  48. Helper(WarnMessage, fmt, ap);
  49. va_end(ap);
  50. }
  51. void Log::Error(char const *fmt, ...)
  52. {
  53. va_list ap;
  54. va_start(ap, fmt);
  55. Helper(ErrorMessage, fmt, ap);
  56. va_end(ap);
  57. }
  58. /*
  59. * Private helper function
  60. */
  61. void Log::Helper(MessageType type, char const *fmt, va_list ap)
  62. {
  63. #if defined __ANDROID__
  64. int const prio[] =
  65. {
  66. ANDROID_LOG_DEBUG,
  67. ANDROID_LOG_INFO,
  68. ANDROID_LOG_WARN,
  69. ANDROID_LOG_ERROR
  70. };
  71. String buf = String::VPrintf(fmt, ap);
  72. __android_log_print(prio[type], "LOL", "[%d] %s", (int)gettid(), &buf[0]);
  73. #else
  74. char const *prefix[] =
  75. {
  76. "DEBUG",
  77. "INFO",
  78. "WARN",
  79. "ERROR",
  80. };
  81. # if defined _WIN32
  82. String buf = String(prefix[type]) + ": " + String::VPrintf(fmt, ap);
  83. Array<WCHAR> widechar;
  84. widechar.Resize(buf.Count() + 1);
  85. MultiByteToWideChar(CP_UTF8, 0, buf.C(), buf.Count() + 1, widechar.Data(), widechar.Count());
  86. OutputDebugStringW(widechar.Data());
  87. # else
  88. fprintf(stderr, "%s: ", prefix[type]);
  89. vfprintf(stderr, fmt, ap);
  90. # endif
  91. #endif
  92. }
  93. } /* namespace lol */