Log.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #pragma once
  23. #include "Object.h"
  24. namespace Urho3D
  25. {
  26. /// Debug message level. By default only shown in debug mode.
  27. static const int LOG_DEBUG = 0;
  28. /// Informative message level.
  29. static const int LOG_INFO = 1;
  30. /// Warning message level.
  31. static const int LOG_WARNING = 2;
  32. /// Error message level.
  33. static const int LOG_ERROR = 3;
  34. /// Disable all log messages.
  35. static const int LOG_NONE = 4;
  36. class File;
  37. /// Logging subsystem.
  38. class Log : public Object
  39. {
  40. OBJECT(Log);
  41. public:
  42. /// Construct.
  43. Log(Context* context);
  44. /// Destruct. Close the log file if open.
  45. virtual ~Log();
  46. /// Open the log file.
  47. void Open(const String& fileName);
  48. /// Write to the log. If logging level is higher than the level of the message, the message is ignored.
  49. void Write(int level, const String& message);
  50. /// Write raw output to the log.
  51. void WriteRaw(const String& message);
  52. /// Set logging level.
  53. void SetLevel(int level);
  54. /// Set whether to timestamp log messages.
  55. void SetTimeStamp(bool enable);
  56. /// Return logging level.
  57. int GetLevel() const { return level_; }
  58. /// Return whether log messages are timestamped.
  59. bool GetTimeStamp() const { return timeStamp_; }
  60. /// Return last log message.
  61. const String& GetLastMessage() const { return lastMessage_; }
  62. private:
  63. /// Log file.
  64. SharedPtr<File> logFile_;
  65. /// Last log message.
  66. String lastMessage_;
  67. /// Logging level.
  68. int level_;
  69. /// Timestamp log messages flag.
  70. bool timeStamp_;
  71. /// In write flag to prevent recursion.
  72. bool inWrite_;
  73. };
  74. /// Write to the log (static.)
  75. void WriteToLog(Context* context, int level, const String& message);
  76. /// Write raw output to the log (static.)
  77. void WriteToLogRaw(Context* context, const String& message);
  78. #ifdef ENABLE_LOGGING
  79. #define LOGDEBUG(message) WriteToLog(context_, LOG_DEBUG, message)
  80. #define LOGINFO(message) WriteToLog(context_, LOG_INFO, message)
  81. #define LOGWARNING(message) WriteToLog(context_, LOG_WARNING, message)
  82. #define LOGERROR(message) WriteToLog(context_, LOG_ERROR, message)
  83. #define LOGRAW(message) WriteToLogRaw(context_, message)
  84. #else
  85. #define LOGDEBUG(message)
  86. #define LOGINFO(message)
  87. #define LOGWARNING(message)
  88. #define LOGERROR(message)
  89. #define LOGRAW(message)
  90. #endif
  91. }