John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_UTIL_LOG_INTERVAL_H_ |
| 2 | #define AOS_UTIL_LOG_INTERVAL_H_ |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 3 | |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 4 | #include <string> |
| 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/logging/logging.h" |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 7 | #include "aos/time/time.h" |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 8 | |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 9 | namespace aos { |
| 10 | namespace util { |
| 11 | |
| 12 | // A class to help with logging things that happen a lot only occasionally. |
| 13 | // |
| 14 | // Intended use { |
Austin Schuh | f2a50ba | 2016-12-24 16:16:26 -0800 | [diff] [blame] | 15 | // static LogInterval interval(::std::chrono::millseconds(200)); |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 16 | // |
| 17 | // if (WantToLog()) { |
| 18 | // interval.WantToLog(); |
| 19 | // } |
| 20 | // if (interval.ShouldLog()) { |
| 21 | // LOG(DEBUG, "thingie happened! (%d times)\n", interval.Count()); |
| 22 | // } |
| 23 | // } |
| 24 | class LogInterval { |
| 25 | public: |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 26 | constexpr LogInterval(::std::chrono::nanoseconds interval) |
| 27 | : interval_(interval) {} |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 28 | |
| 29 | void WantToLog() { |
| 30 | if (count_ == 0) { |
Austin Schuh | 9fe68f7 | 2019-08-10 19:32:03 -0700 | [diff] [blame] | 31 | // TODO(austin): event loops! |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 32 | last_done_ = ::aos::monotonic_clock::now(); |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 33 | } |
| 34 | ++count_; |
| 35 | } |
| 36 | bool ShouldLog() { |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 37 | const ::aos::monotonic_clock::time_point now = |
| 38 | ::aos::monotonic_clock::now(); |
| 39 | const bool r = now >= interval_ + last_done_ && count_ > 0; |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 40 | if (r) { |
| 41 | last_done_ = now; |
| 42 | } |
| 43 | return r; |
| 44 | } |
| 45 | int Count() { |
| 46 | const int r = count_; |
| 47 | count_ = 0; |
| 48 | return r; |
| 49 | } |
| 50 | |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 51 | ::std::chrono::nanoseconds interval() const { return interval_; } |
Brian Silverman | 50a9d03 | 2014-02-16 17:20:57 -0800 | [diff] [blame] | 52 | |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 53 | private: |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 54 | int count_ = 0; |
| 55 | const ::std::chrono::nanoseconds interval_; |
| 56 | ::aos::monotonic_clock::time_point last_done_ = |
| 57 | ::aos::monotonic_clock::min_time; |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | // This one is even easier to use. It always logs with a message "%s %d |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 61 | // times\n". Call LOG_INTERVAL wherever it should log and make sure Print gets |
| 62 | // called often (ie not after a conditional return) |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 63 | class SimpleLogInterval { |
| 64 | public: |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 65 | SimpleLogInterval(::std::chrono::nanoseconds interval, log_level level, |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 66 | const ::std::string &message) |
| 67 | : interval_(interval), level_(level), message_(message) {} |
| 68 | |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 69 | #define AOS_LOG_INTERVAL(simple_log) \ |
Austin Schuh | c69e679 | 2021-02-05 11:20:05 -0800 | [diff] [blame] | 70 | simple_log.WantToLog(LOG_SOURCENAME ": " AOS_STRINGIFY(__LINE__)) |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 71 | void WantToLog(const char *context) { |
| 72 | context_ = context; |
| 73 | interval_.WantToLog(); |
| 74 | } |
| 75 | |
| 76 | void Print() { |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 77 | if (interval_.ShouldLog()) { |
Austin Schuh | f257f3c | 2019-10-27 21:00:43 -0700 | [diff] [blame] | 78 | AOS_CHECK_NOTNULL(context_); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 79 | log_do(level_, "%s: %.*s %d times over %f sec\n", context_, |
Brian Silverman | 9747834 | 2014-02-16 20:26:31 -0800 | [diff] [blame] | 80 | static_cast<int>(message_.size()), message_.data(), |
Austin Schuh | 61bdc60 | 2016-12-04 19:10:10 -0800 | [diff] [blame] | 81 | interval_.Count(), |
James Kuszmaul | 651fc3f | 2019-05-15 21:14:25 -0700 | [diff] [blame] | 82 | ::aos::time::DurationInSeconds(interval_.interval())); |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 83 | context_ = NULL; |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | private: |
| 88 | LogInterval interval_; |
| 89 | const log_level level_; |
| 90 | const ::std::string message_; |
Brian Silverman | 9c9ab42 | 2014-02-25 13:42:53 -0800 | [diff] [blame] | 91 | const char *context_ = NULL; |
Brian Silverman | 01f0d22 | 2014-02-16 01:09:11 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | } // namespace util |
| 95 | } // namespace aos |
| 96 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 97 | #endif // AOS_UTIL_LOG_INTERVAL_H_ |