feature_logging.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2017-present The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Test debug logging.""" 6 7 import os 8 9 from test_framework.test_framework import BitcoinTestFramework 10 from test_framework.test_node import ErrorMatch 11 12 13 class LoggingTest(BitcoinTestFramework): 14 def set_test_params(self): 15 self.num_nodes = 1 16 self.setup_clean_chain = True 17 18 def relative_log_path(self, name): 19 return os.path.join(self.nodes[0].chain_path, name) 20 21 def run_test(self): 22 # test default log file name 23 default_log_path = self.relative_log_path("debug.log") 24 assert os.path.isfile(default_log_path) 25 26 # test alternative log file name in datadir 27 self.restart_node(0, ["-debuglogfile=foo.log"]) 28 assert os.path.isfile(self.relative_log_path("foo.log")) 29 30 # test alternative log file name outside datadir 31 tempname = os.path.join(self.options.tmpdir, "foo.log") 32 self.restart_node(0, [f"-debuglogfile={tempname}"]) 33 assert os.path.isfile(tempname) 34 35 # check that invalid log (relative) will cause error 36 invdir = self.relative_log_path("foo") 37 invalidname = os.path.join("foo", "foo.log") 38 self.stop_node(0) 39 exp_stderr = r"Error: Could not open debug log file \S+$" 40 self.nodes[0].assert_start_raises_init_error([f"-debuglogfile={invalidname}"], exp_stderr, match=ErrorMatch.FULL_REGEX) 41 assert not os.path.isfile(os.path.join(invdir, "foo.log")) 42 43 # check that invalid log (relative) works after path exists 44 self.stop_node(0) 45 os.mkdir(invdir) 46 self.start_node(0, [f"-debuglogfile={invalidname}"]) 47 assert os.path.isfile(os.path.join(invdir, "foo.log")) 48 49 # check that invalid log (absolute) will cause error 50 self.stop_node(0) 51 invdir = os.path.join(self.options.tmpdir, "foo") 52 invalidname = os.path.join(invdir, "foo.log") 53 self.nodes[0].assert_start_raises_init_error([f"-debuglogfile={invalidname}"], exp_stderr, match=ErrorMatch.FULL_REGEX) 54 assert not os.path.isfile(os.path.join(invdir, "foo.log")) 55 56 # check that invalid log (absolute) works after path exists 57 self.stop_node(0) 58 os.mkdir(invdir) 59 self.start_node(0, [f"-debuglogfile={invalidname}"]) 60 assert os.path.isfile(os.path.join(invdir, "foo.log")) 61 62 # check that -nodebuglogfile disables logging 63 self.stop_node(0) 64 os.unlink(default_log_path) 65 assert not os.path.isfile(default_log_path) 66 self.start_node(0, ["-nodebuglogfile"]) 67 assert not os.path.isfile(default_log_path) 68 69 # just sanity check no crash here 70 self.restart_node(0, [f"-debuglogfile={os.devnull}"]) 71 72 self.log.info("Test -debug and -debugexclude raise when invalid values are passed") 73 self.stop_node(0) 74 self.nodes[0].assert_start_raises_init_error( 75 extra_args=["-debug=abc"], 76 expected_msg="Error: Unsupported logging category -debug=abc.", 77 match=ErrorMatch.FULL_REGEX, 78 ) 79 self.nodes[0].assert_start_raises_init_error( 80 extra_args=["-debugexclude=abc"], 81 expected_msg="Error: Unsupported logging category -debugexclude=abc.", 82 match=ErrorMatch.FULL_REGEX, 83 ) 84 85 self.log.info("Test -loglevel raises when invalid values are passed") 86 self.nodes[0].assert_start_raises_init_error( 87 extra_args=["-loglevel=abc"], 88 expected_msg="Error: Unsupported global logging level -loglevel=abc. Valid values: info, debug, trace.", 89 match=ErrorMatch.FULL_REGEX, 90 ) 91 self.nodes[0].assert_start_raises_init_error( 92 extra_args=["-loglevel=net:abc"], 93 expected_msg="Error: Unsupported category-specific logging level -loglevel=net:abc.", 94 match=ErrorMatch.PARTIAL_REGEX, 95 ) 96 self.nodes[0].assert_start_raises_init_error( 97 extra_args=["-loglevel=net:info:abc"], 98 expected_msg="Error: Unsupported category-specific logging level -loglevel=net:info:abc.", 99 match=ErrorMatch.PARTIAL_REGEX, 100 ) 101 102 self.log.info("Test that -nodebug,-debug=0,-debug=none clear previously specified debug options") 103 disable_debug_options = [ 104 '-debug=0', 105 '-debug=none', 106 '-nodebug' 107 ] 108 109 for disable_debug_opt in disable_debug_options: 110 # Every category before disable_debug_opt will be ignored, including the invalid 'abc' 111 self.restart_node(0, ['-debug=http', '-debug=abc', disable_debug_opt, '-debug=rpc', '-debug=net']) 112 logging = self.nodes[0].logging() 113 assert not logging['http'] 114 assert 'abc' not in logging 115 assert logging['rpc'] 116 assert logging['net'] 117 118 if __name__ == '__main__': 119 LoggingTest(__file__).main()