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

107 lines
2.3 KiB

  1. /*
  2. * Copyright (C) 2017-2019 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 "ansi-escape-codes.hpp"
  21. #include <iostream>
  22. Logger::Logger():
  23. os(&std::cout),
  24. logPrefix(std::string()),
  25. logPostfix("\n"),
  26. warningPrefix(ANSI_CODE_YELLOW + std::string("Warning: ")),
  27. warningPostfix(ANSI_CODE_RESET),
  28. errorPrefix(ANSI_CODE_RED + std::string("Error: ")),
  29. errorPostfix(ANSI_CODE_RESET),
  30. successPrefix(ANSI_CODE_GREEN + std::string("Success: ")),
  31. successPostfix(ANSI_CODE_RESET)
  32. {}
  33. Logger::~Logger()
  34. {}
  35. void Logger::redirect(std::ostream* stream)
  36. {
  37. os = stream;
  38. }
  39. void Logger::log(const std::string& text)
  40. {
  41. if (os)
  42. {
  43. (*os) << (logPrefix + text + logPostfix);
  44. os->flush();
  45. }
  46. }
  47. void Logger::warning(const std::string& text)
  48. {
  49. log(warningPrefix + text + warningPostfix);
  50. }
  51. void Logger::error(const std::string& text)
  52. {
  53. log(errorPrefix + text + errorPostfix);
  54. }
  55. void Logger::success(const std::string& text)
  56. {
  57. log(successPrefix + text + successPostfix);
  58. }
  59. void Logger::setLogPrefix(const std::string& prefix)
  60. {
  61. logPrefix = prefix;
  62. }
  63. void Logger::setLogPostfix(const std::string& postfix)
  64. {
  65. logPostfix = postfix;
  66. }
  67. void Logger::setWarningPrefix(const std::string& prefix)
  68. {
  69. warningPrefix = prefix;
  70. }
  71. void Logger::setWarningPostfix(const std::string& postfix)
  72. {
  73. warningPostfix = postfix;
  74. }
  75. void Logger::setErrorPrefix(const std::string& prefix)
  76. {
  77. errorPrefix = prefix;
  78. }
  79. void Logger::setErrorPostfix(const std::string& postfix)
  80. {
  81. errorPostfix = postfix;
  82. }
  83. void Logger::setSuccessPrefix(const std::string& prefix)
  84. {
  85. successPrefix = prefix;
  86. }
  87. void Logger::setSuccessPostfix(const std::string& postfix)
  88. {
  89. successPostfix = postfix;
  90. }