1
- """
1
+ """
2
2
@file
3
3
@brief This file sets the default logging settings
4
4
@author Noah Figg <[email protected] >
5
5
@author Jonathan Thomas <[email protected] >
6
-
6
+
7
7
@section LICENSE
8
-
8
+
9
9
Copyright (c) 2008-2018 OpenShot Studios, LLC
10
10
(http://www.openshotstudios.com). This file is part of
11
11
OpenShot Video Editor (http://www.openshot.org), an open-source project
12
12
dedicated to delivering high quality video editing and animation solutions
13
13
to the world.
14
-
14
+
15
15
OpenShot Video Editor is free software: you can redistribute it and/or modify
16
16
it under the terms of the GNU General Public License as published by
17
17
the Free Software Foundation, either version 3 of the License, or
18
18
(at your option) any later version.
19
-
19
+
20
20
OpenShot Video Editor is distributed in the hope that it will be useful,
21
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
22
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
23
GNU General Public License for more details.
24
-
24
+
25
25
You should have received a copy of the GNU General Public License
26
26
along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
27
27
"""
28
28
29
- import logging
30
29
import os , sys
31
- from logging .handlers import RotatingFileHandler
30
+ import logging
31
+ import logging .handlers
32
+
32
33
from classes import info
33
34
35
+ # Dictionary of logging handlers we create, keyed by type
36
+ handlers = {}
37
+
34
38
35
39
class StreamToLogger (object ):
36
40
"""Custom class to log all stdout and stderr streams (from libopenshot / and other libraries)"""
@@ -49,30 +53,42 @@ def flush(self):
49
53
def errors (self ):
50
54
pass
51
55
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 )
56
56
57
- # Create a formatter
58
- formatter = logging .Formatter ( '%(module)12s:%(levelname)s %(message)s ' )
57
+ # Create logger instance
58
+ log = logging .Logger ( 'OpenShot ' )
59
59
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' )
63
62
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 (
66
72
os .path .join (info .USER_PATH , 'openshot-qt.log' ), encoding = "utf-8" , maxBytes = 25 * 1024 * 1024 , backupCount = 3 )
67
73
fh .setFormatter (formatter )
74
+ fh .setLevel (info .LOG_LEVEL_FILE )
68
75
log .addHandler (fh )
76
+ handlers ['file' ] = fh
77
+
69
78
70
79
def reroute_output ():
71
80
"""Route stdout and stderr to logger (custom handler)"""
72
81
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 )
75
91
76
- se = StreamToLogger (log , logging .ERROR )
77
- sys .stderr = se
78
92
93
+ def set_level_console (level = logging .INFO ):
94
+ handlers ['stream' ].setLevel (level )
0 commit comments