Log.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright: 2014, Ableton AG, Berlin, all rights reserved
  2. #pragma once
  3. #include <ableton/util/Injected.hpp>
  4. #include <iostream>
  5. #include <string>
  6. namespace ableton
  7. {
  8. namespace util
  9. {
  10. // Null object for the Log concept
  11. struct NullLog
  12. {
  13. template <typename T>
  14. friend const NullLog& operator<<(const NullLog& log, const T&)
  15. {
  16. return log;
  17. }
  18. friend const NullLog& debug(const NullLog& log) { return log; }
  19. friend const NullLog& info(const NullLog& log) { return log; }
  20. friend const NullLog& warning(const NullLog& log) { return log; }
  21. friend const NullLog& error(const NullLog& log) { return log; }
  22. friend NullLog channel(const NullLog&, std::string) { return {}; }
  23. };
  24. // std streams-based log
  25. struct StdLog
  26. {
  27. StdLog(std::string channelName = "")
  28. : mChannelName(std::move(channelName))
  29. {
  30. }
  31. // Stream type used by std log to prepend the channel name to log messages
  32. struct StdLogStream
  33. {
  34. StdLogStream(std::ostream& ioStream, const std::string& channelName)
  35. : mpIoStream(&ioStream)
  36. , mChannelName(channelName)
  37. {
  38. ioStream << "[" << mChannelName << "] ";
  39. }
  40. StdLogStream(StdLogStream&& rhs)
  41. : mpIoStream(rhs.mpIoStream)
  42. , mChannelName(rhs.mChannelName)
  43. {
  44. rhs.mpIoStream = nullptr;
  45. }
  46. ~StdLogStream()
  47. {
  48. if (mpIoStream)
  49. {
  50. (*mpIoStream) << "\n";
  51. }
  52. }
  53. template <typename T>
  54. std::ostream& operator<<(const T& rhs)
  55. {
  56. (*mpIoStream) << rhs;
  57. return *mpIoStream;
  58. }
  59. std::ostream* mpIoStream;
  60. const std::string& mChannelName;
  61. };
  62. friend StdLogStream debug(const StdLog& log) { return {std::clog, log.mChannelName}; }
  63. friend StdLogStream info(const StdLog& log) { return {std::clog, log.mChannelName}; }
  64. friend StdLogStream warning(const StdLog& log) { return {std::clog, log.mChannelName}; }
  65. friend StdLogStream error(const StdLog& log) { return {std::cerr, log.mChannelName}; }
  66. friend StdLog channel(const StdLog& log, const std::string& channelName)
  67. {
  68. auto compositeName =
  69. log.mChannelName.empty() ? channelName : log.mChannelName + "::" + channelName;
  70. return {std::move(compositeName)};
  71. }
  72. std::string mChannelName;
  73. };
  74. // Log adapter that adds timestamps
  75. template <typename Log>
  76. struct Timestamped
  77. {
  78. using InnerLog = typename util::Injected<Log>::type;
  79. Timestamped() = default;
  80. Timestamped(util::Injected<Log> log)
  81. : mLog(std::move(log))
  82. {
  83. }
  84. util::Injected<Log> mLog;
  85. friend decltype(debug(std::declval<InnerLog>())) debug(const Timestamped& log)
  86. {
  87. return log.logTimestamp(debug(*log.mLog));
  88. }
  89. friend decltype(info(std::declval<InnerLog>())) info(const Timestamped& log)
  90. {
  91. return log.logTimestamp(info(*log.mLog));
  92. }
  93. friend decltype(warning(std::declval<InnerLog>())) warning(const Timestamped& log)
  94. {
  95. return log.logTimestamp(warning(*log.mLog));
  96. }
  97. friend decltype(error(std::declval<InnerLog>())) error(const Timestamped& log)
  98. {
  99. return log.logTimestamp(error(*log.mLog));
  100. }
  101. friend Timestamped channel(const Timestamped& log, const std::string& channelName)
  102. {
  103. return {channel(*log.mLog, channelName)};
  104. }
  105. template <typename Stream>
  106. Stream logTimestamp(Stream&& streamRef) const
  107. {
  108. using namespace std::chrono;
  109. Stream stream = std::forward<Stream>(streamRef);
  110. stream << "|"
  111. << duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count()
  112. << "ms| ";
  113. return stream;
  114. }
  115. };
  116. } // namespace util
  117. } // namespace ableton