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

122 lines
2.5 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 <iostream>
  21. logger::logger():
  22. os(&std::cout),
  23. log_prefix(std::string()),
  24. log_postfix(std::string()),
  25. warning_prefix(std::string("Warning: ")),
  26. warning_postfix(std::string()),
  27. error_prefix(std::string("Error: ")),
  28. error_postfix(std::string()),
  29. success_prefix(std::string("Success: ")),
  30. success_postfix(std::string())
  31. {}
  32. logger::~logger()
  33. {}
  34. void logger::redirect(std::ostream* stream)
  35. {
  36. os = stream;
  37. }
  38. void logger::log(const std::string& text)
  39. {
  40. if (os)
  41. {
  42. for (const std::string& prefix: prefix_stack)
  43. {
  44. (*os) << prefix;
  45. }
  46. (*os) << (log_prefix + text + log_postfix);
  47. os->flush();
  48. }
  49. }
  50. void logger::warning(const std::string& text)
  51. {
  52. log(warning_prefix + text + warning_postfix);
  53. }
  54. void logger::error(const std::string& text)
  55. {
  56. log(error_prefix + text + error_postfix);
  57. }
  58. void logger::success(const std::string& text)
  59. {
  60. log(success_prefix + text + success_postfix);
  61. }
  62. void logger::set_log_prefix(const std::string& prefix)
  63. {
  64. log_prefix = prefix;
  65. }
  66. void logger::set_log_postfix(const std::string& postfix)
  67. {
  68. log_postfix = postfix;
  69. }
  70. void logger::set_warning_prefix(const std::string& prefix)
  71. {
  72. warning_prefix = prefix;
  73. }
  74. void logger::set_warning_postfix(const std::string& postfix)
  75. {
  76. warning_postfix = postfix;
  77. }
  78. void logger::set_error_prefix(const std::string& prefix)
  79. {
  80. error_prefix = prefix;
  81. }
  82. void logger::set_error_postfix(const std::string& postfix)
  83. {
  84. error_postfix = postfix;
  85. }
  86. void logger::set_success_prefix(const std::string& prefix)
  87. {
  88. success_prefix = prefix;
  89. }
  90. void logger::set_success_postfix(const std::string& postfix)
  91. {
  92. success_postfix = postfix;
  93. }
  94. void logger::push_prefix(const std::string& prefix)
  95. {
  96. prefix_stack.push_back(prefix);
  97. }
  98. void logger::pop_prefix()
  99. {
  100. prefix_stack.pop_back();
  101. }