🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
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.

101 lines
2.3 KiB

  1. #include "config.h"
  2. #include "logging.h"
  3. #include <cstdarg>
  4. #include <cstdio>
  5. #include <string>
  6. #include "strutils.h"
  7. #include "vector.h"
  8. #ifdef _WIN32
  9. #define WIN32_LEAN_AND_MEAN
  10. #include <windows.h>
  11. void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
  12. {
  13. al::vector<char> dynmsg;
  14. char stcmsg[256];
  15. char *str{stcmsg};
  16. std::va_list args, args2;
  17. va_start(args, fmt);
  18. va_copy(args2, args);
  19. const int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
  20. if(unlikely(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)))
  21. {
  22. dynmsg.resize(static_cast<size_t>(msglen) + 1u);
  23. str = dynmsg.data();
  24. std::vsnprintf(str, dynmsg.size(), fmt, args2);
  25. }
  26. va_end(args2);
  27. va_end(args);
  28. if(gLogLevel >= level)
  29. {
  30. fputs(str, logfile);
  31. fflush(logfile);
  32. }
  33. /* OutputDebugStringW has no 'level' property to distinguish between
  34. * informational, warning, or error debug messages. So only print them for
  35. * non-Release builds.
  36. */
  37. #ifndef NDEBUG
  38. std::wstring wstr{utf8_to_wstr(str)};
  39. OutputDebugStringW(wstr.c_str());
  40. #endif
  41. }
  42. #else
  43. #ifdef __ANDROID__
  44. #include <android/log.h>
  45. #endif
  46. void al_print(LogLevel level, FILE *logfile, const char *fmt, ...)
  47. {
  48. al::vector<char> dynmsg;
  49. char stcmsg[256];
  50. char *str{stcmsg};
  51. std::va_list args, args2;
  52. va_start(args, fmt);
  53. va_copy(args2, args);
  54. const int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
  55. if(unlikely(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg)))
  56. {
  57. dynmsg.resize(static_cast<size_t>(msglen) + 1u);
  58. str = dynmsg.data();
  59. std::vsnprintf(str, dynmsg.size(), fmt, args2);
  60. }
  61. va_end(args2);
  62. va_end(args);
  63. if(gLogLevel >= level)
  64. {
  65. std::fputs(str, logfile);
  66. std::fflush(logfile);
  67. }
  68. #ifdef __ANDROID__
  69. auto android_severity = [](LogLevel l) noexcept
  70. {
  71. switch(l)
  72. {
  73. case LogLevel::Trace: return ANDROID_LOG_DEBUG;
  74. case LogLevel::Warning: return ANDROID_LOG_WARN;
  75. case LogLevel::Error: return ANDROID_LOG_ERROR;
  76. /* Should not happen. */
  77. case LogLevel::Disable:
  78. break;
  79. }
  80. return ANDROID_LOG_ERROR;
  81. };
  82. __android_log_print(android_severity(level), "openal", "%s", str);
  83. #endif
  84. }
  85. #endif