log.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #include "config.hpp"
  40. namespace fv_util {
  41. enum LogPriority {
  42. kLogEmergency = 7, // system is unusable
  43. kLogAlert = 6, // action must be taken immediately
  44. kLogCritical = 5, // critical conditions
  45. kLogError = 4, // error conditions
  46. kLogWarning = 3, // warning conditions
  47. kLogNotice = 2, // normal, but significant, condition
  48. kLogInfo = 1, // informational message
  49. kLogDebug = 0 // debug-level message
  50. };
  51. #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)
  52. #define ERROR(x) std::clog << fv_util::LogPriority::kLogError << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  53. #define WARNING(x) std::clog << fv_util::LogPriority::kLogWarning << x << std::flush
  54. #define INFO(x) std::clog << fv_util::LogPriority::kLogInfo << x << std::flush
  55. #define DEBUG(x) std::clog << fv_util::LogPriority::kLogDebug << x << std::flush
  56. #define DEBUGL(x) std::clog << fv_util::LogPriority::kLogDebug << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  57. /**
  58. * /see http://stackoverflow.com/questions/2638654/redirect-c-stdclog-to-syslog-on-unix
  59. */
  60. std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
  61. class Log : public std::basic_streambuf<char, std::char_traits<char> > {
  62. private:
  63. std::string buffer_;
  64. std::ofstream logfile;
  65. LogPriority priority_curr;
  66. LogPriority priority_set;
  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(const std::string& filename, LogPriority priority)
  100. :logfile(filename, std::ofstream::out){
  101. priority_set = priority;
  102. priority_curr = kLogDebug;
  103. }
  104. };
  105. Log* the_log = nullptr;
  106. void init_log(LogPriority priority){
  107. if(the_config == nullptr)
  108. std::cerr << "Must initialize config before setting up logging!" << std::endl;
  109. std::string out = the_config->get_output_filename();
  110. std::string filename = out.substr(0, out.find_last_of(".")) + ".log";
  111. the_log = new Log(filename, priority);
  112. std::cout << "Writing log data to " << filename << std::endl;
  113. std::clog.rdbuf(the_log);
  114. }
  115. }
  116. #endif // log_hpp