• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* log.c - Log to logcat.
2  *
3  * Copyright 2016 The Android Open Source Project
4 
5 USE_LOG(NEWTOY(log, "<1p:t:", TOYFLAG_USR|TOYFLAG_SBIN))
6 
7 config 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 
24 GLOBALS(
25   char *t, *p;
26 )
27 
log_main(void)28 void log_main(void)
29 {
30   android_LogPriority pri = ANDROID_LOG_INFO;
31   char *s = toybuf;
32   int i;
33 
34   if (TT.p) {
35     i = stridx("defisvw", tolower(*TT.p));
36     if (i==-1 || strlen(TT.p)!=1) error_exit("bad -p '%s'", TT.p);
37     pri = (android_LogPriority []){ANDROID_LOG_DEBUG, ANDROID_LOG_ERROR,
38       ANDROID_LOG_FATAL, ANDROID_LOG_INFO, ANDROID_LOG_SILENT,
39       ANDROID_LOG_VERBOSE, ANDROID_LOG_WARN}[i];
40   }
41   if (!TT.t) TT.t = "log";
42 
43   for (i = 0; toys.optargs[i]; i++) {
44     if (i) *s++ = ' ';
45     if ((s-toybuf)+strlen(toys.optargs[i])>=1024) {
46       memcpy(s, toys.optargs[i], 1024-(s-toybuf));
47       toybuf[1024] = 0;
48       error_msg("log cut at 1024 bytes");
49 
50       break;
51     }
52     s = stpcpy(s, toys.optargs[i]);
53   }
54 
55   __android_log_write(pri, TT.t, toybuf);
56 }
57