Log.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Context.h"
  24. #include "File.h"
  25. #include "IOEvents.h"
  26. #include "Log.h"
  27. #include "Mutex.h"
  28. #include "ProcessUtils.h"
  29. #include "Timer.h"
  30. #include <cstdio>
  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. namespace Urho3D
  39. {
  40. const char* logLevelPrefixes[] =
  41. {
  42. "DEBUG",
  43. "INFO",
  44. "WARNING",
  45. "ERROR"
  46. };
  47. static Log* logInstance = 0;
  48. Log::Log(Context* context) :
  49. Object(context),
  50. #ifdef _DEBUG
  51. level_(LOG_DEBUG),
  52. #else
  53. level_(LOG_INFO),
  54. #endif
  55. timeStamp_(true),
  56. inWrite_(false),
  57. quiet_(false)
  58. {
  59. logInstance = this;
  60. }
  61. Log::~Log()
  62. {
  63. logInstance = 0;
  64. }
  65. void Log::Open(const String& fileName)
  66. {
  67. #if !defined(ANDROID) && !defined(IOS)
  68. if (fileName.Empty())
  69. return;
  70. if (logFile_ && logFile_->IsOpen())
  71. {
  72. if (logFile_->GetName() == fileName)
  73. return;
  74. else
  75. Close();
  76. }
  77. logFile_ = new File(context_);
  78. if (logFile_->Open(fileName, FILE_WRITE))
  79. Write(LOG_INFO, "Opened log file " + fileName);
  80. else
  81. {
  82. logFile_.Reset();
  83. Write(LOG_ERROR, "Failed to create log file " + fileName);
  84. }
  85. #endif
  86. }
  87. void Log::Close()
  88. {
  89. #if !defined(ANDROID) && !defined(IOS)
  90. if (logFile_ && logFile_->IsOpen())
  91. {
  92. logFile_->Close();
  93. logFile_.Reset();
  94. }
  95. #endif
  96. }
  97. void Log::SetLevel(int level)
  98. {
  99. assert(level >= LOG_DEBUG && level < LOG_NONE);
  100. level_ = level;
  101. }
  102. void Log::SetTimeStamp(bool enable)
  103. {
  104. timeStamp_ = enable;
  105. }
  106. void Log::SetQuiet(bool quiet)
  107. {
  108. quiet_ = quiet;
  109. }
  110. void Log::Write(int level, const String& message)
  111. {
  112. assert(level >= LOG_DEBUG && level < LOG_NONE);
  113. // Do not log if message level excluded or if currently sending a log event
  114. if (!logInstance || logInstance->level_ > level || logInstance->inWrite_)
  115. return;
  116. String formattedMessage = logLevelPrefixes[level];
  117. formattedMessage += ": " + message;
  118. logInstance->lastMessage_ = message;
  119. if (logInstance->timeStamp_)
  120. formattedMessage = "[" + Time::GetTimeStamp() + "] " + formattedMessage;
  121. #if defined(ANDROID)
  122. int androidLevel = ANDROID_LOG_DEBUG + level;
  123. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  124. #elif defined(IOS)
  125. SDL_IOS_LogMessage(message.CString());
  126. #else
  127. if (logInstance->quiet_)
  128. {
  129. // If in quiet mode, still print the error message to the standard error stream
  130. if (level == LOG_ERROR)
  131. PrintUnicodeLine(formattedMessage, true);
  132. }
  133. else
  134. PrintUnicodeLine(formattedMessage, level == LOG_ERROR);
  135. #endif
  136. if (logInstance->logFile_)
  137. {
  138. logInstance->logFile_->WriteLine(formattedMessage);
  139. logInstance->logFile_->Flush();
  140. }
  141. logInstance->inWrite_ = true;
  142. using namespace LogMessage;
  143. VariantMap eventData;
  144. eventData[P_MESSAGE] = formattedMessage;
  145. eventData[P_LEVEL] = level;
  146. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  147. logInstance->inWrite_ = false;
  148. }
  149. void Log::WriteRaw(const String& message, bool error)
  150. {
  151. // Prevent recursion during log event
  152. if (!logInstance || logInstance->inWrite_)
  153. return;
  154. logInstance->lastMessage_ = message;
  155. #if defined(ANDROID)
  156. __android_log_print(ANDROID_LOG_INFO, "Urho3D", message.CString());
  157. #elif defined(IOS)
  158. SDL_IOS_LogMessage(message.CString());
  159. #else
  160. if (logInstance->quiet_)
  161. {
  162. // If in quiet mode, still print the error message to the standard error stream
  163. if (error)
  164. PrintUnicode(message, true);
  165. }
  166. else
  167. PrintUnicode(message, error);
  168. #endif
  169. if (logInstance->logFile_)
  170. {
  171. logInstance->logFile_->Write(message.CString(), message.Length());
  172. logInstance->logFile_->Flush();
  173. }
  174. logInstance->inWrite_ = true;
  175. using namespace LogMessage;
  176. VariantMap eventData;
  177. eventData[P_MESSAGE] = message;
  178. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  179. logInstance->inWrite_ = false;
  180. }
  181. }