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

194 lines
3.6 KiB

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