Skip to content

Instantly share code, notes, and snippets.

@mattak
Created November 9, 2012 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattak/4044162 to your computer and use it in GitHub Desktop.
Save mattak/4044162 to your computer and use it in GitHub Desktop.
Simplify android logcat.
#!/usr/bin/env ruby
#
# adb logcat をもっと見やすく.
#
require 'optparse'
#
# global option.
#
options = {
:loglevels => ['D'],
:tags => [],
:command => 'adb logcat -v tag',
:format => '[$tag]\n $msg',
:format_tag => "$tag",
:format_msg => "$msg",
:format_level => "$level",
:coloring => true,
:level_colors => {
'D' => 'blue',
'I' => 'cyan',
'W' => 'yellow',
'E' => 'red',
},
:system_tags => [
'AbsListView',
'ActivityManager',
'AlarmManagerService',
'AndroidRuntime',
'ApplicationPolicy',
'AudioHardwareALSA',
'AudioStreamOutALSA',
'AwesomePlayer',
'AlarmItem',
'AlarmWidget',
'AlarmReceiver',
'accuweather',
'alsa_ucm',
'ALSAModule',
'ANDROID_DRM_TEST',
'AlarmProvider',
'AudioPolicyManagerBase',
'AudioService',
'AccountTypeManager',
'AllShareClient',
'AllShareContext',
'AllShareSource',
'AvrcpProxy',
'AvrcpStubService',
"ApplicationUsage",
"AudioFlinger",
"AudioPlayer",
"AlarmManager",
"AudioTrack",
"AvrcpService",
'ACRA',
'BatteryService',
'BackupManagerService',
'BluetoothPolicyService',
'BluetoothUtils',
"BtOppRfcommListener",
'CircleLockScreen',
'CircleMissedEventWidget',
'CircleShortcutWidget',
'CircleUnlockRipple',
'CircleUnlockRippleRenderer',
'ClockWidget',
'ConnectivityService',
'comsamsunglog',
'comsamsungapp',
'CLIPBOARD',
'CapabilityManagerService',
'ContactProvider',
"ClipboardServiceEx",
"CpuGovernorService",
"chromium",
"DBG_WSS_DM",
"DownloadManager",
"DrmEventService",
'DEFERED_APP_VISIBILITY',
'DrmPVPlugIn',
'dalvikvm',
'DivXPlugin',
"DigestUtils",
'DEEPAK',
"EventLogService",
'EmojiDrawable',
'EMOJI_SL',
'FastDormancy',
'FirewallPolicy',
'Finsky',
'GpsLocationProvider',
"GlobalActions",
'Gmail',
'GLWebViewState',
'InputReader',
"InputDispatcher",
"installd",
"InputManagerService",
'jniAvrcpService',
'KeyguardViewManager',
'KeyguardUpdateMonitor',
'KeyguardViewMediator',
'LibQmg_native',
'LockPatternKeyguardView',
'LocationManagerService',
'lights',
'libEGL',
'MtpService',
'MTPJNIInterface',
'memalloc',
'MSC_Accu_Daemon',
'MobileDataStateTracker',
'MonitorSver',
"MotionRecognitionManager",
'NetworkConnectivityReceiver',
'NetworkManagementService',
"NetworkManagementSocketTagger",
'OMXCodec',
'OpenGLRenderer',
'PhoneStatusBar',
'PhoneNumberUtils',
'PointerInterceptView',
'PopupuiReceiver',
'PVPlayReadyReceiver',
'PowerManagerService',
'PackageManager',
"PCWCLIENTTRACE_LOG",
"PCWCLIENTTRACE_SPPReceiver",
"PictController",
"ProcessStats",
"paletteui",
'QMI_FW',
'jdwp',
"ResourceType",
'RestrictionPolicy',
"rmt_storage",
'Sensors',
'SurfaceFlinger',
'SystemClock',
'SensorManager',
'StatusChecker',
'SignalStrength',
'SMD',
"syncContacts",
"SamplingRateChangeProcessor",
"SqliteDatabaseCpp",
"SimpleChannelHandler",
"SurfaceView",
'ThermistorObserver',
'Tethering',
'UsbDeviceManager',
'UsbDetectingManager',
'UsbDeviceManager',
'VolumePanel',
'VoIPInterfaceManager',
"Vibrator",
"ViewRootImpl",
'VoldCmdListener',
'Volley',
'vclib',
'WallpaperWidget',
'WindowManager',
'WifiService',
'WifiP2pStateTracker',
'WML_SISO',
'webviewglue',
'webclipboard',
'webkit',
],
}
color_pallet = {
"black" => 30,
"red" => 31,
"yellow" => 33,
"blue" => 34,
"magenta" => 35,
"cyan" => 36,
"white" => 37,
}
#
# option parse
#
opt = OptionParser.new
opt.on('-l LEVELS',
"log level which is separated by comma. it should be V or D or I or W or E") do |v|
options[:loglevels] = v.upcase.split(',')
end
opt.on('-t TAGS', 'filtering tag') do |v|
options[:tags] = v.split(',')
end
opt.on('-f format', 'set specified format') do |v|
options[:format] = v
end
opt.on('--[no-]color', 'set coloring') do |v|
options[:coloring] = v
end
opt.parse!(ARGV)
#
# main
#
IO.popen(options[:command], 'r+') do |io|
io.each do |line|
line.chomp!
line.encode!("UTF-8", "UTF-8",
invalid: :replace,
undef: :replace,
replace: '.')
if %r{^(V|D|I|W|E)/(\w+)\s*:\s*(.+)} =~ line
level = $1
tag = $2
msg = $3
next if !options[:loglevels].include?(level)
next if !options[:tags].empty? && !options[:tags].include?(tag)
next if options[:system_tags].include?(tag)
# coloring
if options[:coloring] && options[:level_colors][level] != nil then
color_format = "\033[%dm%s\033[0m"
color = color_pallet[options[:level_colors][level]]
level = color_format % [color, level]
tag = color_format % [color, tag]
msg = color_format % [color, msg]
end
# formating
outstr = options[:format]
.gsub(options[:format_level], level)
.gsub(options[:format_tag], tag)
.gsub(options[:format_msg], msg)
.gsub('\n', "\n")
.gsub('\t', "\t")
puts outstr
end
end
end
__END__
@mattak
Copy link
Author

mattak commented Nov 30, 2012

TODO

  • write message tracking.
  • syntax test
  • add time format.
  • view html viewer mode.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment