Android-自动抓取日志工具 v1.0
资源内容介绍
Android-自动抓取日志工具 v1.0 # -*- coding: utf-8 -*-# Author: jiangjialinimport sysimport timefrom screencap import screencapfrom resource.logger import *# Get python version,int.py_ver_info = sys.version_info.major# Call my logger.module = 'GET-LOGS'logfile = './log/'logger = MyLogger(module, logfile)class GetLogs: """ Get logs: 1. logcat 2. kernel 3. anr 4. tombstones 5. bugreport """ def __init__(self): self.localtime = time.strftime( '%Y%m%d%H%M%S', time.localtime(time.time())) self.local_path = sys.path[0] + "/log/" + self.localtime self.loglst = ['all', 'system', 'radio', 'events', 'main', 'crash'] if not os.path.exists(self.local_path): os.makedirs(self.local_path) def logcat(self, remote_path): """ See logcat --help option for '-b' """ for log_name in self.loglst: os.system('adb shell "rm {}/logcat-{}.log"'.format(remote_path, log_name)) cmd = 'adb shell "logcat -b {} -d > {}/logcat-{}.log"'.format( log_name, remote_path, log_name) logger.info('Getting <logcat-{}> log ......'.format(log_name)) # Save log to file (Overwrite if the file exists). os.system(cmd) def dmesg_log(self, remote_path): """ Get kernel log, and save to file. """ logger.info('Getting <Kernel> log ......') os.system('adb shell "dmesg > {}/Kernel.log"'.format(remote_path)) # from /data/vendor/kmsgd get kernel log # os.system('adb pull /data/vendor/kmsgd {}'.format(self.local_path)) def anr_log(self, local_path): """ Get ANR log,pull /data/anr all files to local. ANR: Application Not Responding must get it. """ logger.info('Getting for <ANR> log ......') cmd = 'adb pull /data/anr/ {}/'.format(local_path) os.system(cmd) def tombstones_log(self, local_path): """ Get tombstones log,pull /data/tombstones all files to local. """ logger.info('Getting for <tombstones> log ......') cmd = 'adb pull /data/tombstones/ {}/'.format(local_path) os.system(cmd) def misc_logd(self, local_path): """ Pull /data/misc/logd all files to local. """ logger.info('Getting for <misc_logd> log ......') cmd = 'adb pull /data/misc/logd {}/misc/'.format(local_path) os.system(cmd) def misc_bluetooth(self, local_path): ''' Get /data/misc/bluetooth all files to local. about bluetooth issue must get it. ''' logger.info('Getting for <misc_bluetooth> log ......') cmd = 'adb pull /data/misc/bluetooth {}/misc/'.format(local_path) os.system(cmd) def bugreport(self, local_path): """ Get bugreport log After the Android 7.x use "adb bugreport +path", export to zip. Previous versions were incompatible. """ logger.info('Getting <bugreport> log ......') # Export bugreport(zip). cmd = 'adb bugreport {}'.format(local_path) os.system(cmd) def take_custom_log(self): """ Get all files in the "/sdcard/awlog/" directory, mainly including custom logs. """ # make custom directory. custom_remote_path = '/sdcard/awlog' os.system('adb shell mkdir {}'.format(custom_remote_path)) logger.info('Getting "/sdcard/awlog/*" log ......') pull_cmd = 'adb pull {} {}/awlog'.format(custom_remote_path, self.local_path) print(pull_cmd) self.logcat(custom_remote_path) self.dmesg_log(custom_remote_path) # Pull log file to local. os.system(pull_cmd)def main(): """ Start get log. """ os.system('adb wait-for-device') os.system('adb remount') os.system('adb root') # Userdebug must use it. get = GetLogs() local_path = get.local_path # take picture. screencap().capture(local_path) get.take_custom_log() # Default don't get following logs,because it's already included in bugreport(zip). # get.anr_log() # get.tombstones_log() # get.misc_logd() # get.misc_bluetooth() # take bugreport. get.bugreport(local_path) logger.info('Has been saved to [%s]' % get.local_path)if __name__ == '__main__': main()