Log.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #pragma once
  24. #include "Object.h"
  25. /// Logging levels
  26. static const int LOG_DEBUG = 0;
  27. static const int LOG_INFO = 1;
  28. static const int LOG_WARNING = 2;
  29. static const int LOG_ERROR = 3;
  30. static const int LOG_NONE = 4;
  31. class File;
  32. /// Log file subsystem
  33. class Log : public Object
  34. {
  35. OBJECT(Log);
  36. public:
  37. /// Construct
  38. Log(Context* context);
  39. /// Destruct. Close the log file if open
  40. virtual ~Log();
  41. /// Open the log file
  42. void Open(const String& fileName);
  43. /// Write to the log. If logging level is higher than the level of the message, the message is ignored
  44. void Write(int level, const String& message);
  45. /// Write raw output to the log
  46. void WriteRaw(const String& message);
  47. /// Set logging level
  48. void SetLevel(int level);
  49. /// Return logging level
  50. int GetLevel() const { return level_; }
  51. /// Return last log message
  52. const String& GetLastMessage() const { return lastMessage_; }
  53. private:
  54. /// Log file
  55. SharedPtr<File> logFile_;
  56. /// Last log message
  57. String lastMessage_;
  58. /// Logging level
  59. int level_;
  60. /// In write flag to prevent recursion
  61. bool inWrite_;
  62. };
  63. /// Write to the log (static)
  64. void WriteToLog(Context* context, int level, const String& message);
  65. /// Write raw output to the log (static)
  66. void WriteToLogRaw(Context* context, const String& message);
  67. #define LOGDEBUG(message) WriteToLog(context_, LOG_DEBUG, message)
  68. #define LOGINFO(message) WriteToLog(context_, LOG_INFO, message)
  69. #define LOGWARNING(message) WriteToLog(context_, LOG_WARNING, message)
  70. #define LOGERROR(message) WriteToLog(context_, LOG_ERROR, message)
  71. #define LOGRAW(message) WriteToLogRaw(context_, message)