blob: 9acbe407be4333b855a01d8a5b3623334fabf9cb [file] [log] [blame]
Brian Silverman01f0d222014-02-16 01:09:11 -08001#ifndef AOS_COMMON_UTIL_LOG_INTERVAL_H_
2#define AOS_COMMON_UTIL_LOG_INTERVAL_H_
3
4#include "aos/common/time.h"
5#include "aos/common/logging/logging.h"
6
7#include <string>
8
9namespace aos {
10namespace util {
11
12// A class to help with logging things that happen a lot only occasionally.
13//
14// Intended use {
15// static LogInterval interval(::aos::time::Time::InSeconds(0.2));
16//
17// if (WantToLog()) {
18// interval.WantToLog();
19// }
20// if (interval.ShouldLog()) {
21// LOG(DEBUG, "thingie happened! (%d times)\n", interval.Count());
22// }
23// }
24class LogInterval {
25 public:
26 constexpr LogInterval(const ::aos::time::Time &interval)
27 : count_(0), interval_(interval), last_done_(0, 0) {}
28
29 void WantToLog() {
30 if (count_ == 0) {
31 last_done_ = ::aos::time::Time::Now();
32 }
33 ++count_;
34 }
35 bool ShouldLog() {
36 const ::aos::time::Time now = ::aos::time::Time::Now();
37 const bool r = (now - last_done_) >= interval_;
38 if (r) {
39 last_done_ = now;
40 }
41 return r;
42 }
43 int Count() {
44 const int r = count_;
45 count_ = 0;
46 return r;
47 }
48
Brian Silverman50a9d032014-02-16 17:20:57 -080049 const ::aos::time::Time &interval() const { return interval_; }
50
Brian Silverman01f0d222014-02-16 01:09:11 -080051 private:
52 int count_;
53 const ::aos::time::Time interval_;
54 ::aos::time::Time last_done_;
55};
56
57// This one is even easier to use. It always logs with a message "%s %d
58// times\n".
59class SimpleLogInterval {
60 public:
61 SimpleLogInterval(const ::aos::time::Time &interval, log_level level,
62 const ::std::string &message)
63 : interval_(interval), level_(level), message_(message) {}
64
65#define LOG_INTERVAL(simple_log) \
66 simple_log.Hit(LOG_SOURCENAME ": " STRINGIFY(__LINE__))
67 void Hit(const char *context) {
68 interval_.WantToLog();
69 if (interval_.ShouldLog()) {
Brian Silverman50a9d032014-02-16 17:20:57 -080070 log_do(level_, "%s: %.*s %d times over %f sec\n", context,
71 message_.size(), message_.data(), interval_.Count(),
72 interval_.interval().ToSeconds());
Brian Silverman01f0d222014-02-16 01:09:11 -080073 }
74 }
75
76 private:
77 LogInterval interval_;
78 const log_level level_;
79 const ::std::string message_;
80};
81
82} // namespace util
83} // namespace aos
84
85#endif // AOS_COMMON_UTIL_LOG_INTERVAL_H_