Log.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "File.h"
  26. #include "IOEvents.h"
  27. #include "Log.h"
  28. #include "ProcessUtils.h"
  29. #include <cstdio>
  30. #include <ctime>
  31. #ifdef ANDROID
  32. #include <android/log.h>
  33. #endif
  34. #ifdef IOS
  35. extern "C" void SDL_IOS_LogMessage(const char* message);
  36. #endif
  37. #include "DebugNew.h"
  38. static const String levelPrefixes[] =
  39. {
  40. "DEBUG",
  41. "INFO",
  42. "WARNING",
  43. "ERROR"
  44. };
  45. OBJECTTYPESTATIC(Log);
  46. Log::Log(Context* context) :
  47. Object(context),
  48. #if defined(_DEBUG) || defined(XCODE_DEBUG_CONFIGURATION)
  49. level_(LOG_DEBUG),
  50. #else
  51. level_(LOG_INFO),
  52. #endif
  53. inWrite_(false)
  54. {
  55. }
  56. Log::~Log()
  57. {
  58. }
  59. void Log::Open(const String& fileName)
  60. {
  61. #if !defined(ANDROID) && !defined(IOS)
  62. if (fileName.Empty())
  63. return;
  64. logFile_ = new File(context_);
  65. if (logFile_->Open(fileName, FILE_WRITE))
  66. Write(LOG_INFO, "Opened log file " + fileName);
  67. else
  68. {
  69. logFile_.Reset();
  70. Write(LOG_ERROR, "Failed to create log file " + fileName);
  71. }
  72. #endif
  73. }
  74. void Log::Write(int level, const String& message)
  75. {
  76. // Prevent recursion
  77. if (inWrite_)
  78. return;
  79. // Check message level
  80. if (level_ > level || level < LOG_DEBUG || level >= LOG_NONE)
  81. return;
  82. inWrite_ = true;
  83. lastMessage_ = message;
  84. time_t sysTime;
  85. time(&sysTime);
  86. const char* dateTime = ctime(&sysTime);
  87. String dateTimeString = String(dateTime).Replaced("\n", "");
  88. String formattedMessage = "[" + dateTimeString + "] " + levelPrefixes[level] + ": " + message;
  89. #if defined(ANDROID)
  90. int androidLevel = ANDROID_LOG_DEBUG + level;
  91. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  92. #elif defined(IOS)
  93. SDL_IOS_LogMessage(message.CString());
  94. #else
  95. PrintUnicodeLine(formattedMessage);
  96. #endif
  97. if (logFile_)
  98. logFile_->WriteLine(formattedMessage);
  99. using namespace LogMessage;
  100. VariantMap eventData;
  101. eventData[P_MESSAGE] = formattedMessage;
  102. SendEvent(E_LOGMESSAGE, eventData);
  103. inWrite_ = false;
  104. }
  105. void Log::WriteRaw(const String& message)
  106. {
  107. // Prevent recursion
  108. if (inWrite_)
  109. return;
  110. inWrite_ = true;
  111. lastMessage_ = message;
  112. #if defined(ANDROID)
  113. __android_log_print(ANDROID_LOG_INFO, "Urho3D", message.CString());
  114. #elif defined(IOS)
  115. SDL_IOS_LogMessage(message.CString());
  116. #else
  117. PrintUnicode(message);
  118. #endif
  119. if (logFile_)
  120. {
  121. logFile_->Write(message.CString(), message.Length());
  122. logFile_->Flush();
  123. }
  124. using namespace LogMessage;
  125. VariantMap eventData;
  126. eventData[P_MESSAGE] = message;
  127. SendEvent(E_LOGMESSAGE, eventData);
  128. inWrite_ = false;
  129. }
  130. void Log::SetLevel(int level)
  131. {
  132. level_ = level;
  133. }
  134. void WriteToLog(Context* context, int level, const String& message)
  135. {
  136. Log* log = context->GetSubsystem<Log>();
  137. if (log)
  138. log->Write(level, message);
  139. }
  140. void WriteToLogRaw(Context* context, const String& message)
  141. {
  142. Log* log = context->GetSubsystem<Log>();
  143. if (log)
  144. log->WriteRaw(message);
  145. }