Log.cpp 4.8 KB

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