Logger.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2018 Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  3. * Author: Ortega Froysa, Nicolás <nortega@themusicinnoise.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Logger.hpp"
  19. #include <iostream>
  20. Logger::Logger(const std::string &path) :
  21. #ifdef DEBUG
  22. debug(true)
  23. #else
  24. debug(false)
  25. #endif
  26. {
  27. logFile.open(path);
  28. }
  29. Logger::~Logger() {
  30. logFile.close();
  31. }
  32. void Logger::write(const std::string &message) {
  33. std::cout << message << std::endl;
  34. logFile << message << "\n";
  35. log.push_back(message);
  36. }
  37. void Logger::writeDebug(const std::string &message) {
  38. if(debug)
  39. {
  40. const std::string fullMsg = "[DEBUG] " + message;
  41. std::cout << fullMsg << std::endl;
  42. logFile << fullMsg << "\n";
  43. log.push_back(fullMsg);
  44. }
  45. }
  46. void Logger::writeWarn(const std::string &message) {
  47. const std::string fullMsg = "[WARN] " + message;
  48. std::cout << fullMsg << std::endl;
  49. logFile << fullMsg << "\n";
  50. log.push_back(fullMsg);
  51. }
  52. void Logger::writeError(const std::string &message) {
  53. const std::string fullMsg = "[ERROR] " + message;
  54. std::cerr << fullMsg << std::endl;
  55. logFile << fullMsg << "\n";
  56. log.push_back(fullMsg);
  57. }