Log.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. // Do not log if message level excluded or if currently sending a log event
  105. if (level_ > level || inWrite_)
  106. return;
  107. {
  108. MutexLock lock(GetStaticMutex());
  109. String formattedMessage = levelPrefixes[level] + ": " + message;
  110. lastMessage_ = message;
  111. if (timeStamp_)
  112. formattedMessage = "[" + Time::GetTimeStamp() + "] " + formattedMessage;
  113. #if defined(ANDROID)
  114. int androidLevel = ANDROID_LOG_DEBUG + level;
  115. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  116. #elif defined(IOS)
  117. SDL_IOS_LogMessage(message.CString());
  118. #else
  119. if (quiet_)
  120. {
  121. // If in quiet mode, still print the error message to the standard error stream
  122. if (level == LOG_ERROR)
  123. PrintUnicodeLine(formattedMessage, true);
  124. }
  125. else
  126. PrintUnicodeLine(formattedMessage, level == LOG_ERROR);
  127. #endif
  128. if (logFile_)
  129. {
  130. logFile_->WriteLine(formattedMessage);
  131. logFile_->Flush();
  132. }
  133. // Log messages can be safely sent as an event only in single-instance mode
  134. if (logInstances.Size() == 1)
  135. {
  136. inWrite_ = true;
  137. using namespace LogMessage;
  138. VariantMap eventData;
  139. eventData[P_MESSAGE] = formattedMessage;
  140. logInstances[0]->SendEvent(E_LOGMESSAGE, eventData);
  141. inWrite_ = false;
  142. }
  143. }
  144. }
  145. void Log::WriteRaw(const String& message, bool error)
  146. {
  147. // Prevent recursion during log event
  148. if (inWrite_)
  149. return;
  150. {
  151. MutexLock lock(GetStaticMutex());
  152. lastMessage_ = message;
  153. #if defined(ANDROID)
  154. __android_log_print(ANDROID_LOG_INFO, "Urho3D", message.CString());
  155. #elif defined(IOS)
  156. SDL_IOS_LogMessage(message.CString());
  157. #else
  158. if (quiet_)
  159. {
  160. // If in quiet mode, still print the error message to the standard error stream
  161. if (error)
  162. PrintUnicode(message, true);
  163. }
  164. else
  165. PrintUnicode(message, error);
  166. #endif
  167. if (logFile_)
  168. {
  169. logFile_->Write(message.CString(), message.Length());
  170. logFile_->Flush();
  171. }
  172. // Log messages can be safely sent as an event only in single-instance mode
  173. if (logInstances.Size() == 1)
  174. {
  175. inWrite_ = true;
  176. using namespace LogMessage;
  177. VariantMap eventData;
  178. eventData[P_MESSAGE] = message;
  179. logInstances[0]->SendEvent(E_LOGMESSAGE, eventData);
  180. inWrite_ = false;
  181. }
  182. }
  183. }
  184. }