17
17
# along with this program; if not, write to the Free Software Foundation,
18
18
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
#
20
+ import logging
21
+ from logging import Logger
20
22
import threading
21
23
from threading import Thread
22
24
from subprocess import Popen , PIPE
26
28
27
29
class Scanner :
28
30
cfg : Configuration
31
+ log : Logger
29
32
30
33
def __init__ (self , cfg : Configuration ):
31
34
self .cfg = cfg
35
+ self .log = logging .getLogger (__name__ )
36
+ self ._setup_logger (self .log )
37
+
38
+ def _setup_logger (self , log : Logger ):
39
+ log .setLevel (logging .INFO )
40
+ handler = logging .StreamHandler ()
41
+ handler .terminator = ""
42
+ log .addHandler (handler )
32
43
33
44
def scan (self ):
34
45
process = self .execute_command ()
35
- output_thread = threading .Thread (target = self ._print_output , args = (process .stdout ,))
36
- error_thread = threading .Thread (target = self ._print_output , args = (process .stderr ,))
46
+ output_thread = threading .Thread (target = self ._log_output , args = (process .stdout ,))
47
+ error_thread = threading .Thread (target = self ._log_output , args = (process .stderr ,))
37
48
return self .process_output (output_thread , error_thread , process )
38
49
39
50
def process_output (self , output_thread : Thread , error_thread : Thread , process : Popen ) -> int :
@@ -45,16 +56,16 @@ def process_output(self, output_thread: Thread, error_thread: Thread, process: P
45
56
46
57
return process .returncode
47
58
59
+ def execute_command (self ) -> Popen :
60
+ cmd = self .compute_command ()
61
+ return Popen (cmd , stdout = PIPE , stderr = PIPE )
62
+
48
63
def compute_command (self ) -> list [str ]:
49
64
if not self .cfg .sonar_scanner_executable_path :
50
65
raise ValueError ("No executable path provided" )
51
66
return [self .cfg .sonar_scanner_executable_path ] + self .cfg .scan_arguments
52
67
53
- def execute_command (self ) -> Popen :
54
- cmd = self .compute_command ()
55
- return Popen (cmd , stdout = PIPE , stderr = PIPE )
56
-
57
- def _print_output (self , stream : list [bytes ]):
68
+ def _log_output (self , stream : list [bytes ]):
58
69
for line in stream :
59
- decoded_line = line .decode (' utf-8' )
60
- print (decoded_line , end = '' , flush = True )
70
+ decoded_line = line .decode (" utf-8" )
71
+ self . log . info (decoded_line )
0 commit comments