Log.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // Copyright (c) 2008-2014 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 "CoreEvents.h"
  25. #include "File.h"
  26. #include "IOEvents.h"
  27. #include "Log.h"
  28. #include "Mutex.h"
  29. #include "ProcessUtils.h"
  30. #include "Thread.h"
  31. #include "Timer.h"
  32. #include <cstdio>
  33. #ifdef ANDROID
  34. #include <android/log.h>
  35. #endif
  36. #ifdef IOS
  37. extern "C" void SDL_IOS_LogMessage(const char* message);
  38. #endif
  39. #include "DebugNew.h"
  40. namespace Urho3D
  41. {
  42. const char* logLevelPrefixes[] =
  43. {
  44. "DEBUG",
  45. "INFO",
  46. "WARNING",
  47. "ERROR",
  48. 0
  49. };
  50. static Log* logInstance = 0;
  51. static bool threadErrorDisplayed = false;
  52. Log::Log(Context* context) :
  53. Object(context),
  54. #ifdef _DEBUG
  55. level_(LOG_DEBUG),
  56. #else
  57. level_(LOG_INFO),
  58. #endif
  59. timeStamp_(true),
  60. inWrite_(false),
  61. quiet_(false)
  62. {
  63. logInstance = this;
  64. SubscribeToEvent(E_ENDFRAME, HANDLER(Log, HandleEndFrame));
  65. }
  66. Log::~Log()
  67. {
  68. logInstance = 0;
  69. }
  70. void Log::Open(const String& fileName)
  71. {
  72. #if !defined(ANDROID) && !defined(IOS)
  73. if (fileName.Empty())
  74. return;
  75. if (logFile_ && logFile_->IsOpen())
  76. {
  77. if (logFile_->GetName() == fileName)
  78. return;
  79. else
  80. Close();
  81. }
  82. logFile_ = new File(context_);
  83. if (logFile_->Open(fileName, FILE_WRITE))
  84. Write(LOG_INFO, "Opened log file " + fileName);
  85. else
  86. {
  87. logFile_.Reset();
  88. Write(LOG_ERROR, "Failed to create log file " + fileName);
  89. }
  90. #endif
  91. }
  92. void Log::Close()
  93. {
  94. #if !defined(ANDROID) && !defined(IOS)
  95. if (logFile_ && logFile_->IsOpen())
  96. {
  97. logFile_->Close();
  98. logFile_.Reset();
  99. }
  100. #endif
  101. }
  102. void Log::SetLevel(int level)
  103. {
  104. assert(level >= LOG_DEBUG && level < LOG_NONE);
  105. level_ = level;
  106. }
  107. void Log::SetTimeStamp(bool enable)
  108. {
  109. timeStamp_ = enable;
  110. }
  111. void Log::SetQuiet(bool quiet)
  112. {
  113. quiet_ = quiet;
  114. }
  115. void Log::Write(int level, const String& message)
  116. {
  117. assert(level >= LOG_DEBUG && level < LOG_NONE);
  118. // If not in the main thread, store message for later processing
  119. if (!Thread::IsMainThread())
  120. {
  121. if (logInstance)
  122. {
  123. MutexLock lock(logInstance->logMutex_);
  124. logInstance->threadMessages_.Push(StoredLogMessage(message, level, false));
  125. }
  126. return;
  127. }
  128. // Do not log if message level excluded or if currently sending a log event
  129. if (!logInstance || logInstance->level_ > level || logInstance->inWrite_)
  130. return;
  131. String formattedMessage = logLevelPrefixes[level];
  132. formattedMessage += ": " + message;
  133. logInstance->lastMessage_ = message;
  134. if (logInstance->timeStamp_)
  135. formattedMessage = "[" + Time::GetTimeStamp() + "] " + formattedMessage;
  136. #if defined(ANDROID)
  137. int androidLevel = ANDROID_LOG_DEBUG + level;
  138. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  139. #elif defined(IOS)
  140. SDL_IOS_LogMessage(message.CString());
  141. #else
  142. if (logInstance->quiet_)
  143. {
  144. // If in quiet mode, still print the error message to the standard error stream
  145. if (level == LOG_ERROR)
  146. PrintUnicodeLine(formattedMessage, true);
  147. }
  148. else
  149. PrintUnicodeLine(formattedMessage, level == LOG_ERROR);
  150. #endif
  151. if (logInstance->logFile_)
  152. {
  153. logInstance->logFile_->WriteLine(formattedMessage);
  154. logInstance->logFile_->Flush();
  155. }
  156. logInstance->inWrite_ = true;
  157. using namespace LogMessage;
  158. VariantMap& eventData = logInstance->GetEventDataMap();
  159. eventData[P_MESSAGE] = formattedMessage;
  160. eventData[P_LEVEL] = level;
  161. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  162. logInstance->inWrite_ = false;
  163. }
  164. void Log::WriteRaw(const String& message, bool error)
  165. {
  166. // If not in the main thread, store message for later processing
  167. if (!Thread::IsMainThread())
  168. {
  169. if (logInstance)
  170. {
  171. MutexLock lock(logInstance->logMutex_);
  172. logInstance->threadMessages_.Push(StoredLogMessage(message, LOG_RAW, error));
  173. }
  174. return;
  175. }
  176. // Prevent recursion during log event
  177. if (!logInstance || logInstance->inWrite_)
  178. return;
  179. logInstance->lastMessage_ = message;
  180. #if defined(ANDROID)
  181. if (logInstance->quiet_)
  182. {
  183. if (error)
  184. __android_log_print(ANDROID_LOG_ERROR, "Urho3D", message.CString());
  185. }
  186. else
  187. __android_log_print(error ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "Urho3D", message.CString());
  188. #elif defined(IOS)
  189. SDL_IOS_LogMessage(message.CString());
  190. #else
  191. if (logInstance->quiet_)
  192. {
  193. // If in quiet mode, still print the error message to the standard error stream
  194. if (error)
  195. PrintUnicode(message, true);
  196. }
  197. else
  198. PrintUnicode(message, error);
  199. #endif
  200. if (logInstance->logFile_)
  201. {
  202. logInstance->logFile_->Write(message.CString(), message.Length());
  203. logInstance->logFile_->Flush();
  204. }
  205. logInstance->inWrite_ = true;
  206. using namespace LogMessage;
  207. VariantMap& eventData = logInstance->GetEventDataMap();
  208. eventData[P_MESSAGE] = message;
  209. eventData[P_LEVEL] = error ? LOG_ERROR : LOG_INFO;
  210. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  211. logInstance->inWrite_ = false;
  212. }
  213. void Log::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  214. {
  215. // If the MainThreadID is not valid, processing this loop can potentially be endless
  216. if (!Thread::IsMainThread())
  217. {
  218. if (!threadErrorDisplayed)
  219. {
  220. fprintf(stderr, "Thread::mainThreadID is not setup correctly! Threaded log handling disabled\n");
  221. threadErrorDisplayed = true;
  222. }
  223. return;
  224. }
  225. MutexLock lock(logMutex_);
  226. // Process messages accumulated from other threads (if any)
  227. while (!threadMessages_.Empty())
  228. {
  229. const StoredLogMessage& stored = threadMessages_.Front();
  230. if (stored.level_ != LOG_RAW)
  231. Write(stored.level_, stored.message_);
  232. else
  233. WriteRaw(stored.message_, stored.error_);
  234. threadMessages_.PopFront();
  235. }
  236. }
  237. }