Advertisement
pheryus

Untitled

Nov 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #ifndef _LOGGER_HPP_
  2. #define _LOGGER_HPP_
  3.  
  4. #include <iostream>
  5. #include <sstream>
  6.  
  7. /* consider adding boost thread id since we'll want to know whose writting and
  8. * won't want to repeat it for every single call */
  9.  
  10. /* consider adding policy class to allow users to redirect logging to specific
  11. * files via the command line
  12. */
  13.  
  14. enum loglevel_e
  15. {logERROR, logWARNING, logINFO, logDEBUG, logDEBUG1, logDEBUG2, logDEBUG3, logDEBUG4};
  16.  
  17. class logIt{
  18.  
  19. public:
  20. logIt(loglevel_e _loglevel = logERROR) {
  21. _buffer << _loglevel << " :"
  22. << std::string(
  23. _loglevel > logDEBUG
  24. ? (_loglevel - logDEBUG) * 4
  25. : 1
  26. , ' ');
  27. }
  28.  
  29. template <typename T>
  30. logIt & operator<<(T const & value)
  31. {
  32. _buffer << value;
  33. return *this;
  34. }
  35.  
  36. ~logIt()
  37. {
  38. _buffer << std::endl;
  39. // This is atomic according to the POSIX standard
  40. // http://www.gnu.org/s/libc/manual/html_node/Streams-and-Threads.html
  41. std::cerr << _buffer.str();
  42. }
  43.  
  44. private:
  45. std::ostringstream _buffer;
  46. };
  47.  
  48. extern loglevel_e loglevel;
  49.  
  50. #define log(level) \
  51. if (level > loglevel) ; \
  52. else logIt(level)
  53.  
  54. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement