From c418edb291d1a0108330663fcb6e8558fb1287e9 Mon Sep 17 00:00:00 2001 From: Liangliang He Date: Mon, 28 Aug 2017 17:24:04 +0800 Subject: [PATCH] Add logging/selective build support --- mace/BUILD | 25 ++++++++++++++++ mace/core/BUILD | 10 +++++++ mace/core/logging.h | 60 +++++++++++++++++++++++++++++++++++++ mace/examples/BUILD | 11 +++++-- mace/examples/README.md | 9 ++++-- mace/examples/helloworld.cc | 5 ++-- mace/mace.bzl | 25 ++++++++++++++++ mace/utils/BUILD | 0 8 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 mace/BUILD create mode 100644 mace/core/logging.h create mode 100644 mace/mace.bzl delete mode 100644 mace/utils/BUILD diff --git a/mace/BUILD b/mace/BUILD new file mode 100644 index 00000000..17a4be41 --- /dev/null +++ b/mace/BUILD @@ -0,0 +1,25 @@ +config_setting( + name = "android", + values = { + "crosstool_top": "//external:android/crosstool", + }, + visibility = ["//visibility:public"], +) + +config_setting( + name = "android_armv7", + values = { + "crosstool_top": "//external:android/crosstool", + "android_cpu": "armeabi-v7a", + }, + visibility = ["//visibility:public"], +) + +config_setting( + name = "android_arm64", + values = { + "crosstool_top": "//external:android/crosstool", + "android_cpu": "arm64-v8a", + }, + visibility = ["//visibility:public"], +) diff --git a/mace/core/BUILD b/mace/core/BUILD index e69de29b..b6b2cdbd 100644 --- a/mace/core/BUILD +++ b/mace/core/BUILD @@ -0,0 +1,10 @@ +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "lib_core", + hdrs = [ + "logging.h" + ], + srcs = [ + ], + ) diff --git a/mace/core/logging.h b/mace/core/logging.h new file mode 100644 index 00000000..ba5e4c2a --- /dev/null +++ b/mace/core/logging.h @@ -0,0 +1,60 @@ +#ifndef MACE_COMMON_LOGGING_H_ +#define MACE_COMMON_LOGGING_H_ + +#ifdef __ANDROID__ +#include +#else +#include +#endif + +namespace mace { + +const int FATAL = 0; +const int ERROR = 1; +const int WARN = 2; +const int INFO = 3; +const int DEBUG = 4; +const int VERBOSE = 5; + +namespace internal { + +const char *kTag = "MACE"; + + +#ifdef __ANDROID__ + +#define _MACE_LOG_FATAL \ + do { \ + __android_log_print(ANDROID_LOG_FATAL, mace::internal::kTag, __VA_ARGS__); \ + abort(); \ + } while (0) + +#define _MACE_LOG_ERROR(...) \ + __android_log_print(ANDROID_LOG_ERROR, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_WARN(...) \ + __android_log_print(ANDROID_LOG_WARN, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_INFO(...) \ + __android_log_print(ANDROID_LOG_INFO, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_DEBUG(...) \ + __android_log_print(ANDROID_LOG_DEBUG, mace::internal::kTag, __VA_ARGS__) +#define _MACE_LOG_VERBOSE(...) \ + __android_log_print(ANDROID_LOG_VERBOSE, mace::internal::kTag, __VA_ARGS__) + + +#define LOG(severity, ...) _MACE_LOG_##severity(__VA_ARGS__) + +#else // Non Android, just for tests + +// TODO(heliangliang): Fix newline +#define LOG(severity, ...) \ + do { \ + printf(__VA_ARGS__); \ + printf("\n"); \ + } while (0) + +#endif // __ANDROID__ + +} // namespace internal +} // namespace mace + +#endif // MACE_COMMON_LOGGING_H_ diff --git a/mace/examples/BUILD b/mace/examples/BUILD index 887f401b..e932dd38 100644 --- a/mace/examples/BUILD +++ b/mace/examples/BUILD @@ -1,11 +1,16 @@ # Examples +load( + "//mace:mace.bzl", + "if_android", + ) cc_binary( name = "helloworld", srcs = [ "helloworld.cc", ], - linkopts = [ - "-pie", - ] + deps = [ + "//mace/core:lib_core", + ], + linkopts = if_android(["-pie", "-llog"]) ) diff --git a/mace/examples/README.md b/mace/examples/README.md index 25d1f638..4feb5c8d 100644 --- a/mace/examples/README.md +++ b/mace/examples/README.md @@ -1,7 +1,7 @@ Examples ======= -* Build the example +* Build the example (e.g., with armeabi-v7a target) ``` bazel build mace/examples:helloworld \ --crosstool_top=//external:android/crosstool \ @@ -9,7 +9,7 @@ bazel build mace/examples:helloworld \ --cpu=armeabi-v7a ``` -* To run adb inside docker, the network should use 'host': +* To run adb inside docker, the container network should use 'host' ``` docker run -it --net=host mace-dev /bin/bash ``` @@ -20,3 +20,8 @@ adb shell "mkdir /data/local/tmp/helloword" adb shell push bazel-bin/mace/examples/helloworld /data/local/tmp/helloword adb shell /data/local/tmp/helloword/helloworld ``` + +* Check the logs +``` +adb logcat | grep MACE +``` diff --git a/mace/examples/helloworld.cc b/mace/examples/helloworld.cc index 5b38b4bd..f4883a85 100644 --- a/mace/examples/helloworld.cc +++ b/mace/examples/helloworld.cc @@ -1,6 +1,7 @@ -#include +#include "mace/core/logging.h" int main() { - printf("Hello world\n"); + LOG(INFO, "Hello World"); + return 0; } diff --git a/mace/mace.bzl b/mace/mace.bzl new file mode 100644 index 00000000..3b57e724 --- /dev/null +++ b/mace/mace.bzl @@ -0,0 +1,25 @@ +# -*- Python -*- + +def if_android(a): + return select({ + "//mace:android": a, + "//conditions:default": [], + }) + +def if_not_android(a): + return select({ + "//mace:android": [], + "//conditions:default": a, + }) + +def if_android_armv7(a): + return select({ + "//mace:android_armv7": a, + "//conditions:default": [], + }) + +def if_android_arm64(a): + return select({ + "//mace:android_arm64": a, + "//conditions:default": [], + }) diff --git a/mace/utils/BUILD b/mace/utils/BUILD deleted file mode 100644 index e69de29b..00000000 -- GitLab