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

163 lines
3.2 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. #include "logger.hpp"
  20. #include <iostream>
  21. logger::logger():
  22. os(&std::cout),
  23. log_prefix(std::string()),
  24. log_postfix(std::string()),
  25. warning_prefix(std::string("Warning: ")),
  26. warning_postfix(std::string()),
  27. error_prefix(std::string("Error: ")),
  28. error_postfix(std::string()),
  29. success_prefix(std::string("Success: ")),
  30. success_postfix(std::string()),
  31. next_task_id(0)
  32. {}
  33. logger::~logger()
  34. {}
  35. void logger::redirect(std::ostream* stream)
  36. {
  37. os = stream;
  38. }
  39. void logger::log(const std::string& text)
  40. {
  41. if (os)
  42. {
  43. for (const std::string& prefix: prefix_stack)
  44. {
  45. (*os) << prefix;
  46. }
  47. (*os) << (log_prefix + text + log_postfix);
  48. os->flush();
  49. }
  50. }
  51. void logger::warning(const std::string& text)
  52. {
  53. log(warning_prefix + text + warning_postfix);
  54. }
  55. void logger::error(const std::string& text)
  56. {
  57. log(error_prefix + text + error_postfix);
  58. }
  59. void logger::success(const std::string& text)
  60. {
  61. log(success_prefix + text + success_postfix);
  62. }
  63. void logger::set_log_prefix(const std::string& prefix)
  64. {
  65. log_prefix = prefix;
  66. }
  67. void logger::set_log_postfix(const std::string& postfix)
  68. {
  69. log_postfix = postfix;
  70. }
  71. void logger::set_warning_prefix(const std::string& prefix)
  72. {
  73. warning_prefix = prefix;
  74. }
  75. void logger::set_warning_postfix(const std::string& postfix)
  76. {
  77. warning_postfix = postfix;
  78. }
  79. void logger::set_error_prefix(const std::string& prefix)
  80. {
  81. error_prefix = prefix;
  82. }
  83. void logger::set_error_postfix(const std::string& postfix)
  84. {
  85. error_postfix = postfix;
  86. }
  87. void logger::set_success_prefix(const std::string& prefix)
  88. {
  89. success_prefix = prefix;
  90. }
  91. void logger::set_success_postfix(const std::string& postfix)
  92. {
  93. success_postfix = postfix;
  94. }
  95. void logger::push_prefix(const std::string& prefix)
  96. {
  97. prefix_stack.push_back(prefix);
  98. }
  99. void logger::pop_prefix()
  100. {
  101. prefix_stack.pop_back();
  102. }
  103. int logger::open_task(const std::string& text)
  104. {
  105. tasks[next_task_id] = text;
  106. std::string indent;
  107. for (std::size_t i = 0; i < tasks.size() - 1; ++i)
  108. indent += " ";
  109. log(indent + text + "...\n");
  110. return next_task_id++;
  111. }
  112. bool logger::close_task(int id, int status)
  113. {
  114. auto it = tasks.find(id);
  115. if (it == tasks.end())
  116. return false;
  117. std::string message = it->second + "... ";
  118. std::string indent;
  119. for (std::size_t i = 0; i < tasks.size() - 1; ++i)
  120. indent += " ";
  121. if (status == EXIT_SUCCESS)
  122. {
  123. message += "success\n";
  124. log(indent + message);
  125. }
  126. else
  127. {
  128. message += "failed (" + std::to_string(status) + ")\n";
  129. log(indent + message);
  130. }
  131. tasks.erase(it);
  132. }