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

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