log.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. * @section DESCRIPTION
  32. */
  33. #ifndef log_hpp
  34. #define log_hpp
  35. #include <iostream>
  36. #include <fstream>
  37. #include <cstring>
  38. #include <map>
  39. namespace filval::util{
  40. enum LogPriority {
  41. kLogEmergency = 7, // system is unusable
  42. kLogAlert = 6, // action must be taken immediately
  43. kLogCritical = 5, // critical conditions
  44. kLogError = 4, // error conditions
  45. kLogWarning = 3, // warning conditions
  46. kLogNotice = 2, // normal, but significant, condition
  47. kLogInfo = 1, // informational message
  48. kLogDebug = 0 // debug-level message
  49. };
  50. #define CRITICAL(x,y) std::clog << filval::util::LogPriority::kLogCritical << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush; exit(y)
  51. #define ERROR(x) std::clog << filval::util::LogPriority::kLogError << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  52. #define WARNING(x) std::clog << filval::util::LogPriority::kLogWarning << x << std::flush
  53. #define INFO(x) std::clog << filval::util::LogPriority::kLogInfo << x << std::flush
  54. #define DEBUG(x) std::clog << filval::util::LogPriority::kLogDebug << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  55. /**
  56. * /see http://stackoverflow.com/questions/2638654/redirect-c-stdclog-to-syslog-on-unix
  57. */
  58. std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
  59. class Log : public std::basic_streambuf<char, std::char_traits<char> > {
  60. private:
  61. std::string buffer_;
  62. std::ofstream logfile;
  63. LogPriority priority_curr;
  64. LogPriority priority_set;
  65. std::map<LogPriority,std::string> name_map = {{kLogEmergency, "EMERGENCY"},
  66. {kLogAlert, "ALERT"},
  67. {kLogCritical, "CRITICAL"},
  68. {kLogError, "ERROR"},
  69. {kLogWarning, "WARNING"},
  70. {kLogNotice, "NOTICE"},
  71. {kLogInfo, "INFO"},
  72. {kLogDebug, "DEBUG"}};
  73. friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority){
  74. static_cast<Log *>(os.rdbuf())->priority_curr = log_priority;
  75. return os;
  76. }
  77. protected:
  78. int sync(){
  79. if (buffer_.length()) {
  80. if (priority_curr >= priority_set){
  81. logfile << name_map[priority_curr] << ": " << buffer_ << std::endl << std::flush;
  82. }
  83. buffer_.erase();
  84. /* priority_curr = kLogDebug; // default to debug for each message */
  85. }
  86. return 0;
  87. }
  88. int overflow(int c){
  89. if (c != EOF) {
  90. buffer_ += static_cast<char>(c);
  91. } else {
  92. sync();
  93. }
  94. return c;
  95. }
  96. public:
  97. explicit Log(std::string filename, LogPriority priority)
  98. :logfile(filename, std::ofstream::out){
  99. priority_set = priority;
  100. priority_curr = kLogDebug;
  101. }
  102. };
  103. }
  104. #endif // log_hpp