1#
2#
3# Copyright (C) 2012 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17#
18# Script that turns on useful logging for flimflam
19
20FLIMFLAM="org.chromium.flimflam"
21MANAGER="${FLIMFLAM}.Manager"
22SEND_FF_CMD="dbus-send --system --dest=${FLIMFLAM} --print-reply / $MANAGER"
23
24FF_TAGLIST=`${SEND_FF_CMD}.ListDebugTags | sed -e '/string/!d; s/[[:space:]]\+/ /g' | cut -d "\"" -f 2 | tr "+" " "`
25
26usage(){
27  echo "
28Usage: ff_debug [<tag_expression>]|[--reset]|[--help]|[--list_valid_tags]|[--level <level>]
29
30  ff_debug adds and removes debug tags for flimflam.
31  Current debug settings are displayed if no parameters are provided
32
33  <tag_expression> is defined in boolean notation using <debug_tag> separated
34    by an operator [+-], where + and - imply adding and removing of the tag immediately
35    following the operator. An expression beginning with either operators [+-]
36    takes the existing tags and modifies them appropriately.  Otherwise, the existing tags
37    are replaced by those specified in the command.
38
39    <debug_tag> can be listed using the --list_valid_tags
40
41    e.g.: ff_debug network+wifi
42      Sets debug tags to network and wifi
43    e.g.: ff_debug +network-service
44      Adds network and removes service tags from the existing debug settings
45
46  --list_valid_tags : Displays all valid tags
47
48  --level: Displays or sets current debug level for logging
49    All messages at, or above, the current log level are logged. Normal log
50    levels range from 4 (LOG_FATAL) to 0 (LOG_INFO). In addition VERBOSE log
51    levels are available starting at -1.
52
53    e.g.: ff_debug --level 4
54      Logs only FATAL messages.
55    e.g.: ff_debug --level 0
56      Logs INFO, WARNING, ERROR, ERROR_REPORT, and FATAL messages.
57    e.g.: ff_debug --level -4
58      Logs everything that "--level 0" does, plus SLOG(<tag>, <n>) messages,
59      where <tag> has been enabled for logging, and <n> <= 4. (NOTE: you must
60      negate SLOG levels for use with ff_debug. In this example, SLOG(<tag>, 4)
61      maps to "--level -4".)
62
63  --reset : Removes all tagging
64
65  --help : Displays this output
66"
67}
68
69get_ff_debug_tags() {
70  ${SEND_FF_CMD}.GetDebugTags 2>/dev/null | sed -e '/string/!d; s/[[:space:]]\+/ /g' | cut -d "\"" -f 2
71}
72
73set_ff_debug_tags() {
74  ${SEND_FF_CMD}.SetDebugTags string:"$1" > /dev/null 2>&1
75}
76
77get_ff_debug_level() {
78  ${SEND_FF_CMD}.GetDebugLevel 2>/dev/null | sed -e '/int32/!d; s/[[:space:]]\+/ /g' | cut -d " " -f 3
79}
80
81set_ff_debug_level() {
82  ${SEND_FF_CMD}.SetDebugLevel int32:"$1" > /dev/null 2>&1
83}
84
85starting_ff_tags=`get_ff_debug_tags`
86# Global strings
87ff_tags_to_set=$starting_ff_tags
88
89# Returns whether or not $2 exists in $1 where $1 is a space
90# separated list of tags
91is_valid_tag(){
92  expr " $1 " : ".* $2 .*"> /dev/null
93}
94
95# Takes a boolean expression and changes
96# flimflam string appropriately
97modify_flimflam_tag_string(){
98  if [ -n "$1" ]; then
99    for tag in `echo "$1" | sed 's/[+-]/ &/g'`; do
100
101      # Skip the tag if it's not in the list of valid tags
102      if ! is_valid_tag "$FF_TAGLIST" "`echo $tag | sed 's/^[+-]//'`"; then
103        continue
104      fi
105
106      case $tag in
107        -*)
108          tag=`echo $tag | sed 's/-//'`
109          if [ -n "$tag" ]; then
110            # First check/remove instances where it lies between +'s
111            # Then check/remove instances where it lies at the ends
112            ff_tags_to_set=`echo $ff_tags_to_set \
113                            | sed 's/+'$tag'+/+/g' \
114                            | sed 's/\(^\|+\)'$tag'\(+\|$\)//g'`
115          fi
116          ;;
117        +*)
118          tag=`echo $tag | sed 's/+//'`
119          if [ -z "$tag" ]; then
120            continue
121          # Check if exact tag is between + symbols, or at the ends of expression
122          elif [ -n "`echo "$ff_tags_to_set" | egrep "\+$tag\+|\+$tag$|^$tag\+"`" ]; then
123            continue
124          elif [ -n "$ff_tags_to_set" ]; then
125            ff_tags_to_set="${ff_tags_to_set}+${tag}"
126          else
127            ff_tags_to_set="$tag"
128          fi
129          ;;
130        *)
131          ff_tags_to_set="$tag"
132          ;;
133      esac
134    done
135    ff_tags_to_set=`echo "$ff_tags_to_set" | sed 's/^+//'`
136  else
137    ff_tags_to_set=""
138  fi
139}
140
141get_or_set_debug_level() {
142  local ff_debug_level=`get_ff_debug_level`
143  if [ -z "$ff_debug_level" ]; then
144    # flimflam does not implement GetDebugLevel / SetDebugLevel, simply return
145    return
146  fi
147
148  if [ $# -gt 0 ]; then
149    echo "Old flimflam debug level: $ff_debug_level"
150    set_ff_debug_level "$1"
151    ff_debug_level=`get_ff_debug_level`
152  fi
153  echo "Current flimflam debug level: $ff_debug_level"
154}
155
156if [ $# -gt 0 ]; then
157  while [ $# -gt 0 ]; do
158    case "$1" in
159    --reset)
160      ff_tags_to_set=""
161      break
162      ;;
163    --level)
164      shift  # move forward to the <level> argument if specified
165      get_or_set_debug_level "$@"
166      exit 0
167      ;;
168    --list*)
169      echo "Valid Tags: [`echo $FF_TAGLIST | sed 's/ /, /g'`]"
170      exit 0
171      ;;
172    --help|--*)
173      usage
174      exit 0
175      ;;
176    *)
177      modify_flimflam_tag_string "$1"
178      ;;
179    esac
180    shift
181  done
182
183  # set tags only when the starting and ending are different
184  if [ "$ff_tags_to_set" != "$starting_ff_tags" ]; then
185    set_ff_debug_tags "$ff_tags_to_set"
186  fi
187  echo "Old flimflam tags: [$starting_ff_tags]"
188fi
189echo "Current flimflam tags: [`get_ff_debug_tags`]"
190