Log.h 3.3 KB

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