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.
 
 
 

116 lines
2.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstdio>
  14. #ifdef 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. char buf[4096];
  72. vsnprintf(buf, 4095, fmt, ap);
  73. buf[4095] = '\0';
  74. __android_log_print(prio[type], "LOL", "[%d] %s", (int)gettid(), buf);
  75. #else
  76. char const *prefix[] =
  77. {
  78. "DEBUG",
  79. "INFO",
  80. "WARN",
  81. "ERROR",
  82. };
  83. # if defined _WIN32
  84. char buf[4096];
  85. vsnprintf(buf, 4095, fmt, ap);
  86. buf[4095] = '\0';
  87. OutputDebugString(prefix[type]);
  88. OutputDebugString(": ");
  89. OutputDebugString(buf);
  90. # else
  91. fprintf(stderr, "%s: ", prefix[type]);
  92. vfprintf(stderr, fmt, ap);
  93. # endif
  94. #endif
  95. }
  96. } /* namespace lol */