• 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, "p: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 (or stdin) 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   int pri;
28 )
29 
log_line(char ** pline,long len)30 static void log_line(char **pline, long len)
31 {
32   if (!pline) return;
33   __android_log_write(TT.pri, TT.t, *pline);
34 }
35 
log_main(void)36 void log_main(void)
37 {
38   char *s = toybuf;
39   int i;
40 
41   TT.pri = ANDROID_LOG_INFO;
42   if (TT.p) {
43     i = stridx("defisvw", tolower(*TT.p));
44     if (i==-1 || strlen(TT.p)!=1) error_exit("bad -p '%s'", TT.p);
45     TT.pri = (int[]) {ANDROID_LOG_DEBUG, ANDROID_LOG_ERROR,
46       ANDROID_LOG_FATAL, ANDROID_LOG_INFO, ANDROID_LOG_SILENT,
47       ANDROID_LOG_VERBOSE, ANDROID_LOG_WARN}[i];
48   }
49   if (!TT.t) TT.t = "log";
50 
51   if (toys.optc) {
52     for (i = 0; toys.optargs[i]; i++) {
53       if (i) *s++ = ' ';
54       if ((s-toybuf)+strlen(toys.optargs[i])>=1024) {
55         memcpy(s, toys.optargs[i], 1024-(s-toybuf));
56         toybuf[1024] = 0;
57         error_msg("log cut at 1024 bytes");
58         break;
59       }
60       s = stpcpy(s, toys.optargs[i]);
61     }
62   } else do_lines(0, '\n', log_line);
63 
64   __android_log_write(TT.pri, TT.t, toybuf);
65 }
66