# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import sys
import os
import time
from argparser import Parser

class LogCollector(object):

    def __init__(self, device, option):
        self.option = option
        self.strTime = time.strftime('%Y%m%d%H%M%S', time.localtime())
        self.logFolder = 'logs/'+device+'_'+self.strTime+'/'
        if not os.path.exists(self.logFolder):
            os.makedirs(self.logFolder)
        pass
        ## TODO: add timer for pull logs regularly

    def log_b2g_ps(self, attach):
        os.system('adb shell b2g-ps -t -p -P --oom > ' + self.logFolder + 'b2g-ps_' + attach + '.log')

    def log_b2g_info(self, attach):
        os.system('adb shell b2g-info -t > ' + self.logFolder + 'b2g-info_' + attach + '.log')

    def log_b2g_proprank(self, attach):
        os.system('adb shell b2g-procrank --oom > ' + self.logFolder + 'b2g-procrank_' + attach + '.log')

    def log_dumpstate(self, attach):
        os.system('adb shell dumpstate > ' + self.logFolder + 'dumpstate_' + attach + '.log')

    def log_crash_report(self):
        os.system('adb pull /data/b2g/mozilla/Crash\ Reports ' + self.logFolder)

    def log_logcat(self, attach):
        os.system('adb shell logcat -v threadtime -d > ' + self.logFolder + 'logcat_' + attach + '.log')

    def log_dmesg(self, attach):
        os.system('adb shell dmesg > ' + self.logFolder + 'dmesg_' + attach + '.log')

    def log_get_event(self, attach):
        os.system('adb shell getevent -S > ' + self.logFolder + 'getevent_' + attach + '.log')

    def getLogs(self):
        curTime = time.strftime('%m%d%H%M%S', time.localtime())
        if self.option['b2g-ps']:
            self.log_b2g_ps(curTime)
        if self.option['b2g-info']:
            self.log_b2g_info(curTime)
        if self.option['b2g-procrank']:
            self.log_b2g_proprank(curTime)
        if self.option['dumpstate']:
            self.log_dumpstate(curTime)
        if self.option['crash-report']:
            self.log_crash_report()
        if self.option['logcat']:
            self.log_logcat(curTime)
        if self.option['dmesg']:
            self.log_dmesg(curTime)
        if self.option['get-event']:
            self.log_get_event(curTime)

if __name__ == '__main__':
    if len(sys.argv) > 1:
        options = Parser.parser(sys.argv[1:])
        if 'config' in options:
            config = {}
            with open(options.config) as f:
                config = eval(f.read())
            log = LogCollector(config['device_name'], config['logs'])
            log.getLogs()

@@ -749,20 +749,8 @@
 er +
- 'b2g-ps_' +
  att
@@ -748,32 +748,39 @@
 der + attach + '
+/b2g-ps
 .log')%0A%0A    def 
@@ -874,22 +874,8 @@
 er +
- 'b2g-info_' +
  att
@@ -873,32 +873,41 @@
 der + attach + '
+/b2g-info
 .log')%0A%0A    def 
@@ -1009,17 +1009,27 @@
 older + 
-'
+attach + '/
 b2g-proc
@@ -1032,31 +1032,16 @@
 procrank
-_' + attach + '
 .log')%0A%0A
@@ -1143,152 +1143,33 @@
 r + 
-'dumpstate_' + attach + '.log')%0A%0A    def log_crash_report(self):%0A        os.system('adb pull /data/b2g/mozilla/Crash%5C Reports ' + self.logFolder
+attach + '/dumpstate.log'
 )%0A%0A 
@@ -1280,20 +1280,8 @@
 er +
- 'logcat_' +
  att
@@ -1279,32 +1279,39 @@
 der + attach + '
+/logcat
 .log')%0A%0A    def 
@@ -1396,19 +1396,8 @@
 er +
- 'dmesg_' +
  att
@@ -1395,32 +1395,38 @@
 der + attach + '
+/dmesg
 .log')%0A%0A    def 
@@ -1522,37 +1522,146 @@
 r + 
-'getevent_' + attach + '.log'
+attach + '/getevent.log')%0A%0A    def log_crash_report(self):%0A        os.system('adb pull /data/b2g/mozilla/Crash%5C Reports ' + self.logFolder
 )%0A%0A 
@@ -1738,32 +1738,76 @@
 me.localtime())%0A
+        os.makedirs(self.logFolder+curTime)%0A
         if self.
@@ -2100,84 +2100,8 @@
 me)%0A
-        if self.option%5B'crash-report'%5D:%0A            self.log_crash_report()%0A
     
@@ -2312,16 +2312,580 @@
 curTime)
+%0A        if self.option%5B'crash-report'%5D:%0A            self.log_crash_report()%0A%0A    def gen_report(self):%0A        report = open(self.logFolder+'report.html', 'w')%0A        for dir_path, dir_names, dir_files in os.walk(self.logFolder):%0A            for f in dir_files:%0A                if dir_names:%0A                    report.write('File: %3Ca href=%22%25s%22%3E%25s%3C/a%3E%3Cbr%3E%5Cn' %25 (f, f))%0A                else:%0A                    base = os.path.basename(dir_path)%0A                    report.write('File: %3Ca href=%22%25s/%25s%22%3E%25s/%25s%3C/a%3E%3Cbr%3E%5Cn' %25 (base, f, base, f))%0A        report.close()
 %0A%0Aif __n
@@ -3213,8 +3213,37 @@
 tLogs()%0A
+            log.gen_report()%0A

add gen_report