Log.cpp 7.6 KB

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