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

190 lines
3.6 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. std::string message = "";
  47. // Prepend timestamp
  48. if (timestamp_enabled)
  49. {
  50. message += timestamp();
  51. message += ": ";
  52. }
  53. // Prepend indentation
  54. for (std::size_t i = 0; i < tasks.size(); ++i)
  55. message += indent;
  56. // Append text
  57. message += (log_prefix + text + log_postfix);
  58. // Append newline
  59. if (auto_newline)
  60. message += "\n";
  61. // Add message to log history
  62. history += message;
  63. // Output message
  64. (*os) << message;
  65. // Flush output stream
  66. os->flush();
  67. }
  68. }
  69. void logger::warning(const std::string& text)
  70. {
  71. log(warning_prefix + text + warning_postfix);
  72. }
  73. void logger::error(const std::string& text)
  74. {
  75. log(error_prefix + text + error_postfix);
  76. }
  77. void logger::success(const std::string& text)
  78. {
  79. log(success_prefix + text + success_postfix);
  80. }
  81. void logger::set_auto_newline(bool enabled)
  82. {
  83. auto_newline = enabled;
  84. }
  85. void logger::set_timestamp(bool enabled)
  86. {
  87. timestamp_enabled = enabled;
  88. }
  89. void logger::set_indent(const std::string& indent)
  90. {
  91. this->indent = indent;
  92. }
  93. void logger::set_log_prefix(const std::string& prefix)
  94. {
  95. log_prefix = prefix;
  96. }
  97. void logger::set_log_postfix(const std::string& postfix)
  98. {
  99. log_postfix = postfix;
  100. }
  101. void logger::set_warning_prefix(const std::string& prefix)
  102. {
  103. warning_prefix = prefix;
  104. }
  105. void logger::set_warning_postfix(const std::string& postfix)
  106. {
  107. warning_postfix = postfix;
  108. }
  109. void logger::set_error_prefix(const std::string& prefix)
  110. {
  111. error_prefix = prefix;
  112. }
  113. void logger::set_error_postfix(const std::string& postfix)
  114. {
  115. error_postfix = postfix;
  116. }
  117. void logger::set_success_prefix(const std::string& prefix)
  118. {
  119. success_prefix = prefix;
  120. }
  121. void logger::set_success_postfix(const std::string& postfix)
  122. {
  123. success_postfix = postfix;
  124. }
  125. void logger::push_task(const std::string& description)
  126. {
  127. std::string message = description + "...";
  128. if (!auto_newline)
  129. message += "\n";
  130. log(message);
  131. tasks.push(description);
  132. }
  133. void logger::pop_task(int status)
  134. {
  135. if (tasks.empty())
  136. {
  137. return;
  138. }
  139. std::string message = tasks.top() + "... ";
  140. tasks.pop();
  141. if (status == EXIT_SUCCESS)
  142. {
  143. message += "success";
  144. if (!auto_newline)
  145. message += "\n";
  146. log(message);
  147. }
  148. else
  149. {
  150. message += "failed (" + std::to_string(status) + ")";
  151. if (!auto_newline)
  152. message += "\n";
  153. error(message);
  154. }
  155. }