log.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 << __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. inline static Log* singleton = nullptr;
  66. const std::map<LogPriority,std::string> name_map = {{kLogEmergency, "EMERGENCY"},
  67. {kLogAlert, "ALERT"},
  68. {kLogCritical, "CRITICAL"},
  69. {kLogError, "ERROR"},
  70. {kLogWarning, "WARNING"},
  71. {kLogNotice, "NOTICE"},
  72. {kLogInfo, "INFO"},
  73. {kLogDebug, "DEBUG"}};
  74. friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority){
  75. static_cast<Log *>(os.rdbuf())->priority_curr = log_priority;
  76. return os;
  77. }
  78. protected:
  79. int sync(){
  80. if (buffer_.length()) {
  81. if (priority_curr >= priority_set){
  82. logfile << name_map.at(priority_curr) << ": " << buffer_ << std::endl << std::flush;
  83. }
  84. buffer_.erase();
  85. /* priority_curr = kLogDebug; // default to debug for each message */
  86. }
  87. return 0;
  88. }
  89. int overflow(int c){
  90. if (c != EOF) {
  91. buffer_ += static_cast<char>(c);
  92. } else {
  93. sync();
  94. }
  95. return c;
  96. }
  97. public:
  98. explicit Log(std::string filename, LogPriority priority)
  99. :logfile(filename, std::ofstream::out){
  100. priority_set = priority;
  101. priority_curr = kLogDebug;
  102. }
  103. static Log& init_logger(std::string filename, LogPriority priority){
  104. if (singleton == nullptr){
  105. singleton = new Log(filename, priority);
  106. std::cout << "Writing log data to " << filename << std::endl;
  107. std::clog.rdbuf(singleton);
  108. }
  109. return *singleton;
  110. }
  111. };
  112. }
  113. #endif // log_hpp