Skip to content

Commit 3f6f3bd

Browse files
authored
Add --debug flag(s) to launch.py (#3539)
1 parent 8ebde40 commit 3f6f3bd

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

src/classes/info.py

+4
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@
8888
# Data-model debugging enabler
8989
MODEL_TEST = False
9090

91+
# Default/initial logging levels
92+
LOG_LEVEL_FILE = 'INFO'
93+
LOG_LEVEL_CONSOLE = 'INFO'
94+
9195
# Languages
9296
CMDLINE_LANGUAGE = None
9397
CURRENT_LANGUAGE = 'en_US'

src/classes/logger.py

+39-23
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
1-
"""
1+
"""
22
@file
33
@brief This file sets the default logging settings
44
@author Noah Figg <[email protected]>
55
@author Jonathan Thomas <[email protected]>
6-
6+
77
@section LICENSE
8-
8+
99
Copyright (c) 2008-2018 OpenShot Studios, LLC
1010
(http://www.openshotstudios.com). This file is part of
1111
OpenShot Video Editor (http://www.openshot.org), an open-source project
1212
dedicated to delivering high quality video editing and animation solutions
1313
to the world.
14-
14+
1515
OpenShot Video Editor is free software: you can redistribute it and/or modify
1616
it under the terms of the GNU General Public License as published by
1717
the Free Software Foundation, either version 3 of the License, or
1818
(at your option) any later version.
19-
19+
2020
OpenShot Video Editor is distributed in the hope that it will be useful,
2121
but WITHOUT ANY WARRANTY; without even the implied warranty of
2222
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2323
GNU General Public License for more details.
24-
24+
2525
You should have received a copy of the GNU General Public License
2626
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
2727
"""
2828

29-
import logging
3029
import os, sys
31-
from logging.handlers import RotatingFileHandler
30+
import logging
31+
import logging.handlers
32+
3233
from classes import info
3334

35+
# Dictionary of logging handlers we create, keyed by type
36+
handlers = {}
37+
3438

3539
class StreamToLogger(object):
3640
"""Custom class to log all stdout and stderr streams (from libopenshot / and other libraries)"""
@@ -49,30 +53,42 @@ def flush(self):
4953
def errors(self):
5054
pass
5155

52-
# Initialize logging module, give basic formats and level we want to report
53-
logging.basicConfig(format="%(module)12s:%(levelname)s %(message)s",
54-
datefmt='%H:%M:%S',
55-
level=logging.INFO)
5656

57-
# Create a formatter
58-
formatter = logging.Formatter('%(module)12s:%(levelname)s %(message)s')
57+
# Create logger instance
58+
log = logging.Logger('OpenShot')
5959

60-
# Get logger instance & set level
61-
log = logging.getLogger('OpenShot')
62-
log.setLevel(logging.INFO)
60+
# Set up a log formatter
61+
formatter = logging.Formatter('%(module)12s:%(levelname)s %(message)s', datefmt='%H:%M:%S')
6362

64-
# Add rotation file handler
65-
fh = RotatingFileHandler(
63+
# Add normal stderr stream handler
64+
sh = logging.StreamHandler()
65+
sh.setFormatter(formatter)
66+
sh.setLevel(info.LOG_LEVEL_CONSOLE)
67+
log.addHandler(sh)
68+
handlers['stream'] = sh
69+
70+
# Add rotating file handler
71+
fh = logging.handlers.RotatingFileHandler(
6672
os.path.join(info.USER_PATH, 'openshot-qt.log'), encoding="utf-8", maxBytes=25*1024*1024, backupCount=3)
6773
fh.setFormatter(formatter)
74+
fh.setLevel(info.LOG_LEVEL_FILE)
6875
log.addHandler(fh)
76+
handlers['file'] = fh
77+
6978

7079
def reroute_output():
7180
"""Route stdout and stderr to logger (custom handler)"""
7281
if not getattr(sys, 'frozen', False):
73-
so = StreamToLogger(log, logging.INFO)
74-
sys.stdout = so
82+
handlers['stdout'] = StreamToLogger(log, logging.INFO)
83+
sys.stdout = handlers['stdout']
84+
85+
handlers['stderr'] = StreamToLogger(log, logging.ERROR)
86+
sys.stderr = handlers['stderr']
87+
88+
89+
def set_level_file(level=logging.INFO):
90+
handlers['file'].setLevel(level)
7591

76-
se = StreamToLogger(log, logging.ERROR)
77-
sys.stderr = se
7892

93+
def set_level_console(level=logging.INFO):
94+
handlers['stream'].setLevel(level)

src/launch.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@
4646

4747
try:
4848
from classes import info
49-
print("Loaded modules from current directory: %s" % info.PATH)
5049
except ImportError:
5150
import openshot_qt
5251
sys.path.append(openshot_qt.OPENSHOT_PATH)
5352
from classes import info
54-
print("Loaded modules from installed directory: %s" % info.PATH)
5553

5654

5755
def main():
@@ -71,6 +69,12 @@ def main():
7169
action='store_true',
7270
help="Load Qt's QAbstractItemModelTester into data models "
7371
'(requires Qt 5.11+)')
72+
parser.add_argument('-d', '--debug', action='store_true',
73+
help='Enable debugging output')
74+
parser.add_argument('--debug-file', action='store_true',
75+
help='Debugging output (logfile only)')
76+
parser.add_argument('--debug-console', action='store_true',
77+
help='Debugging output (console only)')
7478
parser.add_argument('-V', '--version', action='store_true')
7579
parser.add_argument('remain', nargs=argparse.REMAINDER,
7680
help=argparse.SUPPRESS)
@@ -82,6 +86,12 @@ def main():
8286
print("OpenShot version %s" % info.SETUP['version'])
8387
sys.exit()
8488

89+
# Set up debugging log level to requested streams
90+
if args.debug or args.debug_file:
91+
info.LOG_LEVEL_FILE = 'DEBUG'
92+
if args.debug or args.debug_console:
93+
info.LOG_LEVEL_CONSOLE = 'DEBUG'
94+
8595
if args.list_languages:
8696
from classes.language import get_all_languages
8797
print("Supported Languages:")
@@ -114,6 +124,9 @@ def main():
114124
print("Unsupported language '{}'! (See --list-languages)".format(args.lang))
115125
sys.exit(-1)
116126

127+
# Normal startup, print module path and lauch application
128+
print("Loaded modules from: %s" % info.PATH)
129+
117130
# Create Qt application, pass any unprocessed arguments
118131
from classes.app import OpenShotApp
119132

0 commit comments

Comments
 (0)