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

175 lines
3.3 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. for (const std::string& prefix: prefix_stack)
  52. {
  53. (*os) << prefix;
  54. }
  55. (*os) << (log_prefix + text + log_postfix);
  56. os->flush();
  57. }
  58. }
  59. void logger::warning(const std::string& text)
  60. {
  61. log(warning_prefix + text + warning_postfix);
  62. }
  63. void logger::error(const std::string& text)
  64. {
  65. log(error_prefix + text + error_postfix);
  66. }
  67. void logger::success(const std::string& text)
  68. {
  69. log(success_prefix + text + success_postfix);
  70. }
  71. void logger::set_log_prefix(const std::string& prefix)
  72. {
  73. log_prefix = prefix;
  74. }
  75. void logger::set_log_postfix(const std::string& postfix)
  76. {
  77. log_postfix = postfix;
  78. }
  79. void logger::set_warning_prefix(const std::string& prefix)
  80. {
  81. warning_prefix = prefix;
  82. }
  83. void logger::set_warning_postfix(const std::string& postfix)
  84. {
  85. warning_postfix = postfix;
  86. }
  87. void logger::set_error_prefix(const std::string& prefix)
  88. {
  89. error_prefix = prefix;
  90. }
  91. void logger::set_error_postfix(const std::string& postfix)
  92. {
  93. error_postfix = postfix;
  94. }
  95. void logger::set_success_prefix(const std::string& prefix)
  96. {
  97. success_prefix = prefix;
  98. }
  99. void logger::set_success_postfix(const std::string& postfix)
  100. {
  101. success_postfix = postfix;
  102. }
  103. void logger::push_prefix(const std::string& prefix)
  104. {
  105. prefix_stack.push_back(prefix);
  106. }
  107. void logger::pop_prefix()
  108. {
  109. prefix_stack.pop_back();
  110. }
  111. void logger::push_task(const std::string& description)
  112. {
  113. std::string message = description + "...\n";
  114. log(message);
  115. tasks.push(description);
  116. }
  117. void logger::pop_task(int status)
  118. {
  119. if (tasks.empty())
  120. {
  121. return;
  122. }
  123. std::string message = tasks.top() + "... ";
  124. tasks.pop();
  125. if (status == EXIT_SUCCESS)
  126. {
  127. message += "success\n";
  128. log(message);
  129. }
  130. else
  131. {
  132. message += "failed (" + std::to_string(status) + ")\n";
  133. error(message);
  134. }
  135. }
  136. void logger::set_indent(const std::string& indent)
  137. {
  138. this->indent = indent;
  139. }
  140. void logger::set_timestamp(bool enabled)
  141. {
  142. timestamp_enabled = enabled;
  143. }