From 3b051985b3c4787c8e323d41e2833c0278a13e20 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 21 Jun 2016 15:32:42 -0500 Subject: new Android toy: log --- toys/android/log.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 toys/android/log.c (limited to 'toys/android') diff --git a/toys/android/log.c b/toys/android/log.c new file mode 100644 index 00000000..a7a8370c --- /dev/null +++ b/toys/android/log.c @@ -0,0 +1,63 @@ +/* log.c - Log to logcat. + * + * Copyright 2016 The Android Open Source Project + +USE_LOG(NEWTOY(log, "<1p:t:", TOYFLAG_USR|TOYFLAG_SBIN)) + +config LOG + bool "log" + depends on TOYBOX_ON_ANDROID + default y + help + usage: log [-p PRI] [-t TAG] MESSAGE... + + Logs message to logcat. + + -p use the given priority instead of INFO: + d: DEBUG e: ERROR f: FATAL + i: INFO v: VERBOSE w: WARN + -t use the given tag instead of "log" +*/ + +#define FOR_log +#include "toys.h" + +#if defined(__ANDROID__) +#include +#endif + +GLOBALS( + char *tag; + char *pri; +) + +void log_main(void) +{ +#if defined(__ANDROID__) + android_LogPriority pri = ANDROID_LOG_INFO; + int i; + + if (TT.pri) { + if (strlen(TT.pri) != 1) TT.pri = "?"; + switch (tolower(*TT.pri)) { + case 'd': pri = ANDROID_LOG_DEBUG; break; + case 'e': pri = ANDROID_LOG_ERROR; break; + case 'f': pri = ANDROID_LOG_FATAL; break; + case 'i': pri = ANDROID_LOG_INFO; break; + case 's': pri = ANDROID_LOG_SILENT; break; + case 'v': pri = ANDROID_LOG_VERBOSE; break; + case 'w': pri = ANDROID_LOG_WARN; break; + case '*': pri = ANDROID_LOG_DEFAULT; break; + default: error_exit("bad -p '%s'", TT.pri); + } + } + if (!TT.tag) TT.tag = "log"; + + for (i = 0; toys.optargs[i]; i++) { + if (i > 0) xstrncat(toybuf, " ", sizeof(toybuf)); + xstrncat(toybuf, toys.optargs[i], sizeof(toybuf)); + } + + __android_log_write(pri, TT.tag, toybuf); +#endif +} -- cgit v1.2.3