Saturday, September 22, 2012

MacOS USBProber script to clean logs.

MacOS' USBProber is a great tool to debug USB transactions; however, it's just full of information. The logs that it generates just have too much! I created a bash script that I use to remove timestamps and other key words.
Consider the following three entries:
 4733.082 [7] AppleUSBOHCI[0x425d800]::ControlPacketHandler(0:0): still more to come: todo=0x40
 4733.098 [7] IOUSBControllerV3(AppleUSBOHCI)[0x425d800]::RootHubTimerFired - PolicyMaker[0x45be400] powerState[4] _powerStateChangingTo[-1]
 4736.585 [7] IOUSBHIDDriver[0x474a800]::powerChangeDone from state (4) to state (4) _device name(Apple Internal Keyboard / Trackpad)

I wanted to remove anything that had "PolicyMaker" or "Trackpad", along with the timestamp. This resulted in the following script:
#!/bin/sh

#sui's script to clean out MacOS USB prober information
#densui.blogspot.com

#change this list to include more filters
removelist="PolicyMaker Trackpad"

#function that looks for things in the last
function listcontains() {
  line=$2
  a_list=( $1 )
  #echo ${#a_list[@]}
  a_length=${#a_list[@]};
 for (( i = 0; i < $a_length; ++i ))
 do
     a_word=${a_list[$i]}
     if [[ $2 == *"$a_word"* ]]
  then
   return 0
  fi
 done
  return 1
}

#remove the old file if it exists
rm -f $2
# the main loop
cat $1 | while read line; do 
    if listcontains "$removelist" "$line"; 
    then 
     #echo Y;
     HOLDER=1
    else 
     echo ${line#*.*' '} >> $2
     #echo N;
     #echo ""
    fi    
done
Running the script on a file with the lines from the top of the page, I will get a filtered output of:
[7] AppleUSBOHCI[0x425d800]::ControlPacketHandler(0:0): still more to come: todo=0x40
So, no timestamp, no trackpad and no hub information! :)

No comments:

Post a Comment