Log.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. //
  2. // Copyright (c) 2008-2020 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. #if defined(IOS) || defined(TVOS)
  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. "TRACE",
  44. "DEBUG",
  45. "INFO",
  46. "WARNING",
  47. "ERROR",
  48. nullptr
  49. };
  50. static Log* logInstance = nullptr;
  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, URHO3D_HANDLER(Log, HandleEndFrame));
  65. }
  66. Log::~Log()
  67. {
  68. logInstance = nullptr;
  69. }
  70. void Log::Open(const String& fileName)
  71. {
  72. #if !defined(__ANDROID__) && !defined(IOS) && !defined(TVOS)
  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) && !defined(TVOS)
  95. if (logFile_ && logFile_->IsOpen())
  96. {
  97. logFile_->Close();
  98. logFile_.Reset();
  99. }
  100. #endif
  101. }
  102. void Log::SetLevel(int level)
  103. {
  104. if (level < LOG_TRACE || level > LOG_NONE)
  105. {
  106. URHO3D_LOGERRORF("Attempted to set erroneous log level %d", level);
  107. return;
  108. }
  109. level_ = level;
  110. }
  111. void Log::SetTimeStamp(bool enable)
  112. {
  113. timeStamp_ = enable;
  114. }
  115. void Log::SetQuiet(bool quiet)
  116. {
  117. quiet_ = quiet;
  118. }
  119. void Log::Write(int level, const String& message)
  120. {
  121. // Special case for LOG_RAW level
  122. if (level == LOG_RAW)
  123. {
  124. WriteRaw(message, false);
  125. return;
  126. }
  127. // No-op if illegal level
  128. if (level < LOG_TRACE || level >= LOG_NONE)
  129. return;
  130. // If not in the main thread, store message for later processing
  131. if (!Thread::IsMainThread())
  132. {
  133. if (logInstance)
  134. {
  135. MutexLock lock(logInstance->logMutex_);
  136. logInstance->threadMessages_.Push(StoredLogMessage(message, level, false));
  137. }
  138. return;
  139. }
  140. // Do not log if message level excluded or if currently sending a log event
  141. if (!logInstance || logInstance->level_ > level || logInstance->inWrite_)
  142. return;
  143. String formattedMessage = logLevelPrefixes[level];
  144. formattedMessage += ": " + message;
  145. logInstance->lastMessage_ = message;
  146. if (logInstance->timeStamp_)
  147. formattedMessage = "[" + Time::GetTimeStamp() + "] " + formattedMessage;
  148. #if defined(__ANDROID__)
  149. int androidLevel = ANDROID_LOG_VERBOSE + level;
  150. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  151. #elif defined(IOS) || defined(TVOS)
  152. SDL_IOS_LogMessage(message.CString());
  153. #else
  154. if (logInstance->quiet_)
  155. {
  156. // If in quiet mode, still print the error message to the standard error stream
  157. if (level == LOG_ERROR)
  158. PrintUnicodeLine(formattedMessage, true);
  159. }
  160. else
  161. PrintUnicodeLine(formattedMessage, level == LOG_ERROR);
  162. #endif
  163. if (logInstance->logFile_)
  164. {
  165. logInstance->logFile_->WriteLine(formattedMessage);
  166. logInstance->logFile_->Flush();
  167. }
  168. logInstance->inWrite_ = true;
  169. using namespace LogMessage;
  170. VariantMap& eventData = logInstance->GetEventDataMap();
  171. eventData[P_MESSAGE] = formattedMessage;
  172. eventData[P_LEVEL] = level;
  173. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  174. logInstance->inWrite_ = false;
  175. }
  176. void Log::WriteRaw(const String& message, bool error)
  177. {
  178. // If not in the main thread, store message for later processing
  179. if (!Thread::IsMainThread())
  180. {
  181. if (logInstance)
  182. {
  183. MutexLock lock(logInstance->logMutex_);
  184. logInstance->threadMessages_.Push(StoredLogMessage(message, LOG_RAW, error));
  185. }
  186. return;
  187. }
  188. // Prevent recursion during log event
  189. if (!logInstance || logInstance->inWrite_)
  190. return;
  191. logInstance->lastMessage_ = message;
  192. #if defined(__ANDROID__)
  193. if (logInstance->quiet_)
  194. {
  195. if (error)
  196. __android_log_print(ANDROID_LOG_ERROR, "Urho3D", "%s", message.CString());
  197. }
  198. else
  199. __android_log_print(error ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "Urho3D", "%s", message.CString());
  200. #elif defined(IOS) || defined(TVOS)
  201. SDL_IOS_LogMessage(message.CString());
  202. #else
  203. if (logInstance->quiet_)
  204. {
  205. // If in quiet mode, still print the error message to the standard error stream
  206. if (error)
  207. PrintUnicode(message, true);
  208. }
  209. else
  210. PrintUnicode(message, error);
  211. #endif
  212. if (logInstance->logFile_)
  213. {
  214. logInstance->logFile_->Write(message.CString(), message.Length());
  215. logInstance->logFile_->Flush();
  216. }
  217. logInstance->inWrite_ = true;
  218. using namespace LogMessage;
  219. VariantMap& eventData = logInstance->GetEventDataMap();
  220. eventData[P_MESSAGE] = message;
  221. eventData[P_LEVEL] = error ? LOG_ERROR : LOG_INFO;
  222. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  223. logInstance->inWrite_ = false;
  224. }
  225. void Log::HandleEndFrame(StringHash eventType, VariantMap& eventData)
  226. {
  227. // If the MainThreadID is not valid, processing this loop can potentially be endless
  228. if (!Thread::IsMainThread())
  229. {
  230. if (!threadErrorDisplayed)
  231. {
  232. fprintf(stderr, "Thread::mainThreadID is not setup correctly! Threaded log handling disabled\n");
  233. threadErrorDisplayed = true;
  234. }
  235. return;
  236. }
  237. MutexLock lock(logMutex_);
  238. // Process messages accumulated from other threads (if any)
  239. while (!threadMessages_.Empty())
  240. {
  241. const StoredLogMessage& stored = threadMessages_.Front();
  242. if (stored.level_ != LOG_RAW)
  243. Write(stored.level_, stored.message_);
  244. else
  245. WriteRaw(stored.message_, stored.error_);
  246. threadMessages_.PopFront();
  247. }
  248. }
  249. }