Log.cpp 5.0 KB

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