Log.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <string>
  5. namespace da {
  6. struct nullstream : std::ostream {
  7. nullstream() : std::ostream(0) {}
  8. };
  9. template <typename T>
  10. nullstream &operator<<(nullstream &o, T const &x) {
  11. return o;
  12. }
  13. nullstream __nullstream;
  14. class LogMessage {
  15. std::string level;
  16. std::ostream &ofs;
  17. static bool enable;
  18. public:
  19. LogMessage(const std::string &l)
  20. : level(l), ofs(enable ? std::cerr : __nullstream) {
  21. stream() << "[" << level << "]\t";
  22. }
  23. LogMessage(std::ostream &o) : level("ERROR"), ofs(o) {
  24. stream() << "[" << level << "]\t";
  25. }
  26. inline std::ostream &stream() { return ofs; }
  27. ~LogMessage() { stream() << std::endl; }
  28. static void Enable(bool _enable) { enable = _enable; }
  29. };
  30. bool LogMessage::enable = true;
  31. } // namespace da
  32. #define LOG(type) da::LogMessage(#type).stream()
  33. #define DLOG(type) da::LogMessage(#type).stream()
  34. #define VLOG(level) \
  35. if ((level) <= FLAGS_v) LOG(INFO)
  36. #define CHECK(x) \
  37. if (x) { \
  38. } else \
  39. LOG(ERROR) << #x
  40. #define DCHECK(x) CHECK(x)
  41. #define CHECK_EQ(x, y) CHECK((x) == (y))
  42. #define CHECK_LT(x, y) CHECK((x) < (y))
  43. #define CHECK_GT(x, y) CHECK((x) > (y))
  44. #define CHECK_LE(x, y) CHECK((x) <= (y))
  45. #define CHECK_GE(x, y) CHECK((x) >= (y))
  46. #define CHECK_NE(x, y) CHECK((x) != (y))
  47. #define CHECK_NOTNULL(x) \
  48. ({ \
  49. CHECK_NE(x, NULL); \
  50. (x); \
  51. })
  52. #define DCHECK_EQ(x, y) DCHECK((x) == (y))
  53. #define DCHECK_LT(x, y) DCHECK((x) < (y))
  54. #define DCHECK_GT(x, y) DCHECK((x) > (y))
  55. #define DCHECK_LE(x, y) DCHECK((x) <= (y))
  56. #define DCHECK_GE(x, y) DCHECK((x) >= (y))
  57. #define DCHECK_NE(x, y) DCHECK((x) != (y))