💿🐜 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.

128 lines
3.4 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. namespace debug {
  26. /**
  27. * Logs formatted debug messages to an output stream.
  28. */
  29. class logger
  30. {
  31. public:
  32. /// Creates a logger.
  33. logger();
  34. /// Destroys a logger.
  35. ~logger();
  36. /**
  37. * Redirects log output to the specified output stream.
  38. *
  39. * @param stream Output stream to which log text will be written.
  40. */
  41. void redirect(std::ostream* stream);
  42. /**
  43. * Outputs text to the log.
  44. */
  45. void log(const std::string& text);
  46. void warning(const std::string& text);
  47. void error(const std::string& text);
  48. void success(const std::string& text);
  49. /**
  50. * Enables or disables automatic appending of newlines to log messages.
  51. *
  52. * @param enabled `true` if auto newline should be enabled, `false` otherwise.
  53. */
  54. void set_auto_newline(bool enabled);
  55. /**
  56. * Enables or disables prefixing log messages with a timestamp.
  57. *
  58. * @param enabled `true` if timestamps should be enabled, `false` otherwise.
  59. */
  60. void set_timestamp(bool enabled);
  61. /**
  62. * Sets the indent string which prefixes subtasks. This string will be repeated according to the level of indentation.
  63. *
  64. * @param indent Indent string.
  65. */
  66. void set_indent(const std::string& indent);
  67. void set_log_prefix(const std::string& prefix);
  68. void set_log_postfix(const std::string& postfix);
  69. void set_warning_prefix(const std::string& prefix);
  70. void set_warning_postfix(const std::string& postfix);
  71. void set_error_prefix(const std::string& prefix);
  72. void set_error_postfix(const std::string& postfix);
  73. void set_success_prefix(const std::string& prefix);
  74. void set_success_postfix(const std::string& postfix);
  75. /**
  76. * Pushes a task onto the stack and outputs it to the log.
  77. *
  78. * @param description Task description.
  79. */
  80. void push_task(const std::string& description);
  81. /**
  82. * Pops a task off the stack and outputs its status to the log.
  83. *
  84. * @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.
  85. */
  86. void pop_task(int status);
  87. const std::string& get_history() const;
  88. private:
  89. std::ostream* os;
  90. bool auto_newline;
  91. bool timestamp_enabled;
  92. std::string indent;
  93. std::string log_prefix;
  94. std::string log_postfix;
  95. std::string warning_prefix;
  96. std::string warning_postfix;
  97. std::string error_prefix;
  98. std::string error_postfix;
  99. std::string success_prefix;
  100. std::string success_postfix;
  101. std::stack<std::string> tasks;
  102. std::string history;
  103. };
  104. inline const std::string& logger::get_history() const
  105. {
  106. return history;
  107. }
  108. } // namespace debug
  109. #endif // ANTKEEPER_DEBUG_LOGGER_HPP