log.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 <csignal>
  39. #include <map>
  40. #include "config.hpp"
  41. namespace fv_util {
  42. enum LogPriority {
  43. kLogEmergency = 7, // system is unusable
  44. kLogAlert = 6, // action must be taken immediately
  45. kLogCritical = 5, // critical conditions
  46. kLogError = 4, // error conditions
  47. kLogWarning = 3, // warning conditions
  48. kLogNotice = 2, // normal, but significant, condition
  49. kLogInfo = 1, // informational message
  50. kLogDebug = 0 // debug-level message
  51. };
  52. #define CRITICAL(x) std::clog << fv_util::LogPriority::kLogCritical << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush; std::cout << "Errors encountered! See log file for details."<<std::endl;raise(SIGTERM)
  53. #define ERROR(x) std::clog << fv_util::LogPriority::kLogError << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  54. #define WARNING(x) std::clog << fv_util::LogPriority::kLogWarning << x << std::flush
  55. #define INFO(x) std::clog << fv_util::LogPriority::kLogInfo << x << std::flush
  56. #define DEBUG(x) std::clog << fv_util::LogPriority::kLogDebug << x << std::flush
  57. #define DEBUGL(x) std::clog << fv_util::LogPriority::kLogDebug << __FILE__ << "@L" << __LINE__ << " :: " << x << std::flush
  58. /**
  59. * /see http://stackoverflow.com/questions/2638654/redirect-c-stdclog-to-syslog-on-unix
  60. */
  61. std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority);
  62. class Log : public std::basic_streambuf<char, std::char_traits<char> > {
  63. private:
  64. std::string buffer_;
  65. std::ofstream logfile;
  66. LogPriority priority_curr;
  67. LogPriority priority_set;
  68. const std::map<LogPriority,std::string> name_map = {{kLogEmergency, "EMERGENCY"},
  69. {kLogAlert, "ALERT"},
  70. {kLogCritical, "CRITICAL"},
  71. {kLogError, "ERROR"},
  72. {kLogWarning, "WARNING"},
  73. {kLogNotice, "NOTICE"},
  74. {kLogInfo, "INFO"},
  75. {kLogDebug, "DEBUG"}};
  76. friend std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority){
  77. static_cast<Log *>(os.rdbuf())->priority_curr = log_priority;
  78. return os;
  79. }
  80. protected:
  81. int sync(){
  82. if (buffer_.length()) {
  83. if (priority_curr >= priority_set){
  84. logfile << name_map.at(priority_curr) << ": " << buffer_ << std::endl << std::flush;
  85. }
  86. buffer_.erase();
  87. /* priority_curr = kLogDebug; // default to debug for each message */
  88. }
  89. return 0;
  90. }
  91. int overflow(int c){
  92. if (c != EOF) {
  93. buffer_ += static_cast<char>(c);
  94. } else {
  95. sync();
  96. }
  97. return c;
  98. }
  99. public:
  100. explicit Log(const std::string& filename, LogPriority priority)
  101. :logfile(filename, std::ofstream::out){
  102. priority_set = priority;
  103. priority_curr = kLogDebug;
  104. }
  105. };
  106. Log* the_log = nullptr;
  107. void init_log(LogPriority priority){
  108. if(the_config == nullptr)
  109. std::cerr << "Must initialize config before setting up logging!" << std::endl;
  110. std::string out = the_config->get_output_filename();
  111. std::string filename = out.substr(0, out.find_last_of(".")) + ".log";
  112. the_log = new Log(filename, priority);
  113. std::cout << "Writing log data to " << filename << std::endl;
  114. std::clog.rdbuf(the_log);
  115. }
  116. }
  117. #endif // log_hpp