Log.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "Exception.h"
  25. #include "Log.h"
  26. #include "ProcessUtils.h"
  27. #include "StringUtils.h"
  28. #include "DebugNew.h"
  29. #include <time.h>
  30. Log* Log::sInstance = 0;
  31. Log::Log(const std::string& fileName, LogLevel level) :
  32. mHandle(0),
  33. mLevel(level)
  34. {
  35. if (sInstance)
  36. EXCEPTION("Log already exists");
  37. // If empty log file name, use a default name in the executable directory. Writing to it may fail depending on access rights.
  38. std::string useFileName = fileName.empty() ? getExecutableDirectory() + "Urho3D.log" : fileName;
  39. mHandle = fopen(useFileName.c_str(), "w");
  40. if (mHandle)
  41. write(LOG_INFO, "Log file " + useFileName + " created");
  42. else
  43. write(LOG_ERROR, "Failed to create log file " + useFileName);
  44. sInstance = this;
  45. }
  46. Log::~Log()
  47. {
  48. if (mHandle)
  49. {
  50. write(LOG_INFO, "Log file closed");
  51. fclose(mHandle);
  52. mHandle = 0;
  53. }
  54. if (sInstance == this)
  55. sInstance = 0;
  56. }
  57. void Log::write(LogLevel level, const std::string& message)
  58. {
  59. static const std::string levelPrefixes[] =
  60. {
  61. "DEBUG",
  62. "INFO",
  63. "WARNING",
  64. "ERROR"
  65. };
  66. if ((mLevel > level) || (level == LOG_NONE))
  67. return;
  68. mLastMessage = message;
  69. static time_t sysTime;
  70. time(&sysTime);
  71. const char* dateTime = ctime(&sysTime);
  72. std::string dateTimeString = replace(std::string(dateTime), "\n", "");
  73. std::string formattedMessage = "[" + dateTimeString + "] " + levelPrefixes[level] + ": " + message;
  74. printf("%s\n", formattedMessage.c_str());
  75. if (mHandle)
  76. {
  77. fprintf(mHandle, "%s\n", formattedMessage.c_str());
  78. fflush(mHandle);
  79. }
  80. using namespace LogMessage;
  81. VariantMap eventData;
  82. eventData[P_MESSAGE] = formattedMessage;
  83. sendEvent(EVENT_LOGMESSAGE, eventData);
  84. }
  85. void Log::writeRaw(const std::string& message)
  86. {
  87. mLastMessage = message;
  88. printf("%s", message.c_str());
  89. if (mHandle)
  90. {
  91. fprintf(mHandle, "%s", message.c_str());
  92. fflush(mHandle);
  93. }
  94. using namespace LogMessage;
  95. VariantMap eventData;
  96. eventData[P_MESSAGE] = message;
  97. sendEvent(EVENT_LOGMESSAGE, eventData);
  98. }
  99. void writeToLog(LogLevel level, const std::string& message)
  100. {
  101. Log* log = getLog();
  102. if (log)
  103. log->write(level, message);
  104. }
  105. void writeToLogRaw(const std::string& message)
  106. {
  107. Log* log = getLog();
  108. if (log)
  109. log->writeRaw(message);
  110. }