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 | |
mukesh agrawal | 3484736 | 2013-02-06 13:25:02 -0800 | [diff] [blame] | 30 | e.g.: ff_debug network+wifi |
Darin Petkov | 2a0614e | 2012-10-22 15:28:41 +0200 | [diff] [blame] | 31 | Sets debug tags to network and wifi |
mukesh agrawal | 3484736 | 2013-02-06 13:25:02 -0800 | [diff] [blame] | 32 | e.g.: ff_debug +network-service |
Darin Petkov | 2a0614e | 2012-10-22 15:28:41 +0200 | [diff] [blame] | 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 |
mukesh agrawal | 3484736 | 2013-02-06 13:25:02 -0800 | [diff] [blame] | 38 | All messages at, or above, the current log level are logged. Normal log |
| 39 | levels range from 4 (LOG_FATAL) to 0 (LOG_INFO). In addition VERBOSE log |
| 40 | levels are available starting at -1. |
| 41 | |
| 42 | e.g.: ff_debug --level 4 |
| 43 | Logs only FATAL messages. |
| 44 | e.g.: ff_debug --level 0 |
| 45 | Logs INFO, WARNING, ERROR, ERROR_REPORT, and FATAL messages. |
| 46 | e.g.: ff_debug --level -4 |
| 47 | Logs everything that "--level 0" does, plus SLOG(<tag>, <n>) messages, |
| 48 | where <tag> has been enabled for logging, and <n> <= 4. (NOTE: you must |
| 49 | negate SLOG levels for use with ff_debug. In this example, SLOG(<tag>, 4) |
| 50 | maps to "--level -4".) |
Darin Petkov | 2a0614e | 2012-10-22 15:28:41 +0200 | [diff] [blame] | 51 | |
| 52 | --reset : Removes all tagging |
| 53 | |
| 54 | --help : Displays this output |
| 55 | " |
| 56 | } |
| 57 | |
| 58 | get_ff_debug_tags() { |
| 59 | ${SEND_FF_CMD}.GetDebugTags 2>/dev/null | awk '/string/ {print substr($2,2,length($2)-2);}' |
| 60 | } |
| 61 | |
| 62 | set_ff_debug_tags() { |
| 63 | ${SEND_FF_CMD}.SetDebugTags string:"$1" >& /dev/null |
| 64 | } |
| 65 | |
| 66 | get_ff_debug_level() { |
| 67 | ${SEND_FF_CMD}.GetDebugLevel 2>/dev/null | awk '/int32/ {print $2}' |
| 68 | } |
| 69 | |
| 70 | set_ff_debug_level() { |
| 71 | ${SEND_FF_CMD}.SetDebugLevel int32:"$1" >& /dev/null |
| 72 | } |
| 73 | |
| 74 | is_different(){ |
| 75 | list1=`echo $1 | tr "[ +-]" "\n" | sort | tr "\n" " "` |
| 76 | list2=`echo $2 | tr "[ +-]" "\n" | sort | tr "\n" " "` |
| 77 | |
| 78 | [[$list == "$list2"]] |
| 79 | } |
| 80 | |
| 81 | starting_ff_tags=`get_ff_debug_tags` |
| 82 | # Global strings |
| 83 | ff_tags_to_set=$starting_ff_tags |
| 84 | |
| 85 | # Returns whether ot not $2 exists in $1 where $1 is a space |
| 86 | # separated list of tags |
| 87 | is_valid_tag(){ |
| 88 | expr match " $1 " ".* $2 .*" > /dev/null |
| 89 | } |
| 90 | |
| 91 | # Takes a boolean expression and changes |
| 92 | # flimflam string appropriately |
| 93 | modify_flimflam_tag_string(){ |
| 94 | if [ -n "$1" ]; then |
| 95 | for tag in `echo "$1" | sed 's/[+-]/ &/g'`; do |
| 96 | |
| 97 | # Skip the tag if it's not in the list of valid tags |
| 98 | if ! is_valid_tag "$FF_TAGLIST" "`echo $tag | sed 's/^[+-]//'`"; then |
| 99 | continue |
| 100 | fi |
| 101 | |
| 102 | case $tag in |
| 103 | -*) |
| 104 | tag=`echo $tag | sed 's/-//'` |
| 105 | if [ -n "$tag" ]; then |
| 106 | # First check/remove instances where it lies between +'s |
| 107 | # Then check/remove instances where it lies at the ends |
| 108 | ff_tags_to_set=`echo $ff_tags_to_set \ |
| 109 | | sed 's/+'$tag'+/+/g' \ |
| 110 | | sed 's/\(^\|+\)'$tag'\(+\|$\)//g'` |
| 111 | fi |
| 112 | ;; |
| 113 | +*) |
| 114 | tag=`echo $tag | sed 's/+//'` |
| 115 | if [ -z "$tag" ]; then |
| 116 | continue |
| 117 | # Check if exact tag is between + symbols, or at the ends of expression |
| 118 | elif [ -n "`echo "$ff_tags_to_set" | egrep "\+$tag\+|\+$tag$|^$tag\+"`" ]; then |
| 119 | continue |
| 120 | elif [ -n "$ff_tags_to_set" ]; then |
| 121 | ff_tags_to_set="${ff_tags_to_set}+${tag}" |
| 122 | else |
| 123 | ff_tags_to_set="$tag" |
| 124 | fi |
| 125 | ;; |
| 126 | *) |
| 127 | ff_tags_to_set="$tag" |
| 128 | ;; |
| 129 | esac |
| 130 | done |
| 131 | ff_tags_to_set=`echo "$ff_tags_to_set" | sed 's/^+//'` |
| 132 | else |
| 133 | ff_tags_to_set="" |
| 134 | fi |
| 135 | } |
| 136 | |
| 137 | get_or_set_debug_level() { |
| 138 | local ff_debug_level=`get_ff_debug_level` |
| 139 | if [ -z "$ff_debug_level" ]; then |
| 140 | # flimflam does not implement GetDebugLevel / SetDebugLevel, simply return |
| 141 | return |
| 142 | fi |
| 143 | |
| 144 | if [ $# -gt 0 ]; then |
| 145 | echo "Old flimflam debug level: $ff_debug_level" |
| 146 | set_ff_debug_level "$1" |
| 147 | ff_debug_level=`get_ff_debug_level` |
| 148 | fi |
| 149 | echo "Current flimflam debug level: $ff_debug_level" |
| 150 | } |
| 151 | |
| 152 | if [ $# -gt 0 ]; then |
| 153 | while [ $# -gt 0 ]; do |
| 154 | case "$1" in |
| 155 | --reset) |
| 156 | ff_tags_to_set="" |
| 157 | break |
| 158 | ;; |
| 159 | --level) |
| 160 | shift # move forward to the <level> argument if specified |
| 161 | get_or_set_debug_level "$@" |
| 162 | exit 0 |
| 163 | ;; |
| 164 | --list*) |
| 165 | echo "Valid Tags: [`echo $FF_TAGLIST | sed 's/ /, /g'`]" |
| 166 | exit 0 |
| 167 | ;; |
| 168 | --help|--*) |
| 169 | usage |
| 170 | exit 0 |
| 171 | ;; |
| 172 | *) |
| 173 | modify_flimflam_tag_string "$1" |
| 174 | ;; |
| 175 | esac |
| 176 | shift |
| 177 | done |
| 178 | |
| 179 | # set tags only when the starting and ending are different |
| 180 | if [ "$ff_tags_to_set" != "$starting_ff_tags" ]; then |
| 181 | set_ff_debug_tags "$ff_tags_to_set" |
| 182 | fi |
| 183 | echo "Old flimflam tags: [$starting_ff_tags]" |
| 184 | fi |
| 185 | echo "Current flimflam tags: [`get_ff_debug_tags`]" |