Log.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. OBJECTTYPESTATIC(Log);
  41. SharedPtr<File> Log::logFile_;
  42. String Log::lastMessage_;
  43. #ifdef _DEBUG
  44. int Log::level_ = LOG_DEBUG;
  45. #else
  46. int Log::level_ = LOG_INFO;
  47. #endif
  48. bool Log::timeStamp_ = true;
  49. bool Log::inWrite_ = false;
  50. bool Log::quiet_ = false;
  51. static PODVector<Log*> logInstances;
  52. Log::Log(Context* context) :
  53. Object(context)
  54. {
  55. MutexLock lock(GetStaticMutex());
  56. logInstances.Push(this);
  57. }
  58. Log::~Log()
  59. {
  60. MutexLock lock(GetStaticMutex());
  61. logInstances.Remove(this);
  62. // Close log file if was last instance
  63. if (logInstances.Empty())
  64. logFile_.Reset();
  65. }
  66. void Log::Open(const String& fileName)
  67. {
  68. #if !defined(ANDROID) && !defined(IOS)
  69. MutexLock lock(GetStaticMutex());
  70. // Only the first log instance actually opens the file, the rest are routed to it
  71. if ((logFile_ && logFile_->IsOpen()) || fileName.Empty())
  72. return;
  73. logFile_ = new File(context_);
  74. if (logFile_->Open(fileName, FILE_WRITE))
  75. Write(LOG_INFO, "Opened log file " + fileName);
  76. else
  77. {
  78. logFile_.Reset();
  79. Write(LOG_ERROR, "Failed to create log file " + fileName);
  80. }
  81. #endif
  82. }
  83. void Log::SetLevel(int level)
  84. {
  85. assert(level >= LOG_DEBUG && level < LOG_NONE);
  86. level_ = level;
  87. }
  88. void Log::SetTimeStamp(bool enable)
  89. {
  90. timeStamp_ = enable;
  91. }
  92. void Log::SetQuiet(bool quiet)
  93. {
  94. quiet_ = quiet;
  95. }
  96. String Log::GetLastMessage() const
  97. {
  98. MutexLock lock(GetStaticMutex());
  99. return lastMessage_;
  100. }
  101. void Log::Write(int level, const String& message)
  102. {
  103. assert(level >= LOG_DEBUG && level < LOG_NONE);
  104. // Check message level
  105. if (level_ > level)
  106. return;
  107. // Prevent recursion during log event
  108. if (inWrite_)
  109. return;
  110. {
  111. MutexLock lock(GetStaticMutex());
  112. String formattedMessage = levelPrefixes[level] + ": " + message;
  113. lastMessage_ = message;
  114. if (timeStamp_)
  115. formattedMessage = "[" + Time::GetTimeStamp() + "] " + formattedMessage;
  116. #if defined(ANDROID)
  117. int androidLevel = ANDROID_LOG_DEBUG + level;
  118. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  119. #elif defined(IOS)
  120. SDL_IOS_LogMessage(message.CString());
  121. #else
  122. if (quiet_)
  123. {
  124. // If in quiet mode, still print the error message to the standard error stream
  125. if (level == LOG_ERROR)
  126. PrintUnicodeLine(formattedMessage, true);
  127. }
  128. else
  129. PrintUnicodeLine(formattedMessage, level == LOG_ERROR);
  130. #endif
  131. if (logFile_)
  132. {
  133. logFile_->WriteLine(formattedMessage);
  134. logFile_->Flush();
  135. }
  136. // Log messages can be safely sent as an event only in single-instance mode
  137. if (logInstances.Size() == 1)
  138. {
  139. inWrite_ = true;
  140. using namespace LogMessage;
  141. VariantMap eventData;
  142. eventData[P_MESSAGE] = formattedMessage;
  143. logInstances[0]->SendEvent(E_LOGMESSAGE, eventData);
  144. inWrite_ = false;
  145. }
  146. }
  147. }
  148. void Log::WriteRaw(const String& message)
  149. {
  150. // Prevent recursion during log event
  151. if (inWrite_)
  152. return;
  153. {
  154. MutexLock lock(GetStaticMutex());
  155. lastMessage_ = message;
  156. #if defined(ANDROID)
  157. __android_log_print(ANDROID_LOG_INFO, "Urho3D", message.CString());
  158. #elif defined(IOS)
  159. SDL_IOS_LogMessage(message.CString());
  160. #else
  161. if (!quiet_)
  162. PrintUnicode(message);
  163. #endif
  164. if (logFile_)
  165. {
  166. logFile_->Write(message.CString(), message.Length());
  167. logFile_->Flush();
  168. }
  169. // Log messages can be safely sent as an event only in single-instance mode
  170. if (logInstances.Size() == 1)
  171. {
  172. inWrite_ = true;
  173. using namespace LogMessage;
  174. VariantMap eventData;
  175. eventData[P_MESSAGE] = message;
  176. logInstances[0]->SendEvent(E_LOGMESSAGE, eventData);
  177. inWrite_ = false;
  178. }
  179. }
  180. }
  181. }