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

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