Darin Petkov | 2a0614e | 2012-10-22 15:28:41 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | # |
| 7 | # Script that turns on useful logging for flimflam |
| 8 | |
| 9 | FLIMFLAM="org.chromium.flimflam" |
| 10 | MANAGER="${FLIMFLAM}.Manager" |
| 11 | SEND_FF_CMD="dbus-send --system --dest=${FLIMFLAM} --print-reply / $MANAGER" |
| 12 | |
| 13 | FF_TAGLIST=`${SEND_FF_CMD}.ListDebugTags | awk '/string/ {print substr($2, 2, length($2)-2);}' | tr "+" " "` |
| 14 | |
| 15 | usage(){ |
| 16 | echo " |
| 17 | Usage: ff_debug [<tag_expression>]|[--reset]|[--help]|[--list_valid_tags]|[--level <level>] |
| 18 | |
| 19 | ff_debug adds and removes debug tags for flimflam. |
| 20 | Current debug settings are displayed if no parameters are provided |
| 21 | |
| 22 | <tag_expression> is defined in boolean notation using <debug_tag> separated |
| 23 | by an operator [+-], where + and - imply adding and removing of the tag immediately |
| 24 | following the operator. An expression beginning with either operators [+-] |
| 25 | takes the existing tags and modifies them appropriately. Otherwise, the existing tags |
| 26 | are replaced by those specified in the command. |
| 27 | |
| 28 | <debug_tag> can be listed using the --list_valid_tags |
| 29 | |
| 30 | eg: ff_debug network+wifi |
| 31 | Sets debug tags to network and wifi |
| 32 | eg: ff_debug +network-service |
| 33 | Adds network and removes service tags from the existing debug settings |
| 34 | |
| 35 | --list_valid_tags : Displays all valid tags |
| 36 | |
| 37 | --level: Displays or sets current debug level for logging |
| 38 | |
| 39 | --reset : Removes all tagging |
| 40 | |
| 41 | --help : Displays this output |
| 42 | " |
| 43 | } |
| 44 | |
| 45 | get_ff_debug_tags() { |
| 46 | ${SEND_FF_CMD}.GetDebugTags 2>/dev/null | awk '/string/ {print substr($2,2,length($2)-2);}' |
| 47 | } |
| 48 | |
| 49 | set_ff_debug_tags() { |
| 50 | ${SEND_FF_CMD}.SetDebugTags string:"$1" >& /dev/null |
| 51 | } |
| 52 | |
| 53 | get_ff_debug_level() { |
| 54 | ${SEND_FF_CMD}.GetDebugLevel 2>/dev/null | awk '/int32/ {print $2}' |
| 55 | } |
| 56 | |
| 57 | set_ff_debug_level() { |
| 58 | ${SEND_FF_CMD}.SetDebugLevel int32:"$1" >& /dev/null |
| 59 | } |
| 60 | |
| 61 | is_different(){ |
| 62 | list1=`echo $1 | tr "[ +-]" "\n" | sort | tr "\n" " "` |
| 63 | list2=`echo $2 | tr "[ +-]" "\n" | sort | tr "\n" " "` |
| 64 | |
| 65 | [[$list == "$list2"]] |
| 66 | } |
| 67 | |
| 68 | starting_ff_tags=`get_ff_debug_tags` |
| 69 | # Global strings |
| 70 | ff_tags_to_set=$starting_ff_tags |
| 71 | |
| 72 | # Returns whether ot not $2 exists in $1 where $1 is a space |
| 73 | # separated list of tags |
| 74 | is_valid_tag(){ |
| 75 | expr match " $1 " ".* $2 .*" > /dev/null |
| 76 | } |
| 77 | |
| 78 | # Takes a boolean expression and changes |
| 79 | # flimflam string appropriately |
| 80 | modify_flimflam_tag_string(){ |
| 81 | if [ -n "$1" ]; then |
| 82 | for tag in `echo "$1" | sed 's/[+-]/ &/g'`; do |
| 83 | |
| 84 | # Skip the tag if it's not in the list of valid tags |
| 85 | if ! is_valid_tag "$FF_TAGLIST" "`echo $tag | sed 's/^[+-]//'`"; then |
| 86 | continue |
| 87 | fi |
| 88 | |
| 89 | case $tag in |
| 90 | -*) |
| 91 | tag=`echo $tag | sed 's/-//'` |
| 92 | if [ -n "$tag" ]; then |
| 93 | # First check/remove instances where it lies between +'s |
| 94 | # Then check/remove instances where it lies at the ends |
| 95 | ff_tags_to_set=`echo $ff_tags_to_set \ |
| 96 | | sed 's/+'$tag'+/+/g' \ |
| 97 | | sed 's/\(^\|+\)'$tag'\(+\|$\)//g'` |
| 98 | fi |
| 99 | ;; |
| 100 | +*) |
| 101 | tag=`echo $tag | sed 's/+//'` |
| 102 | if [ -z "$tag" ]; then |
| 103 | continue |
| 104 | # Check if exact tag is between + symbols, or at the ends of expression |
| 105 | elif [ -n "`echo "$ff_tags_to_set" | egrep "\+$tag\+|\+$tag$|^$tag\+"`" ]; then |
| 106 | continue |
| 107 | elif [ -n "$ff_tags_to_set" ]; then |
| 108 | ff_tags_to_set="${ff_tags_to_set}+${tag}" |
| 109 | else |
| 110 | ff_tags_to_set="$tag" |
| 111 | fi |
| 112 | ;; |
| 113 | *) |
| 114 | ff_tags_to_set="$tag" |
| 115 | ;; |
| 116 | esac |
| 117 | done |
| 118 | ff_tags_to_set=`echo "$ff_tags_to_set" | sed 's/^+//'` |
| 119 | else |
| 120 | ff_tags_to_set="" |
| 121 | fi |
| 122 | } |
| 123 | |
| 124 | get_or_set_debug_level() { |
| 125 | local ff_debug_level=`get_ff_debug_level` |
| 126 | if [ -z "$ff_debug_level" ]; then |
| 127 | # flimflam does not implement GetDebugLevel / SetDebugLevel, simply return |
| 128 | return |
| 129 | fi |
| 130 | |
| 131 | if [ $# -gt 0 ]; then |
| 132 | echo "Old flimflam debug level: $ff_debug_level" |
| 133 | set_ff_debug_level "$1" |
| 134 | ff_debug_level=`get_ff_debug_level` |
| 135 | fi |
| 136 | echo "Current flimflam debug level: $ff_debug_level" |
| 137 | } |
| 138 | |
| 139 | if [ $# -gt 0 ]; then |
| 140 | while [ $# -gt 0 ]; do |
| 141 | case "$1" in |
| 142 | --reset) |
| 143 | ff_tags_to_set="" |
| 144 | break |
| 145 | ;; |
| 146 | --level) |
| 147 | shift # move forward to the <level> argument if specified |
| 148 | get_or_set_debug_level "$@" |
| 149 | exit 0 |
| 150 | ;; |
| 151 | --list*) |
| 152 | echo "Valid Tags: [`echo $FF_TAGLIST | sed 's/ /, /g'`]" |
| 153 | exit 0 |
| 154 | ;; |
| 155 | --help|--*) |
| 156 | usage |
| 157 | exit 0 |
| 158 | ;; |
| 159 | *) |
| 160 | modify_flimflam_tag_string "$1" |
| 161 | ;; |
| 162 | esac |
| 163 | shift |
| 164 | done |
| 165 | |
| 166 | # set tags only when the starting and ending are different |
| 167 | if [ "$ff_tags_to_set" != "$starting_ff_tags" ]; then |
| 168 | set_ff_debug_tags "$ff_tags_to_set" |
| 169 | fi |
| 170 | echo "Old flimflam tags: [$starting_ff_tags]" |
| 171 | fi |
| 172 | echo "Current flimflam tags: [`get_ff_debug_tags`]" |