Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # |
| 2 | # Collects debug information in order to create error report files. |
| 3 | # |
| 4 | # Copyright (C) 2013 Intel Corporation |
| 5 | # Author: Andreea Brandusa Proca <andreea.b.proca@intel.com> |
| 6 | # |
| 7 | # Licensed under the MIT license, see COPYING.MIT for details |
| 8 | |
| 9 | ERR_REPORT_DIR ?= "${LOG_DIR}/error-report" |
| 10 | |
| 11 | def errorreport_getdata(e): |
| 12 | logpath = e.data.getVar('ERR_REPORT_DIR', True) |
| 13 | datafile = os.path.join(logpath, "error-report.txt") |
| 14 | with open(datafile) as f: |
| 15 | data = f.read() |
| 16 | return data |
| 17 | |
| 18 | def errorreport_savedata(e, newdata, file): |
| 19 | import json |
| 20 | logpath = e.data.getVar('ERR_REPORT_DIR', True) |
| 21 | datafile = os.path.join(logpath, file) |
| 22 | with open(datafile, "w") as f: |
| 23 | json.dump(newdata, f, indent=4, sort_keys=True) |
| 24 | return datafile |
| 25 | |
| 26 | python errorreport_handler () { |
| 27 | import json |
| 28 | |
| 29 | logpath = e.data.getVar('ERR_REPORT_DIR', True) |
| 30 | datafile = os.path.join(logpath, "error-report.txt") |
| 31 | |
| 32 | if isinstance(e, bb.event.BuildStarted): |
| 33 | bb.utils.mkdirhier(logpath) |
| 34 | data = {} |
| 35 | machine = e.data.getVar("MACHINE", True) |
| 36 | data['machine'] = machine |
| 37 | data['build_sys'] = e.data.getVar("BUILD_SYS", True) |
| 38 | data['nativelsb'] = e.data.getVar("NATIVELSBSTRING", True) |
| 39 | data['distro'] = e.data.getVar("DISTRO", True) |
| 40 | data['target_sys'] = e.data.getVar("TARGET_SYS", True) |
| 41 | data['failures'] = [] |
| 42 | data['component'] = e.getPkgs()[0] |
| 43 | data['branch_commit'] = base_detect_branch(e.data) + ": " + base_detect_revision(e.data) |
| 44 | lock = bb.utils.lockfile(datafile + '.lock') |
| 45 | errorreport_savedata(e, data, "error-report.txt") |
| 46 | bb.utils.unlockfile(lock) |
| 47 | |
| 48 | elif isinstance(e, bb.build.TaskFailed): |
| 49 | task = e.task |
| 50 | taskdata={} |
| 51 | log = e.data.getVar('BB_LOGFILE', True) |
| 52 | taskdata['package'] = e.data.expand("${PF}") |
| 53 | taskdata['task'] = task |
| 54 | if log: |
| 55 | try: |
| 56 | logFile = open(log, 'r') |
| 57 | logdata = logFile.read().decode('utf-8') |
| 58 | logFile.close() |
| 59 | except: |
| 60 | logdata = "Unable to read log file" |
| 61 | |
| 62 | else: |
| 63 | logdata = "No Log" |
| 64 | |
| 65 | # server will refuse failures longer than param specified in project.settings.py |
| 66 | # MAX_UPLOAD_SIZE = "5242880" |
| 67 | # use lower value, because 650 chars can be spent in task, package, version |
| 68 | max_logdata_size = 5242000 |
| 69 | # upload last max_logdata_size characters |
| 70 | if len(logdata) > max_logdata_size: |
| 71 | logdata = "..." + logdata[-max_logdata_size:] |
| 72 | taskdata['log'] = logdata |
| 73 | lock = bb.utils.lockfile(datafile + '.lock') |
| 74 | jsondata = json.loads(errorreport_getdata(e)) |
| 75 | jsondata['failures'].append(taskdata) |
| 76 | errorreport_savedata(e, jsondata, "error-report.txt") |
| 77 | bb.utils.unlockfile(lock) |
| 78 | |
| 79 | elif isinstance(e, bb.event.BuildCompleted): |
| 80 | lock = bb.utils.lockfile(datafile + '.lock') |
| 81 | jsondata = json.loads(errorreport_getdata(e)) |
| 82 | bb.utils.unlockfile(lock) |
| 83 | failures = jsondata['failures'] |
| 84 | if(len(failures) > 0): |
| 85 | filename = "error_report_" + e.data.getVar("BUILDNAME", True)+".txt" |
| 86 | datafile = errorreport_savedata(e, jsondata, filename) |
| 87 | bb.note("The errors for this build are stored in %s\nYou can send the errors to a reports server by running:\n send-error-report %s [-s server]" % (datafile, datafile)) |
| 88 | bb.note("The contents of these logs will be posted in public if you use the above command with the default server. Please ensure you remove any identifying or proprietary information when prompted before sending.") |
| 89 | } |
| 90 | |
| 91 | addhandler errorreport_handler |
| 92 | errorreport_handler[eventmask] = "bb.event.BuildStarted bb.event.BuildCompleted bb.build.TaskFailed" |