Log.cpp 4.5 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 "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. static const String levelPrefixes[] =
  40. {
  41. "DEBUG",
  42. "INFO",
  43. "WARNING",
  44. "ERROR"
  45. };
  46. OBJECTTYPESTATIC(Log);
  47. Log::Log(Context* context) :
  48. Object(context),
  49. #ifdef _DEBUG
  50. level_(LOG_DEBUG),
  51. #else
  52. level_(LOG_INFO),
  53. #endif
  54. timeStamp_(true),
  55. inWrite_(false)
  56. {
  57. }
  58. Log::~Log()
  59. {
  60. }
  61. void Log::Open(const String& fileName)
  62. {
  63. #if !defined(ANDROID) && !defined(IOS)
  64. if (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::Write(int level, const String& message)
  77. {
  78. assert(level >= LOG_DEBUG && level < LOG_NONE);
  79. // Prevent recursion
  80. if (inWrite_)
  81. return;
  82. // Check message level
  83. if (level_ > level)
  84. return;
  85. inWrite_ = true;
  86. lastMessage_ = message;
  87. String formattedMessage = levelPrefixes[level] + ": " + message;
  88. if (timeStamp_)
  89. {
  90. Time* time = GetSubsystem<Time>();
  91. if (time)
  92. formattedMessage = "[" + time->GetTimeStamp() + "] " + formattedMessage;
  93. }
  94. #if defined(ANDROID)
  95. int androidLevel = ANDROID_LOG_DEBUG + level;
  96. __android_log_print(androidLevel, "Urho3D", "%s", message.CString());
  97. #elif defined(IOS)
  98. SDL_IOS_LogMessage(message.CString());
  99. #else
  100. PrintUnicodeLine(formattedMessage);
  101. #endif
  102. if (logFile_)
  103. {
  104. logFile_->WriteLine(formattedMessage);
  105. logFile_->Flush();
  106. }
  107. using namespace LogMessage;
  108. VariantMap eventData;
  109. eventData[P_MESSAGE] = formattedMessage;
  110. SendEvent(E_LOGMESSAGE, eventData);
  111. inWrite_ = false;
  112. }
  113. void Log::WriteRaw(const String& message)
  114. {
  115. // Prevent recursion
  116. if (inWrite_)
  117. return;
  118. inWrite_ = true;
  119. lastMessage_ = message;
  120. #if defined(ANDROID)
  121. __android_log_print(ANDROID_LOG_INFO, "Urho3D", message.CString());
  122. #elif defined(IOS)
  123. SDL_IOS_LogMessage(message.CString());
  124. #else
  125. PrintUnicode(message);
  126. #endif
  127. if (logFile_)
  128. {
  129. logFile_->Write(message.CString(), message.Length());
  130. logFile_->Flush();
  131. }
  132. using namespace LogMessage;
  133. VariantMap eventData;
  134. eventData[P_MESSAGE] = message;
  135. SendEvent(E_LOGMESSAGE, eventData);
  136. inWrite_ = false;
  137. }
  138. void Log::SetLevel(int level)
  139. {
  140. assert(level >= LOG_DEBUG && level < LOG_NONE);
  141. level_ = level;
  142. }
  143. void Log::SetTimeStamp(bool enable)
  144. {
  145. timeStamp_ = enable;
  146. }
  147. void WriteToLog(Context* context, int level, const String& message)
  148. {
  149. Log* log = context->GetSubsystem<Log>();
  150. if (log)
  151. log->Write(level, message);
  152. }
  153. void WriteToLogRaw(Context* context, const String& message)
  154. {
  155. Log* log = context->GetSubsystem<Log>();
  156. if (log)
  157. log->WriteRaw(message);
  158. }
  159. }