diff --git a/bootstat/Android.bp b/bootstat/Android.bp index d98a9d7dd..f744ad128 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 @@ cc_defaults { "-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 f902af337..26485942d 100644 --- a/bootstat/boot_event_record_store.cpp +++ b/bootstat/boot_event_record_store.cpp @@ -26,7 +26,6 @@ #include #include #include -#include "histogram_logger.h" #include "uptime_parser.h" namespace { diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp index c85667e86..a7354a7d3 100644 --- a/bootstat/bootstat.cpp +++ b/bootstat/bootstat.cpp @@ -35,9 +35,9 @@ #include #include #include +#include #include "boot_event_record_store.h" -#include "histogram_logger.h" #include "uptime_parser.h" namespace { @@ -49,7 +49,7 @@ void LogBootEvents() { 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); } } @@ -317,18 +317,18 @@ void RecordFactoryReset() { 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); @@ -347,9 +347,9 @@ void RecordFactoryReset() { // 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 000000000..ca8af574a --- /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 73% rename from bootstat/histogram_logger.h rename to libmetricslogger/include/metricslogger/metrics_logger.h index 60c7776a6..d30e56c4c 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 #include -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 77% rename from bootstat/histogram_logger.cpp rename to libmetricslogger/metrics_logger.cpp index 73f32951f..f8e01745c 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 -#include #include -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 000000000..d77199c35 --- /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 + +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/libmetricslogger/testrunner.cpp b/libmetricslogger/testrunner.cpp new file mode 100644 index 000000000..edbf4d503 --- /dev/null +++ b/libmetricslogger/testrunner.cpp @@ -0,0 +1,24 @@ +/* + * 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. + * 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 +#include + +int main(int argc, char** argv) { + ::testing::InitGoogleTest(&argc, argv); + android::base::InitLogging(argv, android::base::StderrLogger); + return RUN_ALL_TESTS(); +}