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

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