Log.cpp 5.0 KB

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