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

124 lines
3.4 KiB

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