Logging.cpp 932 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include <stdarg.h>
  2. #include <cstdio>
  3. #include <cassert>
  4. #include "Logging.h"
  5. #include "Output.h"
  6. using namespace il2cpp::utils;
  7. static void DefaultLogCallback(const char* message)
  8. {
  9. Output::WriteToStdout(message);
  10. Output::WriteToStdout("\n");
  11. }
  12. Il2CppLogCallback Logging::s_Callback = DefaultLogCallback;
  13. void Logging::Write(const char* format, ...)
  14. {
  15. IL2CPP_ASSERT(s_Callback != NULL);
  16. if (format == NULL)
  17. return;
  18. va_list va;
  19. va_start(va, format);
  20. const char* prefix = "[libil2cpp] ";
  21. const int bufferSize = 1024 * 5;
  22. char buffer[bufferSize];
  23. memcpy(buffer, prefix, 12);
  24. vsnprintf(buffer + 12, bufferSize - 12, format, va);
  25. s_Callback(buffer);
  26. va_end(va);
  27. }
  28. void Logging::SetLogCallback(Il2CppLogCallback method)
  29. {
  30. IL2CPP_ASSERT(method != NULL);
  31. s_Callback = method;
  32. }
  33. bool Logging::IsLogCallbackSet()
  34. {
  35. return s_Callback != DefaultLogCallback;
  36. }