10f66f451Sopenharmony_ci/* log.c - Log to logcat.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2016 The Android Open Source Project
40f66f451Sopenharmony_ci
50f66f451Sopenharmony_ciUSE_LOG(NEWTOY(log, "<1p:t:", TOYFLAG_USR|TOYFLAG_SBIN))
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciconfig LOG
80f66f451Sopenharmony_ci  bool "log"
90f66f451Sopenharmony_ci  depends on TOYBOX_ON_ANDROID
100f66f451Sopenharmony_ci  default y
110f66f451Sopenharmony_ci  help
120f66f451Sopenharmony_ci    usage: log [-p PRI] [-t TAG] MESSAGE...
130f66f451Sopenharmony_ci
140f66f451Sopenharmony_ci    Logs message to logcat.
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci    -p	Use the given priority instead of INFO:
170f66f451Sopenharmony_ci    	d: DEBUG  e: ERROR  f: FATAL  i: INFO  v: VERBOSE  w: WARN  s: SILENT
180f66f451Sopenharmony_ci    -t	Use the given tag instead of "log"
190f66f451Sopenharmony_ci*/
200f66f451Sopenharmony_ci
210f66f451Sopenharmony_ci#define FOR_log
220f66f451Sopenharmony_ci#include "toys.h"
230f66f451Sopenharmony_ci
240f66f451Sopenharmony_ciGLOBALS(
250f66f451Sopenharmony_ci  char *t, *p;
260f66f451Sopenharmony_ci)
270f66f451Sopenharmony_ci
280f66f451Sopenharmony_civoid log_main(void)
290f66f451Sopenharmony_ci{
300f66f451Sopenharmony_ci  android_LogPriority pri = ANDROID_LOG_INFO;
310f66f451Sopenharmony_ci  char *s = toybuf;
320f66f451Sopenharmony_ci  int i;
330f66f451Sopenharmony_ci
340f66f451Sopenharmony_ci  if (TT.p) {
350f66f451Sopenharmony_ci    i = stridx("defisvw", tolower(*TT.p));
360f66f451Sopenharmony_ci    if (i==-1 || strlen(TT.p)!=1) error_exit("bad -p '%s'", TT.p);
370f66f451Sopenharmony_ci    pri = (android_LogPriority []){ANDROID_LOG_DEBUG, ANDROID_LOG_ERROR,
380f66f451Sopenharmony_ci      ANDROID_LOG_FATAL, ANDROID_LOG_INFO, ANDROID_LOG_SILENT,
390f66f451Sopenharmony_ci      ANDROID_LOG_VERBOSE, ANDROID_LOG_WARN}[i];
400f66f451Sopenharmony_ci  }
410f66f451Sopenharmony_ci  if (!TT.t) TT.t = "log";
420f66f451Sopenharmony_ci
430f66f451Sopenharmony_ci  for (i = 0; toys.optargs[i]; i++) {
440f66f451Sopenharmony_ci    if (i) *s++ = ' ';
450f66f451Sopenharmony_ci    if ((s-toybuf)+strlen(toys.optargs[i])>=1024) {
460f66f451Sopenharmony_ci      memcpy(s, toys.optargs[i], 1024-(s-toybuf));
470f66f451Sopenharmony_ci      toybuf[1024] = 0;
480f66f451Sopenharmony_ci      error_msg("log cut at 1024 bytes");
490f66f451Sopenharmony_ci
500f66f451Sopenharmony_ci      break;
510f66f451Sopenharmony_ci    }
520f66f451Sopenharmony_ci    s = stpcpy(s, toys.optargs[i]);
530f66f451Sopenharmony_ci  }
540f66f451Sopenharmony_ci
550f66f451Sopenharmony_ci  __android_log_write(pri, TT.t, toybuf);
560f66f451Sopenharmony_ci}
57