Log.cpp 7.3 KB

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