diff options
author | 2018-03-12 16:37:21 -0700 | |
---|---|---|
committer | 2018-03-12 16:42:03 -0700 | |
commit | cca6fc0fbd2c4a4757956bbf20a27cd72281afcf (patch) | |
tree | b4c6a4970183205b0f1f08dde4960295c3c5438e | |
parent | 5fa784d89019f48eaf55844262a4e0d9638b7ced (diff) |
sigchain: use libasync_safe logging.
__android_log_write doesn't have async safe guarantees. Use
libasync_safe, which has the added benefit of setting the abort message
for fatal errors.
Bug: http://b/67632085
Test: treehugger
Change-Id: I4d710753fddbce43fca44485443c446ed745ec30
-rw-r--r-- | sigchainlib/Android.bp | 4 | ||||
-rw-r--r-- | sigchainlib/log.h | 45 | ||||
-rw-r--r-- | sigchainlib/sigchain.cc | 23 | ||||
-rw-r--r-- | sigchainlib/sigchain_dummy.cc | 21 |
4 files changed, 49 insertions, 44 deletions
diff --git a/sigchainlib/Android.bp b/sigchainlib/Android.bp index 1f490cd3b9..a151d7a6bc 100644 --- a/sigchainlib/Android.bp +++ b/sigchainlib/Android.bp @@ -35,7 +35,7 @@ cc_library { }, android: { - shared_libs: ["liblog"], + whole_static_libs: ["libasync_safe"], }, }, // Sigchainlib is whole-statically linked into binaries. For Android.mk-based binaries, @@ -56,7 +56,7 @@ cc_library_static { srcs: ["sigchain_dummy.cc"], target: { android: { - shared_libs: ["liblog"], + whole_static_libs: ["libasync_safe"], }, }, } diff --git a/sigchainlib/log.h b/sigchainlib/log.h new file mode 100644 index 0000000000..d689930c1e --- /dev/null +++ b/sigchainlib/log.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 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 ART_SIGCHAINLIB_LOG_H_ +#define ART_SIGCHAINLIB_LOG_H_ + +#if defined(__ANDROID__) + +#include <async_safe/log.h> + +#define log(...) async_safe_format_log(ANDROID_LOG_ERROR, "libsigchain", __VA_ARGS__) +#define fatal async_safe_fatal + +#else + +#include <stdarg.h> +#include <stdio.h> + +static void log(const char* format, ...) { + va_list ap; + va_start(ap, format); + vprintf(format, ap); + va_end(ap); + + printf("\n"); +} + +#define fatal(...) log(__VA_ARGS__); abort() + +#endif + +#endif // ART_SIGCHAINLIB_LOG_H_ diff --git a/sigchainlib/sigchain.cc b/sigchainlib/sigchain.cc index 3127c5cfbd..2e5f46ca69 100644 --- a/sigchainlib/sigchain.cc +++ b/sigchainlib/sigchain.cc @@ -14,13 +14,6 @@ * limitations under the License. */ -#ifdef ART_TARGET_ANDROID -#include <android/log.h> -#else -#include <stdarg.h> -#include <iostream> -#endif - #include <dlfcn.h> #include <errno.h> #include <pthread.h> @@ -35,6 +28,7 @@ #include <type_traits> #include <utility> +#include "log.h" #include "sigchain.h" #if defined(__APPLE__) @@ -65,21 +59,6 @@ // doesn't have SA_RESTART, and raise the signal to avoid restarting syscalls that are // expected to be interrupted? -static void log(const char* format, ...) { - char buf[256]; - va_list ap; - va_start(ap, format); - vsnprintf(buf, sizeof(buf), format, ap); -#ifdef ART_TARGET_ANDROID - __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); -#else - std::cout << buf << "\n"; -#endif - va_end(ap); -} - -#define fatal(...) log(__VA_ARGS__); abort() - #if defined(__BIONIC__) && !defined(__LP64__) && !defined(__mips__) static int sigismember(const sigset64_t* sigset, int signum) { return sigismember64(sigset, signum); diff --git a/sigchainlib/sigchain_dummy.cc b/sigchainlib/sigchain_dummy.cc index edce965e33..c274530987 100644 --- a/sigchainlib/sigchain_dummy.cc +++ b/sigchainlib/sigchain_dummy.cc @@ -17,13 +17,7 @@ #include <stdio.h> #include <stdlib.h> -#ifdef ART_TARGET_ANDROID -#include <android/log.h> -#else -#include <stdarg.h> -#include <iostream> -#endif - +#include "log.h" #include "sigchain.h" #define ATTRIBUTE_UNUSED __attribute__((__unused__)) @@ -33,19 +27,6 @@ #pragma GCC diagnostic ignored "-Wunknown-pragmas" #pragma GCC diagnostic ignored "-Wmissing-noreturn" -static void log(const char* format, ...) { - char buf[256]; - va_list ap; - va_start(ap, format); - vsnprintf(buf, sizeof(buf), format, ap); -#ifdef ART_TARGET_ANDROID - __android_log_write(ANDROID_LOG_ERROR, "libsigchain", buf); -#else - std::cout << buf << "\n"; -#endif - va_end(ap); -} - namespace art { extern "C" void EnsureFrontOfChain(int signal ATTRIBUTE_UNUSED) { |