Clone of Bael'Zharon's Respite @ https://github.com/boardwalk/bzr

Log.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Bael'Zharon's Respite
  3. * Copyright (C) 2014 Daniel Skorupski
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "Log.h"
  19. #include "Config.h"
  20. #include "Core.h"
  21. #include <iostream>
  22. namespace {
  23. // left unopened intentionally
  24. ofstream g_nullStream;
  25. ostream& operator<<(ostream& os, LogSubsystem sys)
  26. {
  27. switch(sys)
  28. {
  29. case LogSubsystem::kMisc:
  30. os << "MISC ";
  31. break;
  32. case LogSubsystem::kNet:
  33. os << "NET ";
  34. break;
  35. default:
  36. throw out_of_range("Bad LogSubsystem");
  37. }
  38. return os;
  39. }
  40. ostream& operator<<(ostream& os, LogSeverity sev)
  41. {
  42. switch(sev)
  43. {
  44. case LogSeverity::kDebug:
  45. os << "DEBUG ";
  46. break;
  47. case LogSeverity::kInfo:
  48. os << "INFO ";
  49. break;
  50. case LogSeverity::kNotice:
  51. os << "NOTICE";
  52. break;
  53. case LogSeverity::kWarn:
  54. os << "WARN ";
  55. break;
  56. case LogSeverity::kError:
  57. os << "ERROR ";
  58. break;
  59. case LogSeverity::kCrit:
  60. os << "CRIT ";
  61. break;
  62. case LogSeverity::kAlert:
  63. os << "ALERT ";
  64. break;
  65. case LogSeverity::kEmerg:
  66. os << "EMERG ";
  67. break;
  68. default:
  69. throw out_of_range("Bad LogSeverity");
  70. }
  71. return os;
  72. }
  73. } // unnamed namespace
  74. Log::Log()
  75. {
  76. string output = Core::get().config().getString("Log.output", "stderr");
  77. if(output == "stdout")
  78. {
  79. os_ = &cout;
  80. }
  81. if(output == "stderr")
  82. {
  83. os_ = &cerr;
  84. }
  85. else
  86. {
  87. // TODO put in proper folder?
  88. fs_.open(output);
  89. os_ = &fs_;
  90. }
  91. if(!os_->good())
  92. {
  93. throw runtime_error("failed to open log file");
  94. }
  95. subsystems_ = Core::get().config().getInt("Log.subsystems", 0x7FFFFFFF);
  96. verbosity_ = Core::get().config().getInt("Log.verbosity", static_cast<int>(LogSeverity::kDebug));
  97. }
  98. ostream& Log::write(LogSubsystem sys, LogSeverity sev)
  99. {
  100. if(!(static_cast<int>(sys) & subsystems_))
  101. {
  102. return g_nullStream;
  103. }
  104. if(static_cast<int>(sev) < verbosity_)
  105. {
  106. return g_nullStream;
  107. }
  108. return *os_ << sys << " " << sev << " ";
  109. }