aboutsummaryrefslogtreecommitdiff
path: root/toys/android/log.c
blob: 7da37b6ffa9176d3ccf70d4039bdf7bae3a0b328 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* 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  s: SILENT
    -t	use the given tag instead of "log"
*/

#define FOR_log
#include "toys.h"
#include <android/log.h>

GLOBALS(
  char *tag;
  char *pri;
)

void log_main(void)
{
  android_LogPriority pri = ANDROID_LOG_INFO;
  char *s = toybuf;
  int i;

  if (TT.pri) {
    i = stridx("defisvw", tolower(*TT.pri));
    if (i==-1 || strlen(TT.pri)!=1) error_exit("bad -p '%s'", TT.pri);
    pri = (android_LogPriority []){ANDROID_LOG_DEBUG, ANDROID_LOG_ERROR,
      ANDROID_LOG_FATAL, ANDROID_LOG_INFO, ANDROID_LOG_SILENT,
      ANDROID_LOG_VERBOSE, ANDROID_LOG_WARN}[i];
  }
  if (!TT.tag) TT.tag = "log";

  for (i = 0; toys.optargs[i]; i++) {
    if (i) *s++ = ' ';
    if ((s-toybuf)+strlen(toys.optargs[i])>=1024) {
      memcpy(s, toys.optargs[i], 1024-(s-toybuf));
      toybuf[1024] = 0;
      perror_msg("log cut at 1024 bytes");

      break;
    }
    s = stpcpy(s, toys.optargs[i]);
  }

  __android_log_write(pri, TT.tag, toybuf);
}