From b63072e4d029ee7a0038a0d2f053f70c5fe8b33e Mon Sep 17 00:00:00 2001 From: Ruchi Kandoi Date: Mon, 15 May 2017 18:03:35 -0700 Subject: [PATCH] Add the functionality for logging counters. Also correct clang-format for libmetricslogger Test: Run libmetriclogger_test Change-Id: If61f0bdc394a43e249ec4691da70175eeaf4cec6 --- libmetricslogger/Android.bp | 1 + .../include/metricslogger/metrics_logger.h | 15 ++++++++++----- libmetricslogger/metrics_logger.cpp | 16 ++++++++++------ libmetricslogger/metrics_logger_test.cpp | 8 ++++++-- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/libmetricslogger/Android.bp b/libmetricslogger/Android.bp index da8afe176..26a041abd 100644 --- a/libmetricslogger/Android.bp +++ b/libmetricslogger/Android.bp @@ -23,6 +23,7 @@ cc_defaults { // 524291 corresponds to sysui_histogram, from // frameworks/base/core/java/com/android/internal/logging/EventLogTags.logtags "-DHISTOGRAM_LOG_TAG=524292", + "-DCOUNT_LOG_TAG=524290", ], } diff --git a/libmetricslogger/include/metricslogger/metrics_logger.h b/libmetricslogger/include/metricslogger/metrics_logger.h index 26aa18951..36e124d9c 100644 --- a/libmetricslogger/include/metricslogger/metrics_logger.h +++ b/libmetricslogger/include/metricslogger/metrics_logger.h @@ -24,13 +24,18 @@ namespace metricslogger { // buffer. void LogHistogram(const std::string& event, int32_t data); +// Logs a Tron counter metric named |name| containing |val| count to the Tron +// log buffer. +void LogCounter(const std::string& name, int32_t val); + // TODO: replace these with the metric_logger.proto definitions enum { - LOGBUILDER_CATEGORY = 757, - LOGBUILDER_NAME = 799, - LOGBUILDER_BUCKET = 801, - LOGBUILDER_VALUE = 802, - LOGBUILDER_HISTOGRAM = 804, + LOGBUILDER_CATEGORY = 757, + LOGBUILDER_NAME = 799, + LOGBUILDER_BUCKET = 801, + LOGBUILDER_VALUE = 802, + LOGBUILDER_COUNTER = 803, + LOGBUILDER_HISTOGRAM = 804, }; } // namespace metricslogger diff --git a/libmetricslogger/metrics_logger.cpp b/libmetricslogger/metrics_logger.cpp index 0d08f5cde..6f65e10f1 100644 --- a/libmetricslogger/metrics_logger.cpp +++ b/libmetricslogger/metrics_logger.cpp @@ -25,12 +25,16 @@ namespace metricslogger { // Mirror com.android.internal.logging.MetricsLogger#histogram(). void LogHistogram(const std::string& event, int32_t data) { - android_log_event_list log(HISTOGRAM_LOG_TAG); - log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM - << LOGBUILDER_NAME << event - << LOGBUILDER_BUCKET << data - << LOGBUILDER_VALUE << 1 - << LOG_ID_EVENTS; + android_log_event_list log(HISTOGRAM_LOG_TAG); + log << LOGBUILDER_CATEGORY << LOGBUILDER_HISTOGRAM << LOGBUILDER_NAME << event + << LOGBUILDER_BUCKET << data << LOGBUILDER_VALUE << 1 << LOG_ID_EVENTS; +} + +// Mirror com.android.internal.logging.MetricsLogger#count(). +void LogCounter(const std::string& name, int32_t val) { + android_log_event_list log(COUNT_LOG_TAG); + log << LOGBUILDER_CATEGORY << LOGBUILDER_COUNTER << LOGBUILDER_NAME << name << LOGBUILDER_VALUE + << val << LOG_ID_EVENTS; } } // namespace metricslogger diff --git a/libmetricslogger/metrics_logger_test.cpp b/libmetricslogger/metrics_logger_test.cpp index 5a30ad702..440645cb8 100644 --- a/libmetricslogger/metrics_logger_test.cpp +++ b/libmetricslogger/metrics_logger_test.cpp @@ -19,6 +19,10 @@ #include TEST(MetricsLoggerTest, AddSingleBootEvent) { - android::metricslogger::LogHistogram("test_event", 42); - // TODO(jhawkins): Verify the EventLog is updated. + android::metricslogger::LogHistogram("test_event", 42); + // TODO(jhawkins): Verify the EventLog is updated. +} + +TEST(MetricsLoggerTest, AddCounterVal) { + android::metricslogger::LogCounter("test_count", 10); }