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

94 lines
2.7 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 <string>
  24. #include <unordered_map>
  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. void push_prefix(const std::string& prefix);
  52. void pop_prefix();
  53. /**
  54. * Opens a task and outputs it to the log.
  55. *
  56. * @return Task ID used to close the task later.
  57. */
  58. int open_task(const std::string& text);
  59. /**
  60. * Closes a task and outputs its status to the log.
  61. *
  62. * @param id ID of the task to close.
  63. * @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.
  64. * @return `true` if the task was closed, `false` if no task with the specified ID was found.
  65. */
  66. bool close_task(int id, int status);
  67. private:
  68. std::ostream* os;
  69. std::string log_prefix;
  70. std::string log_postfix;
  71. std::string warning_prefix;
  72. std::string warning_postfix;
  73. std::string error_prefix;
  74. std::string error_postfix;
  75. std::string success_prefix;
  76. std::string success_postfix;
  77. std::list<std::string> prefix_stack;
  78. int next_task_id;
  79. std::unordered_map<int, std::string> tasks;
  80. };
  81. #endif // ANTKEEPER_LOGGER_HPP