Log.cpp 6.2 KB

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