Log.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Context.h"
  25. #include "File.h"
  26. #include "IOEvents.h"
  27. #include "Log.h"
  28. #include "ProcessUtils.h"
  29. #include <cstdio>
  30. #include <ctime>
  31. #include "DebugNew.h"
  32. static const String levelPrefixes[] =
  33. {
  34. "DEBUG",
  35. "INFO",
  36. "WARNING",
  37. "ERROR"
  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. inWrite_(false)
  48. {
  49. }
  50. Log::~Log()
  51. {
  52. }
  53. void Log::Open(const String& fileName)
  54. {
  55. if (fileName.Empty())
  56. return;
  57. logFile_ = new File(context_);
  58. if (logFile_->Open(fileName, FILE_WRITE))
  59. Write(LOG_INFO, "Opened log file " + fileName);
  60. else
  61. {
  62. logFile_.Reset();
  63. Write(LOG_ERROR, "Failed to create log file " + fileName);
  64. }
  65. }
  66. void Log::Write(int level, const String& message)
  67. {
  68. // Prevent recursion
  69. if (inWrite_)
  70. return;
  71. // Check message level
  72. if (level_ > level || level < LOG_DEBUG || level >= LOG_NONE)
  73. return;
  74. inWrite_ = true;
  75. lastMessage_ = message;
  76. time_t sysTime;
  77. time(&sysTime);
  78. const char* dateTime = ctime(&sysTime);
  79. String dateTimeString = String(dateTime).Replaced("\n", "");
  80. String formattedMessage = "[" + dateTimeString + "] " + levelPrefixes[level] + ": " + message;
  81. PrintUnicodeLine(formattedMessage);
  82. if (logFile_)
  83. logFile_->WriteLine(formattedMessage);
  84. using namespace LogMessage;
  85. VariantMap eventData;
  86. eventData[P_MESSAGE] = formattedMessage;
  87. SendEvent(E_LOGMESSAGE, eventData);
  88. inWrite_ = false;
  89. }
  90. void Log::WriteRaw(const String& message)
  91. {
  92. // Prevent recursion
  93. if (inWrite_)
  94. return;
  95. inWrite_ = true;
  96. lastMessage_ = message;
  97. PrintUnicode(message);
  98. if (logFile_)
  99. {
  100. logFile_->Write(message.CString(), message.Length());
  101. logFile_->Flush();
  102. }
  103. using namespace LogMessage;
  104. VariantMap eventData;
  105. eventData[P_MESSAGE] = message;
  106. SendEvent(E_LOGMESSAGE, eventData);
  107. inWrite_ = false;
  108. }
  109. void Log::SetLevel(int level)
  110. {
  111. level_ = level;
  112. }
  113. void WriteToLog(Context* context, int level, const String& message)
  114. {
  115. Log* log = context->GetSubsystem<Log>();
  116. if (log)
  117. log->Write(level, message);
  118. }
  119. void WriteToLogRaw(Context* context, const String& message)
  120. {
  121. Log* log = context->GetSubsystem<Log>();
  122. if (log)
  123. log->WriteRaw(message);
  124. }