1"""
2Test API logging.
3"""
4
5import re
6
7import lldb
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbtest import *
10
11
12class APILogTestCase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    NO_DEBUG_INFO_TESTCASE = True
17
18    def test_api_log(self):
19        """Test API logging"""
20        logfile = os.path.join(self.getBuildDir(), "api-log.txt")
21
22        def cleanup():
23            if os.path.exists(logfile):
24                os.unlink(logfile)
25
26        self.addTearDownHook(cleanup)
27        self.expect("log enable lldb api -f {}".format(logfile))
28
29        self.dbg.SetDefaultArchitecture(None)
30        self.dbg.GetScriptingLanguage(None)
31        target = self.dbg.CreateTarget(None)
32
33        print(logfile)
34        with open(logfile, 'r') as f:
35            log = f.read()
36
37        # Find the SBDebugger's address.
38        debugger_addr = re.findall(
39            r"lldb::SBDebugger::GetScriptingLanguage\([^)]*\) \(0x([0-9a-fA-F]+),",
40            log)
41
42        # Make sure we've found a match.
43        self.assertTrue(debugger_addr, log)
44
45        # Make sure the GetScriptingLanguage matches.
46        self.assertTrue(re.search(r'lldb::SBDebugger::GetScriptingLanguage\([^)]*\) \(0x{}, ""\)'.format(
47            debugger_addr[0]), log), log)
48
49        # Make sure the address matches.
50        self.assertTrue(re.search(r'lldb::SBDebugger::CreateTarget\([^)]*\) \(0x{}, ""\)'.format(
51            debugger_addr[0]), log), log)
52