Log.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 <cstdio>
  29. #include <ctime>
  30. #include "DebugNew.h"
  31. static const String levelPrefixes[] =
  32. {
  33. "DEBUG",
  34. "INFO",
  35. "WARNING",
  36. "ERROR"
  37. };
  38. OBJECTTYPESTATIC(Log);
  39. Log::Log(Context* context) :
  40. Object(context),
  41. #ifdef _DEBUG
  42. level_(LOG_DEBUG),
  43. #else
  44. level_(LOG_INFO),
  45. #endif
  46. inWrite_(false)
  47. {
  48. }
  49. Log::~Log()
  50. {
  51. }
  52. void Log::Open(const String& fileName)
  53. {
  54. if (fileName.Empty())
  55. return;
  56. logFile_ = new File(context_);
  57. if (logFile_->Open(fileName, FILE_WRITE))
  58. Write(LOG_INFO, "Opened log file " + fileName);
  59. else
  60. {
  61. logFile_.Reset();
  62. Write(LOG_ERROR, "Failed to create log file " + fileName);
  63. }
  64. }
  65. void Log::Write(int level, const String& message)
  66. {
  67. // Prevent recursion
  68. if (inWrite_)
  69. return;
  70. // Check message level
  71. if (level_ > level || level < LOG_DEBUG || level >= LOG_NONE)
  72. return;
  73. inWrite_ = true;
  74. lastMessage_ = message;
  75. time_t sysTime;
  76. time(&sysTime);
  77. const char* dateTime = ctime(&sysTime);
  78. String dateTimeString = String(dateTime).Replaced("\n", "");
  79. String formattedMessage = "[" + dateTimeString + "] " + levelPrefixes[level] + ": " + message;
  80. printf("%s\n", formattedMessage.CString());
  81. if (logFile_)
  82. logFile_->WriteLine(formattedMessage);
  83. using namespace LogMessage;
  84. VariantMap eventData;
  85. eventData[P_MESSAGE] = formattedMessage;
  86. SendEvent(E_LOGMESSAGE, eventData);
  87. inWrite_ = false;
  88. }
  89. void Log::WriteRaw(const String& message)
  90. {
  91. // Prevent recursion
  92. if (inWrite_)
  93. return;
  94. inWrite_ = true;
  95. lastMessage_ = message;
  96. printf("%s", message.CString());
  97. if (logFile_)
  98. {
  99. logFile_->Write(message.CString(), message.Length());
  100. logFile_->Flush();
  101. }
  102. using namespace LogMessage;
  103. VariantMap eventData;
  104. eventData[P_MESSAGE] = message;
  105. SendEvent(E_LOGMESSAGE, eventData);
  106. inWrite_ = false;
  107. }
  108. void Log::SetLevel(int level)
  109. {
  110. level_ = level;
  111. }
  112. void WriteToLog(Context* context, int level, const String& message)
  113. {
  114. Log* log = context->GetSubsystem<Log>();
  115. if (log)
  116. log->Write(level, message);
  117. }
  118. void WriteToLogRaw(Context* context, const String& message)
  119. {
  120. Log* log = context->GetSubsystem<Log>();
  121. if (log)
  122. log->WriteRaw(message);
  123. }