1# Copyright 2017 Christoph Reiter 2# 3# This program is free software; you can redistribute it and/or modify 4# it under the terms of the GNU General Public License as published by 5# the Free Software Foundation; either version 2 of the License, or 6# (at your option) any later version. 7 8import os 9import sys 10import platform 11import time 12 13import mutagen 14 15import quodlibet 16from quodlibet.util import logging 17from quodlibet.util.path import mkdir 18from quodlibet.util.dprint import print_exc, format_exception 19 20 21def format_dump_header(exc_info): 22 """Returns system information and the traceback 23 24 Args: 25 exc_info (tuple): sys.exc_info() result tuple 26 Returns: 27 str 28 """ 29 30 lines = [ 31 u"=== SYSTEM INFORMATION:" 32 u"", 33 u"Quod Libet %s" % quodlibet.get_build_description(), 34 u"Mutagen %s" % mutagen.version_string, 35 u"Python %s %s" % (sys.version, sys.platform), 36 u"Platform %s" % platform.platform(), 37 u"=== STACK TRACE", 38 u"", 39 ] 40 41 lines.extend(format_exception(*exc_info)) 42 lines.append(u"") 43 return os.linesep.join(lines) 44 45 46def format_dump_log(limit=75): 47 """Returns recent log entries. 48 49 Args: 50 limit (int): number of log entries to return 51 Returns: 52 str 53 """ 54 55 dump = [u"=== LOG:"] 56 dump.extend(logging.get_content(limit=limit)) 57 return os.linesep.join(dump) 58 59 60def dump_to_disk(dump_dir, exc_info): 61 """Writes a new error log file into 'dump_dir' 62 63 Args: 64 dump_dir (path-like) 65 exc_info (tuple): sys.exc_info() result tuple 66 """ 67 68 try: 69 mkdir(dump_dir) 70 except EnvironmentError: 71 print_exc() 72 return 73 74 time_ = time.localtime() 75 dump_path = os.path.join( 76 dump_dir, time.strftime("Dump_%Y%m%d_%H%M%S.txt", time_)) 77 78 header = format_dump_header(exc_info).encode("utf-8") 79 log = format_dump_log().encode("utf-8") 80 81 try: 82 with open(dump_path, "wb") as dump: 83 dump.write(header) 84 dump.write(log) 85 except EnvironmentError: 86 print_exc() 87