💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.5 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_DEBUG_LOGGER_HPP
  20. #define ANTKEEPER_DEBUG_LOGGER_HPP
  21. #include <list>
  22. #include <ostream>
  23. #include <stack>
  24. #include <string>
  25. #include <mutex>
  26. namespace debug {
  27. /**
  28. * Logs formatted debug messages to an output stream.
  29. */
  30. class logger
  31. {
  32. public:
  33. /// Creates a logger.
  34. logger();
  35. /// Destroys a logger.
  36. ~logger();
  37. /**
  38. * Redirects log output to the specified output stream.
  39. *
  40. * @param stream Output stream to which log text will be written.
  41. */
  42. void redirect(std::ostream* stream);
  43. /**
  44. * Outputs text to the log.
  45. */
  46. void log(const std::string& text);
  47. void warning(const std::string& text);
  48. void error(const std::string& text);
  49. void success(const std::string& text);
  50. /**
  51. * Enables or disables automatic appending of newlines to log messages.
  52. *
  53. * @param enabled `true` if auto newline should be enabled, `false` otherwise.
  54. */
  55. void set_auto_newline(bool enabled);
  56. /**
  57. * Enables or disables prefixing log messages with a timestamp.
  58. *
  59. * @param enabled `true` if timestamps should be enabled, `false` otherwise.
  60. */
  61. void set_timestamp(bool enabled);
  62. /**
  63. * Sets the indent string which prefixes subtasks. This string will be repeated according to the level of indentation.
  64. *
  65. * @param indent Indent string.
  66. */
  67. void set_indent(const std::string& indent);
  68. void set_log_prefix(const std::string& prefix);
  69. void set_log_postfix(const std::string& postfix);
  70. void set_warning_prefix(const std::string& prefix);
  71. void set_warning_postfix(const std::string& postfix);
  72. void set_error_prefix(const std::string& prefix);
  73. void set_error_postfix(const std::string& postfix);
  74. void set_success_prefix(const std::string& prefix);
  75. void set_success_postfix(const std::string& postfix);
  76. /**
  77. * Pushes a task onto the stack and outputs it to the log.
  78. *
  79. * @param description Task description.
  80. */
  81. void push_task(const std::string& description);
  82. /**
  83. * Pops a task off the stack and outputs its status to the log.
  84. *
  85. * @param status Exit status of the task. A value of `0` or `EXIT_SUCCESS` indicates the task exited successfully. A non-zero exit status indicates the task failed.
  86. */
  87. void pop_task(int status);
  88. const std::string& get_history() const;
  89. private:
  90. std::ostream* os;
  91. bool auto_newline;
  92. bool timestamp_enabled;
  93. std::string indent;
  94. std::string log_prefix;
  95. std::string log_postfix;
  96. std::string warning_prefix;
  97. std::string warning_postfix;
  98. std::string error_prefix;
  99. std::string error_postfix;
  100. std::string success_prefix;
  101. std::string success_postfix;
  102. std::stack<std::string> tasks;
  103. std::string history;
  104. std::mutex history_mutex;
  105. };
  106. inline const std::string& logger::get_history() const
  107. {
  108. return history;
  109. }
  110. } // namespace debug
  111. #endif // ANTKEEPER_DEBUG_LOGGER_HPP