summaryrefslogtreecommitdiffstats
path: root/logcat/logpersist
blob: f0e7d42e23767ee624eafb1479812ed6bce32e06 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#! /system/bin/sh
# logpersist cat, start and stop handlers
progname="${0##*/}"
case `getprop ro.debuggable` in
1) ;;
*) echo "${progname} - Permission denied"
   exit 1
   ;;
esac

property=persist.logd.logpersistd

case `getprop ${property#persist.}.enable` in
true) ;;
*) echo "${progname} - Disabled"
   exit 1
   ;;
esac

log_tag_property=persist.log.tag
data=/data/misc/logd
service=logcatd
size_default=256
buffer_default=all
args="${@}"

size=${size_default}
buffer=${buffer_default}
clear=false
while [ ${#} -gt 0 ]; do
  case ${1} in
    -c|--clear) clear=true ;;
    --size=*) size="${1#--size=}" ;;
    --rotate-count=*) size="${1#--rotate-count=}" ;;
    -n|--size|--rotate-count) size="${2}" ; shift ;;
    --buffer=*) buffer="${1#--buffer=}" ;;
    -b|--buffer) buffer="${2}" ; shift ;;
    -h|--help|*)
      LEAD_SPACE_="`echo ${progname%.*} | tr '[ -~]' ' '`"
      echo "${progname%.*}.cat             - dump current ${service} logs"
      echo "${progname%.*}.start [--size=<size_in_kb>] [--buffer=<buffers>] [--clear]"
      echo "${LEAD_SPACE_}                 - start ${service} service"
      echo "${progname%.*}.stop [--clear]  - stop ${service} service"
      case ${1} in
        -h|--help) exit 0 ;;
        *) echo ERROR: bad argument ${@} >&2 ; exit 1 ;;
      esac
      ;;
  esac
  shift
done

if [ -z "${size}" -o "${size_default}" = "${size}" ]; then
  unset size
fi
if [ -n "${size}" ] &&
  ! ( [ 0 -lt "${size}" ] && [ 2048 -ge "${size}" ] ) >/dev/null 2>&1; then
  echo ERROR: Invalid --size ${size} >&2
  exit 1
fi
if [ -z "${buffer}" -o "${buffer_default}" = "${buffer}" ]; then
  unset buffer
fi
if [ -n "${buffer}" ] && ! logcat -b ${buffer} -g >/dev/null 2>&1; then
  echo ERROR: Invalid --buffer ${buffer} >&2
  exit 1
fi

log_tag="`getprop ${log_tag_property}`"
logd_logpersistd="`getprop ${property}`"

case ${progname} in
*.cat)
  if [ -n "${size}${buffer}" -o "true" = "${clear}" ]; then
    echo WARNING: Can not use --clear, --size or --buffer with ${progname%.*}.cat >&2
  fi
  su logd ls "${data}" |
  tr -d '\r' |
  sort -ru |
  sed "s#^#${data}/#" |
  su logd xargs cat
  ;;
*.start)
  current_buffer="`getprop ${property#persist.}.buffer`"
  current_size="`getprop ${property#persist.}.size`"
  if [ "${service}" = "`getprop ${property#persist.}`" ]; then
    if [ "true" = "${clear}" ]; then
      setprop ${property#persist.} "clear"
    elif [ "${buffer}|${size}" != "${current_buffer}|${current_size}" ]; then
      echo   "ERROR: Changing existing collection parameters from" >&2
      if [ "${buffer}" != "${current_buffer}" ]; then
        a=${current_buffer}
        b=${buffer}
        if [ -z "${a}" ]; then a="${default_buffer}"; fi
        if [ -z "${b}" ]; then b="${default_buffer}"; fi
        echo "           --buffer ${a} to ${b}" >&2
      fi
      if [ "${size}" != "${current_size}" ]; then
        a=${current_size}
        b=${size}
        if [ -z "${a}" ]; then a="${default_size}"; fi
        if [ -z "${b}" ]; then b="${default_size}"; fi
        echo "           --size ${a} to ${b}" >&2
      fi
      echo   "       Are you sure you want to do this?" >&2
      echo   "       Suggest add --clear to erase data and restart with new settings." >&2
      echo   "       To blindly override and retain data, ${progname%.*}.stop first." >&2
      exit 1
    fi
  elif [ "true" = "${clear}" ]; then
    setprop ${property#persist.} "clear"
  fi
  if [ -n "${buffer}${current_buffer}" ]; then
    setprop ${property}.buffer "${buffer}"
    if [ -z "${buffer}" ]; then
      # deal with trampoline for empty properties
      setprop ${property#persist.}.buffer ""
    fi
  fi
  if [ -n "${size}${current_size}" ]; then
    setprop ${property}.size "${size}"
    if [ -z "${size}" ]; then
      # deal with trampoline for empty properties
      setprop ${property#persist.}.size ""
    fi
  fi
  while [ "clear" = "`getprop ${property#persist.}`" ]; do
    continue
  done
  # Tell Settings that we are back on again if we turned logging off
  tag="${log_tag#Settings}"
  if [ X"${log_tag}" != X"${tag}" ]; then
    echo "WARNING: enabling logd service" >&2
    setprop ${log_tag_property} "${tag#,}"
  fi
  # ${service}.rc does the heavy lifting with the following trigger
  setprop ${property} ${service}
  # 20ms done, to permit process feedback check
  sleep 1
  getprop ${property#persist.}
  # also generate an error return code if not found running
  pgrep -u ${data##*/} ${service%d}
  ;;
*.stop)
  if [ -n "${size}${buffer}" ]; then
    echo "WARNING: Can not use --size or --buffer with ${progname%.*}.stop" >&2
  fi
  if [ "true" = "${clear}" ]; then
    setprop ${property} "clear"
  else
    setprop ${property} "stop"
  fi
  if [ -n "`getprop ${property#persist.}.buffer`" ]; then
    setprop ${property}.buffer ""
    # deal with trampoline for empty properties
    setprop ${property#persist.}.buffer ""
  fi
  if [ -n "`getprop ${property#persist.}.size`" ]; then
    setprop ${property}.size ""
    # deal with trampoline for empty properties
    setprop ${property#persist.}.size ""
  fi
  while [ "clear" = "`getprop ${property#persist.}`" ]; do
    continue
  done
  ;;
*)
  echo "ERROR: Unexpected command ${0##*/} ${args}" >&2
  exit 1
esac

if [ X"${log_tag}" != X"`getprop ${log_tag_property}`" ] ||
   [ X"${logd_logpersistd}" != X"`getprop ${property}`" ]; then
  echo "WARNING: killing Settings" >&2
  am force-stop com.android.settings
fi