Log.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 "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. const char* logLevelPrefixes[] =
  41. {
  42. "DEBUG",
  43. "INFO",
  44. "WARNING",
  45. "ERROR",
  46. 0
  47. };
  48. static Log* logInstance = 0;
  49. Log::Log(Context* context) :
  50. Object(context),
  51. #ifdef _DEBUG
  52. level_(LOG_DEBUG),
  53. #else
  54. level_(LOG_INFO),
  55. #endif
  56. timeStamp_(true),
  57. inWrite_(false),
  58. quiet_(false)
  59. {
  60. logInstance = this;
  61. }
  62. Log::~Log()
  63. {
  64. logInstance = 0;
  65. }
  66. void Log::Open(const String& fileName)
  67. {
  68. #if !defined(ANDROID) && !defined(IOS)
  69. if (fileName.Empty())
  70. return;
  71. if (logFile_ && logFile_->IsOpen())
  72. {
  73. if (logFile_->GetName() == fileName)
  74. return;
  75. else
  76. Close();
  77. }
  78. logFile_ = new File(context_);
  79. if (logFile_->Open(fileName, FILE_WRITE))
  80. Write(LOG_INFO, "Opened log file " + fileName);
  81. else
  82. {
  83. logFile_.Reset();
  84. Write(LOG_ERROR, "Failed to create log file " + fileName);
  85. }
  86. #endif
  87. }
  88. void Log::Close()
  89. {
  90. #if !defined(ANDROID) && !defined(IOS)
  91. if (logFile_ && logFile_->IsOpen())
  92. {
  93. logFile_->Close();
  94. logFile_.Reset();
  95. }
  96. #endif
  97. }
  98. void Log::SetLevel(int level)
  99. {
  100. assert(level >= LOG_DEBUG && level < LOG_NONE);
  101. level_ = level;
  102. }
  103. void Log::SetTimeStamp(bool enable)
  104. {
  105. timeStamp_ = enable;
  106. }
  107. void Log::SetQuiet(bool quiet)
  108. {
  109. quiet_ = quiet;
  110. }
  111. void Log::Write(int level, const String& message)
  112. {
  113. assert(level >= LOG_DEBUG && level < LOG_NONE);
  114. // Do not log if message level excluded or if currently sending a log event
  115. if (!logInstance || logInstance->level_ > level || logInstance->inWrite_)
  116. return;
  117. String formattedMessage = logLevelPrefixes[level];
  118. formattedMessage += ": " + message;
  119. logInstance->lastMessage_ = message;
  120. if (logInstance->timeStamp_)
  121. formattedMessage = "[" + Time::GetTimeStamp() + "] " + formattedMessage;
  122. #if defined(ANDROID)
  123. int androidLevel = ANDROID_LOG_DEBUG + level;
  124. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  125. #elif defined(IOS)
  126. SDL_IOS_LogMessage(message.CString());
  127. #else
  128. if (logInstance->quiet_)
  129. {
  130. // If in quiet mode, still print the error message to the standard error stream
  131. if (level == LOG_ERROR)
  132. PrintUnicodeLine(formattedMessage, true);
  133. }
  134. else
  135. PrintUnicodeLine(formattedMessage, level == LOG_ERROR);
  136. #endif
  137. if (logInstance->logFile_)
  138. {
  139. logInstance->logFile_->WriteLine(formattedMessage);
  140. logInstance->logFile_->Flush();
  141. }
  142. logInstance->inWrite_ = true;
  143. using namespace LogMessage;
  144. VariantMap& eventData = logInstance->GetEventDataMap();
  145. eventData[P_MESSAGE] = formattedMessage;
  146. eventData[P_LEVEL] = level;
  147. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  148. logInstance->inWrite_ = false;
  149. }
  150. void Log::WriteRaw(const String& message, bool error)
  151. {
  152. // Prevent recursion during log event
  153. if (!logInstance || logInstance->inWrite_)
  154. return;
  155. logInstance->lastMessage_ = message;
  156. #if defined(ANDROID)
  157. if (logInstance->quiet_)
  158. {
  159. if (error)
  160. __android_log_print(ANDROID_LOG_ERROR, "Urho3D", message.CString());
  161. }
  162. else
  163. __android_log_print(error ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "Urho3D", message.CString());
  164. #elif defined(IOS)
  165. SDL_IOS_LogMessage(message.CString());
  166. #else
  167. if (logInstance->quiet_)
  168. {
  169. // If in quiet mode, still print the error message to the standard error stream
  170. if (error)
  171. PrintUnicode(message, true);
  172. }
  173. else
  174. PrintUnicode(message, error);
  175. #endif
  176. if (logInstance->logFile_)
  177. {
  178. logInstance->logFile_->Write(message.CString(), message.Length());
  179. logInstance->logFile_->Flush();
  180. }
  181. logInstance->inWrite_ = true;
  182. using namespace LogMessage;
  183. VariantMap& eventData = logInstance->GetEventDataMap();
  184. eventData[P_MESSAGE] = message;
  185. eventData[P_LEVEL] = error ? LOG_ERROR : LOG_INFO;
  186. logInstance->SendEvent(E_LOGMESSAGE, eventData);
  187. logInstance->inWrite_ = false;
  188. }
  189. }