From 3cdbdd522fbbb2611713805c72c6d741a91bcfd5 Mon Sep 17 00:00:00 2001 From: Mark Salyzyn Date: Wed, 21 Dec 2016 15:34:41 -0800 Subject: [PATCH] liblog: add log/log_id.h Move common log_id_t and simple internal support function definitions that use it from log/log.h to log/log_id.h. log_id_t __android_log_buf_write __android_log_buf_print android_name_to_log_id android_log_id_to_name Test: gTest liblog-unit-tests --gtest_filter=\ liblog.log_id:\ liblog.__android_log_buf_print:\ liblog.__android_log_buf_write:\ liblog.__android_log_buf_print__concurrent64 Bug: 34250038 Change-Id: Iad3704cc72943a3094e1193a6d032c7f29a6cd5c --- liblog/include/log/log.h | 36 +-------- liblog/include/log/log_id.h | 63 ++++++++++++++++ liblog/include_vndk/log/log.h | 2 +- liblog/include_vndk/log/log_id.h | 1 + liblog/tests/Android.mk | 3 +- liblog/tests/liblog_test.cpp | 76 +------------------ liblog/tests/log_id_test.cpp | 125 +++++++++++++++++++++++++++++++ 7 files changed, 194 insertions(+), 112 deletions(-) create mode 100644 liblog/include/log/log_id.h create mode 120000 liblog/include_vndk/log/log_id.h create mode 100644 liblog/tests/log_id_test.cpp diff --git a/liblog/include/log/log.h b/liblog/include/log/log.h index 37d30bc73..e592c5c6d 100644 --- a/liblog/include/log/log.h +++ b/liblog/include/log/log.h @@ -28,6 +28,7 @@ #include #include +#include #include #include /* helper to define iovec for portability */ @@ -74,16 +75,6 @@ extern "C" { #pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" #endif -/* - * Send a simple string to the log. - */ -int __android_log_buf_write(int bufID, int prio, const char* tag, const char* text); -int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) -#if defined(__GNUC__) - __attribute__((__format__(printf, 4, 5))) -#endif - ; - /* * Simplified macro to send a verbose system log message using current LOG_TAG. */ @@ -321,25 +312,6 @@ typedef enum { (void) __android_log_bswrite(_tag, _value); #endif -#ifndef log_id_t_defined -#define log_id_t_defined -typedef enum log_id { - LOG_ID_MIN = 0, - - LOG_ID_MAIN = 0, - LOG_ID_RADIO = 1, - LOG_ID_EVENTS = 2, - LOG_ID_SYSTEM = 3, - LOG_ID_CRASH = 4, - LOG_ID_SECURITY = 5, - LOG_ID_KERNEL = 6, /* place last, third-parties can not use it */ - - LOG_ID_MAX -} log_id_t; -#endif -#define sizeof_log_id_t sizeof(typeof_log_id_t) -#define typeof_log_id_t unsigned char - /* --------------------------------------------------------------------- */ /* @@ -776,12 +748,6 @@ clockid_t android_log_clockid(); #endif /* __linux__ */ -/* - * log_id_t helpers - */ -log_id_t android_name_to_log_id(const char* logName); -const char* android_log_id_to_name(log_id_t log_id); - /* --------------------------------------------------------------------- */ #ifndef _ANDROID_USE_LIBLOG_SAFETYNET_INTERFACE diff --git a/liblog/include/log/log_id.h b/liblog/include/log/log_id.h new file mode 100644 index 000000000..3078e4eb1 --- /dev/null +++ b/liblog/include/log/log_id.h @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2005-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. + */ + +#ifndef _LIBS_LOG_LOG_ID_H +#define _LIBS_LOG_LOG_ID_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef log_id_t_defined +#define log_id_t_defined +typedef enum log_id { + LOG_ID_MIN = 0, + + LOG_ID_MAIN = 0, + LOG_ID_RADIO = 1, + LOG_ID_EVENTS = 2, + LOG_ID_SYSTEM = 3, + LOG_ID_CRASH = 4, + LOG_ID_SECURITY = 5, + LOG_ID_KERNEL = 6, /* place last, third-parties can not use it */ + + LOG_ID_MAX +} log_id_t; +#endif +#define sizeof_log_id_t sizeof(typeof_log_id_t) +#define typeof_log_id_t unsigned char + +/* + * Send a simple string to the log. + */ +int __android_log_buf_write(int bufID, int prio, const char* tag, const char* text); +int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...) +#if defined(__GNUC__) + __attribute__((__format__(printf, 4, 5))) +#endif + ; + +/* + * log_id_t helpers + */ +log_id_t android_name_to_log_id(const char* logName); +const char* android_log_id_to_name(log_id_t log_id); + +#ifdef __cplusplus +} +#endif + +#endif /* _LIBS_LOG_LOG_ID_H */ diff --git a/liblog/include_vndk/log/log.h b/liblog/include_vndk/log/log.h index 5d720f6d1..7a776d8d4 100644 --- a/liblog/include_vndk/log/log.h +++ b/liblog/include_vndk/log/log.h @@ -4,10 +4,10 @@ #define _LIBS_LOG_LOG_H #include +#include #include /*The following files will be included once they are available*/ -/*#include */ /*#include */ /* diff --git a/liblog/include_vndk/log/log_id.h b/liblog/include_vndk/log/log_id.h new file mode 120000 index 000000000..dce92b991 --- /dev/null +++ b/liblog/include_vndk/log/log_id.h @@ -0,0 +1 @@ +../../include/log/log_id.h \ No newline at end of file diff --git a/liblog/tests/Android.mk b/liblog/tests/Android.mk index ec5a99b97..6eba86e90 100644 --- a/liblog/tests/Android.mk +++ b/liblog/tests/Android.mk @@ -55,7 +55,8 @@ test_c_flags := \ -fno-builtin \ test_src_files := \ - liblog_test.cpp + liblog_test.cpp \ + log_id_test.cpp # to prevent breaking the build if bionic not relatively visible to us ifneq ($(wildcard $(LOCAL_PATH)/../../../../bionic/libc/bionic/libc_logging.cpp),) diff --git a/liblog/tests/liblog_test.cpp b/liblog/tests/liblog_test.cpp index ec0352eea..d03c6d150 100644 --- a/liblog/tests/liblog_test.cpp +++ b/liblog/tests/liblog_test.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -54,44 +55,6 @@ || (_rc == -EAGAIN)); \ _rc; }) -TEST(liblog, __android_log_buf_print) { -#ifdef __ANDROID__ - EXPECT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, - "TEST__android_log_buf_print", - "radio")); - usleep(1000); - EXPECT_LT(0, __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, - "TEST__android_log_buf_print", - "system")); - usleep(1000); - EXPECT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, - "TEST__android_log_buf_print", - "main")); - usleep(1000); -#else - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif -} - -TEST(liblog, __android_log_buf_write) { -#ifdef __ANDROID__ - EXPECT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO, - "TEST__android_log_buf_write", - "radio")); - usleep(1000); - EXPECT_LT(0, __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, - "TEST__android_log_buf_write", - "system")); - usleep(1000); - EXPECT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO, - "TEST__android_log_buf_write", - "main")); - usleep(1000); -#else - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif -} - TEST(liblog, __android_log_btwrite) { #ifdef __ANDROID__ int intBuf = 0xDEADBEEF; @@ -113,43 +76,6 @@ TEST(liblog, __android_log_btwrite) { #endif } -#ifdef __ANDROID__ -static void* ConcurrentPrintFn(void *arg) { - int ret = __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, - "TEST__android_log_print", "Concurrent %" PRIuPTR, - reinterpret_cast(arg)); - return reinterpret_cast(ret); -} -#endif - -#define NUM_CONCURRENT 64 -#define _concurrent_name(a,n) a##__concurrent##n -#define concurrent_name(a,n) _concurrent_name(a,n) - -TEST(liblog, concurrent_name(__android_log_buf_print, NUM_CONCURRENT)) { -#ifdef __ANDROID__ - pthread_t t[NUM_CONCURRENT]; - int i; - for (i=0; i < NUM_CONCURRENT; i++) { - ASSERT_EQ(0, pthread_create(&t[i], NULL, - ConcurrentPrintFn, - reinterpret_cast(i))); - } - int ret = 0; - for (i=0; i < NUM_CONCURRENT; i++) { - void* result; - ASSERT_EQ(0, pthread_join(t[i], &result)); - int this_result = reinterpret_cast(result); - if ((0 == ret) && (0 != this_result)) { - ret = this_result; - } - } - ASSERT_LT(0, ret); -#else - GTEST_LOG_(INFO) << "This test does nothing.\n"; -#endif -} - #ifdef __ANDROID__ std::string popenToString(std::string command) { std::string ret; diff --git a/liblog/tests/log_id_test.cpp b/liblog/tests/log_id_test.cpp new file mode 100644 index 000000000..3241534f1 --- /dev/null +++ b/liblog/tests/log_id_test.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2013-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 +#include +#include + +#include +// Test the APIs in this standalone include file +#include + +// We do not want to include to acquire ANDROID_LOG_INFO for +// include file API purity. We do however want to allow the _option_ that +// log/log_id.h could include this file, or related content, in the future. +#ifndef __android_LogPriority_defined +# define ANDROID_LOG_INFO 4 +#endif + +TEST(liblog, log_id) { +#ifdef __ANDROID__ + int count = 0; + + for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { + log_id_t id = static_cast(i); + const char *name = android_log_id_to_name(id); + if (id != android_name_to_log_id(name)) { + continue; + } + ++count; + fprintf(stderr, "log buffer %s\r", name); + } + ASSERT_EQ(LOG_ID_MAX, count); +#else + GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif +} + +TEST(liblog, __android_log_buf_print) { +#ifdef __ANDROID__ + EXPECT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, + "TEST__android_log_buf_print", + "radio")); + usleep(1000); + EXPECT_LT(0, __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, + "TEST__android_log_buf_print", + "system")); + usleep(1000); + EXPECT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, + "TEST__android_log_buf_print", + "main")); + usleep(1000); +#else + GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif +} + +TEST(liblog, __android_log_buf_write) { +#ifdef __ANDROID__ + EXPECT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO, + "TEST__android_log_buf_write", + "radio")); + usleep(1000); + EXPECT_LT(0, __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, + "TEST__android_log_buf_write", + "system")); + usleep(1000); + EXPECT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO, + "TEST__android_log_buf_write", + "main")); + usleep(1000); +#else + GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif +} + +#ifdef __ANDROID__ +static void* ConcurrentPrintFn(void *arg) { + int ret = __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, + "TEST__android_log_print", "Concurrent %" PRIuPTR, + reinterpret_cast(arg)); + return reinterpret_cast(ret); +} +#endif + +#define NUM_CONCURRENT 64 +#define _concurrent_name(a,n) a##__concurrent##n +#define concurrent_name(a,n) _concurrent_name(a,n) + +TEST(liblog, concurrent_name(__android_log_buf_print, NUM_CONCURRENT)) { +#ifdef __ANDROID__ + pthread_t t[NUM_CONCURRENT]; + int i; + for (i=0; i < NUM_CONCURRENT; i++) { + ASSERT_EQ(0, pthread_create(&t[i], NULL, + ConcurrentPrintFn, + reinterpret_cast(i))); + } + int ret = 0; + for (i=0; i < NUM_CONCURRENT; i++) { + void* result; + ASSERT_EQ(0, pthread_join(t[i], &result)); + int this_result = reinterpret_cast(result); + if ((0 == ret) && (0 != this_result)) { + ret = this_result; + } + } + ASSERT_LT(0, ret); +#else + GTEST_LOG_(INFO) << "This test does nothing.\n"; +#endif +}