1/* log.c - Log to logcat. 2 * 3 * Copyright 2016 The Android Open Source Project 4 5USE_LOG(NEWTOY(log, "<1p:t:", TOYFLAG_USR|TOYFLAG_SBIN)) 6 7config LOG 8 bool "log" 9 depends on TOYBOX_ON_ANDROID 10 default y 11 help 12 usage: log [-p PRI] [-t TAG] MESSAGE... 13 14 Logs message to logcat. 15 16 -p use the given priority instead of INFO: 17 d: DEBUG e: ERROR f: FATAL i: INFO v: VERBOSE w: WARN s: SILENT 18 -t use the given tag instead of "log" 19*/ 20 21#define FOR_log 22#include "toys.h" 23#include <android/log.h> 24 25GLOBALS( 26 char *tag; 27 char *pri; 28) 29 30void log_main(void) 31{ 32 android_LogPriority pri = ANDROID_LOG_INFO; 33 char *s = toybuf; 34 int i; 35 36 if (TT.pri) { 37 i = stridx("defisvw", tolower(*TT.pri)); 38 if (i==-1 || strlen(TT.pri)!=1) error_exit("bad -p '%s'", TT.pri); 39 pri = (android_LogPriority []){ANDROID_LOG_DEBUG, ANDROID_LOG_ERROR, 40 ANDROID_LOG_FATAL, ANDROID_LOG_INFO, ANDROID_LOG_SILENT, 41 ANDROID_LOG_VERBOSE, ANDROID_LOG_WARN}[i]; 42 } 43 if (!TT.tag) TT.tag = "log"; 44 45 for (i = 0; toys.optargs[i]; i++) { 46 if (i) *s++ = ' '; 47 if ((s-toybuf)+strlen(toys.optargs[i])>=1024) { 48 memcpy(s, toys.optargs[i], 1024-(s-toybuf)); 49 toybuf[1024] = 0; 50 error_msg("log cut at 1024 bytes"); 51 52 break; 53 } 54 s = stpcpy(s, toys.optargs[i]); 55 } 56 57 __android_log_write(pri, TT.tag, toybuf); 58} 59