log.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 fv::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 << fv::util::LogPriority::kLogCritical << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush; std::cout << "Errors encountered! See log file for details."<<std::endl;exit(y)
  51. #define ERROR(x) std::clog << fv::util::LogPriority::kLogError << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  52. #define WARNING(x) std::clog << fv::util::LogPriority::kLogWarning << x << std::flush
  53. #define INFO(x) std::clog << fv::util::LogPriority::kLogInfo << x << std::flush
  54. #define DEBUG(x) std::clog << fv::util::LogPriority::kLogDebug << x << std::flush
  55. #define DEBUGL(x) std::clog << fv::util::LogPriority::kLogDebug << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  56. /**
  57. * /see http://stackoverflow.com/questions/2638654/redirect-c-stdclog-to-syslog-on-unix
  58. */
  59. std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
  60. class Log : public std::basic_streambuf<char, std::char_traits<char> > {
  61. private:
  62. std::string buffer_;
  63. std::ofstream logfile;
  64. LogPriority priority_curr;
  65. LogPriority priority_set;
  66. inline static Log* singleton = nullptr;
  67. const std::map<LogPriority,std::string> name_map = {{kLogEmergency, "EMERGENCY"},
  68. {kLogAlert, "ALERT"},
  69. {kLogCritical, "CRITICAL"},
  70. {kLogError, "ERROR"},
  71. {kLogWarning, "WARNING"},
  72. {kLogNotice, "NOTICE"},
  73. {kLogInfo, "INFO"},
  74. {kLogDebug, "DEBUG"}};
  75. friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority){
  76. static_cast<Log *>(os.rdbuf())->priority_curr = log_priority;
  77. return os;
  78. }
  79. protected:
  80. int sync(){
  81. if (buffer_.length()) {
  82. if (priority_curr >= priority_set){
  83. logfile << name_map.at(priority_curr) << ": " << buffer_ << std::endl << std::flush;
  84. }
  85. buffer_.erase();
  86. /* priority_curr = kLogDebug; // default to debug for each message */
  87. }
  88. return 0;
  89. }
  90. int overflow(int c){
  91. if (c != EOF) {
  92. buffer_ += static_cast<char>(c);
  93. } else {
  94. sync();
  95. }
  96. return c;
  97. }
  98. public:
  99. explicit Log(std::string filename, LogPriority priority)
  100. :logfile(filename, std::ofstream::out){
  101. priority_set = priority;
  102. priority_curr = kLogDebug;
  103. }
  104. static Log& init_logger(std::string filename, LogPriority priority){
  105. if (singleton == nullptr){
  106. singleton = new Log(filename, priority);
  107. std::cout << "Writing log data to " << filename << std::endl;
  108. std::clog.rdbuf(singleton);
  109. }
  110. return *singleton;
  111. }
  112. };
  113. }
  114. #endif // log_hpp