提交 c418edb2 编写于 作者: L Liangliang He

Add logging/selective build support

上级 31dc70cc
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"],
)
package(default_visibility = ["//visibility:public"])
cc_library(
name = "lib_core",
hdrs = [
"logging.h"
],
srcs = [
],
)
#ifndef MACE_COMMON_LOGGING_H_
#define MACE_COMMON_LOGGING_H_
#ifdef __ANDROID__
#include <android/log.h>
#else
#include <cstdio>
#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_
# 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"])
)
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
```
#include <cstdio>
#include "mace/core/logging.h"
int main() {
printf("Hello world\n");
LOG(INFO, "Hello World");
return 0;
}
# -*- 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": [],
})
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册