Log.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "StringUtils.h"
  27. #include "DebugNew.h"
  28. #include <time.h>
  29. Log* Log::sInstance = 0;
  30. Log::Log(const std::string& fileName, LogLevel level) :
  31. mHandle(0),
  32. mLevel(level)
  33. {
  34. if (sInstance)
  35. EXCEPTION("Log already exists");
  36. if (!fileName.empty())
  37. {
  38. mHandle = fopen(fileName.c_str(), "w");
  39. if (mHandle)
  40. write(LOG_INFO, "Log file " + fileName + " created");
  41. else
  42. write(LOG_ERROR, "Failed to create log file " + fileName);
  43. }
  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. static time_t sysTime;
  69. time(&sysTime);
  70. const char* dateTime = ctime(&sysTime);
  71. std::string dateTimeString = replace(std::string(dateTime), "\n", "");
  72. printf("[%s] %s: %s\n", dateTimeString.c_str(), levelPrefixes[level].c_str(), message.c_str());
  73. if (mHandle)
  74. {
  75. fprintf(mHandle, "[%s] %s: %s\n", dateTimeString.c_str(), levelPrefixes[level].c_str(), message.c_str());
  76. fflush(mHandle);
  77. }
  78. }
  79. void Log::writeRaw(const std::string& message)
  80. {
  81. printf("%s", message.c_str());
  82. if (mHandle)
  83. {
  84. fprintf(mHandle, "%s", message.c_str());
  85. fflush(mHandle);
  86. }
  87. }
  88. void writeToLog(LogLevel level, const std::string& message)
  89. {
  90. Log* log = getLog();
  91. if (log)
  92. log->write(level, message);
  93. }
  94. void writeToLogRaw(const std::string& message)
  95. {
  96. Log* log = getLog();
  97. if (log)
  98. log->writeRaw(message);
  99. }