libmetricslogger: Refactor Tron metrics histogram logging out of
bootstat.
To be shared with other native components that want to log histograms.
Bug: 34456830
Test: libmetricslogger_test
Change-Id: I94a1a91c6d33e443d66bc480158dc2470d6c9031
diff --git a/bootstat/Android.bp b/bootstat/Android.bp
index d98a9d7..f744ad1 100644
--- a/bootstat/Android.bp
+++ b/bootstat/Android.bp
@@ -16,7 +16,6 @@
bootstat_lib_src_files = [
"boot_event_record_store.cpp",
- "histogram_logger.cpp",
"uptime_parser.cpp",
]
@@ -27,15 +26,12 @@
"-Wall",
"-Wextra",
"-Werror",
-
- // 524291 corresponds to sysui_histogram, from
- // frameworks/base/core/java/com/android/internal/logging/EventLogTags.logtags
- "-DHISTOGRAM_LOG_TAG=524291",
],
shared_libs: [
"libbase",
"libcutils",
"liblog",
+ "libmetricslogger",
],
whole_static_libs: ["libgtest_prod"],
// Clang is required because of C++14
diff --git a/bootstat/boot_event_record_store.cpp b/bootstat/boot_event_record_store.cpp
index f902af3..2648594 100644
--- a/bootstat/boot_event_record_store.cpp
+++ b/bootstat/boot_event_record_store.cpp
@@ -26,7 +26,6 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
-#include "histogram_logger.h"
#include "uptime_parser.h"
namespace {
diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp
index a626704..595239c 100644
--- a/bootstat/bootstat.cpp
+++ b/bootstat/bootstat.cpp
@@ -33,9 +33,9 @@
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <cutils/properties.h>
+#include <metricslogger/metrics_logger.h>
#include "boot_event_record_store.h"
-#include "histogram_logger.h"
#include "uptime_parser.h"
namespace {
@@ -47,7 +47,7 @@
auto events = boot_event_store.GetAllBootEvents();
for (auto i = events.cbegin(); i != events.cend(); ++i) {
- bootstat::LogHistogram(i->first, i->second);
+ android::metricslogger::LogHistogram(i->first, i->second);
}
}
@@ -292,18 +292,18 @@
if (current_time_utc < 0) {
// UMA does not display negative values in buckets, so convert to positive.
- bootstat::LogHistogram(
+ android::metricslogger::LogHistogram(
"factory_reset_current_time_failure", std::abs(current_time_utc));
- // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
+ // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
// is losing records somehow.
boot_event_store.AddBootEventWithValue(
"factory_reset_current_time_failure", std::abs(current_time_utc));
return;
} else {
- bootstat::LogHistogram("factory_reset_current_time", current_time_utc);
+ android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
- // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
+ // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
// is losing records somehow.
boot_event_store.AddBootEventWithValue(
"factory_reset_current_time", current_time_utc);
@@ -322,9 +322,9 @@
// Calculate and record the difference in time between now and the
// factory_reset time.
time_t factory_reset_utc = record.second;
- bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc);
+ android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
- // Logging via BootEventRecordStore to see if using bootstat::LogHistogram
+ // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
// is losing records somehow.
boot_event_store.AddBootEventWithValue(
"factory_reset_record_value", factory_reset_utc);
diff --git a/libmetricslogger/Android.bp b/libmetricslogger/Android.bp
new file mode 100644
index 0000000..ca8af57
--- /dev/null
+++ b/libmetricslogger/Android.bp
@@ -0,0 +1,64 @@
+// Copyright 2017 The Android Open Source Project
+
+metricslogger_lib_src_files = [
+ "metrics_logger.cpp",
+]
+
+cc_defaults {
+ name: "metricslogger_defaults",
+
+ clang: true,
+ host_supported: true,
+
+ export_include_dirs: ["include"],
+ local_include_dirs: ["include"],
+ shared_libs: ["liblog"],
+ whole_static_libs: ["libgtest_prod"],
+
+ cflags: [
+ "-Wall",
+ "-Wextra",
+ "-Werror",
+
+ // 524291 corresponds to sysui_histogram, from
+ // frameworks/base/core/java/com/android/internal/logging/EventLogTags.logtags
+ "-DHISTOGRAM_LOG_TAG=524291",
+ ],
+}
+
+// metricslogger shared library
+// -----------------------------------------------------------------------------
+cc_library_shared {
+ name: "libmetricslogger",
+ srcs: metricslogger_lib_src_files,
+ defaults: ["metricslogger_defaults"],
+}
+
+// metricslogger shared library, debug
+// -----------------------------------------------------------------------------
+cc_library_shared {
+ name: "libmetricslogger_debug",
+ srcs: metricslogger_lib_src_files,
+ defaults: ["metricslogger_defaults"],
+
+ target: {
+ host: {
+ cflags: ["-UNDEBUG"],
+ },
+ },
+}
+
+// Native tests
+// -----------------------------------------------------------------------------
+cc_test {
+ name: "metricslogger_tests",
+ defaults: ["metricslogger_defaults"],
+ shared_libs: [
+ "libbase",
+ "libmetricslogger_debug",
+ ],
+ srcs: [
+ "metrics_logger_test.cpp",
+ "testrunner.cpp",
+ ],
+}
diff --git a/bootstat/histogram_logger.h b/libmetricslogger/include/metricslogger/metrics_logger.h
similarity index 72%
rename from bootstat/histogram_logger.h
rename to libmetricslogger/include/metricslogger/metrics_logger.h
index 60c7776..d30e56c 100644
--- a/bootstat/histogram_logger.h
+++ b/libmetricslogger/include/metricslogger/metrics_logger.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016 The Android Open Source Project
+ * Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,10 +17,12 @@
#include <cstdint>
#include <string>
-namespace bootstat {
+namespace android {
+namespace metricslogger {
-// Builds an EventLog buffer named |event| containing |data| and writes
-// the log into the Tron histogram logs.
+// Logs a Tron histogram metric named |event| containing |data| to the Tron log
+// buffer.
void LogHistogram(const std::string& event, int32_t data);
-} // namespace bootstat
\ No newline at end of file
+} // namespace metricslogger
+} // namespace android
diff --git a/bootstat/histogram_logger.cpp b/libmetricslogger/metrics_logger.cpp
similarity index 76%
rename from bootstat/histogram_logger.cpp
rename to libmetricslogger/metrics_logger.cpp
index 73f3295..f8e0174 100644
--- a/bootstat/histogram_logger.cpp
+++ b/libmetricslogger/metrics_logger.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016 The Android Open Source Project
+ * Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,19 +14,19 @@
* limitations under the License.
*/
-#include "histogram_logger.h"
+#include "metricslogger/metrics_logger.h"
#include <cstdlib>
-#include <android-base/logging.h>
#include <log/log_event_list.h>
-namespace bootstat {
+namespace android {
+namespace metricslogger {
void LogHistogram(const std::string& event, int32_t data) {
- LOG(INFO) << "Logging histogram: " << event << " " << data;
android_log_event_list log(HISTOGRAM_LOG_TAG);
log << event << data << LOG_ID_EVENTS;
}
-} // namespace bootstat
+} // namespace metricslogger
+} // namespace android
diff --git a/libmetricslogger/metrics_logger_test.cpp b/libmetricslogger/metrics_logger_test.cpp
new file mode 100644
index 0000000..d77199c
--- /dev/null
+++ b/libmetricslogger/metrics_logger_test.cpp
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "metricslogger/metrics_logger.h"
+
+#include <gtest/gtest.h>
+
+TEST(MetricsLoggerTest, AddSingleBootEvent) {
+ android::metricslogger::LogHistogram("test_event", 42);
+ /*pid_t pid = getpid();
+ struct logger_list *logger_list = android_logger_list_open(
+ LOG_ID_EVENTS, ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, pid);
+
+ logger_list = NULL;
+ log_msg log_msg;
+ android_logger_list_read(logger_list, &log_msg);
+ std::cout << log_msg.len() << std::endl;*/
+}
diff --git a/bootstat/histogram_logger.h b/libmetricslogger/testrunner.cpp
similarity index 64%
copy from bootstat/histogram_logger.h
copy to libmetricslogger/testrunner.cpp
index 60c7776..edbf4d5 100644
--- a/bootstat/histogram_logger.h
+++ b/libmetricslogger/testrunner.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2016 The Android Open Source Project
+ * Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,11 @@
* limitations under the License.
*/
-#include <cstdint>
-#include <string>
+#include <android-base/logging.h>
+#include <gtest/gtest.h>
-namespace bootstat {
-
-// Builds an EventLog buffer named |event| containing |data| and writes
-// the log into the Tron histogram logs.
-void LogHistogram(const std::string& event, int32_t data);
-
-} // namespace bootstat
\ No newline at end of file
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ android::base::InitLogging(argv, android::base::StderrLogger);
+ return RUN_ALL_TESTS();
+}