xref: /llvm-project/lldb/test/API/commands/log/basic/TestLogging.py (revision 80fcecb13c388ff087a27a4b0e7ca3dd8c98eaa4)
1"""
2Test lldb logging.  This test just makes sure logging doesn't crash, and produces some output.
3"""
4
5
6import os
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class LogTestCase(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def setUp(self):
17        super(LogTestCase, self).setUp()
18        self.log_file = self.getBuildArtifact("log-file.txt")
19
20    def test_file_writing(self):
21        self.build()
22        exe = self.getBuildArtifact("a.out")
23        self.expect("file " + exe, patterns=["Current executable set to .*a.out"])
24
25        if os.path.exists(self.log_file):
26            os.remove(self.log_file)
27
28        # By default, Debugger::EnableLog() will set log options to
29        # PREPEND_THREAD_NAME + OPTION_THREADSAFE. We don't want the
30        # threadnames here, so we enable just threadsafe (-t).
31        self.runCmd("log enable -f '%s' lldb commands" % (self.log_file))
32
33        self.runCmd("command alias bp breakpoint")
34
35        self.runCmd("bp set -n main")
36
37        self.runCmd("bp l")
38
39        self.runCmd("log disable lldb")
40
41        self.assertTrue(os.path.isfile(self.log_file))
42
43        with open(self.log_file, "r") as f:
44            log_lines = f.read()
45        os.remove(self.log_file)
46
47        self.assertGreater(len(log_lines), 0, "Something was written to the log file.")
48
49    # Check that lldb truncates its log files
50    def test_log_truncate(self):
51        # put something in our log file
52        with open(self.log_file, "w") as f:
53            for i in range(1, 1000):
54                f.write("bacon\n")
55
56        self.runCmd("log enable -f '%s' lldb commands" % self.log_file)
57        self.runCmd("help log")
58        self.runCmd("log disable lldb")
59
60        self.assertTrue(os.path.isfile(self.log_file))
61        with open(self.log_file, "r") as f:
62            contents = f.read()
63
64        # check that it got removed
65        self.assertEqual(contents.find("bacon"), -1)
66
67    # Check that lldb can append to a log file
68    def test_log_append(self):
69        # put something in our log file
70        with open(self.log_file, "w") as f:
71            f.write("bacon\n")
72
73        self.runCmd("log enable -a -f '%s' lldb commands" % self.log_file)
74        self.runCmd("help log")
75        self.runCmd("log disable lldb")
76
77        self.assertTrue(os.path.isfile(self.log_file))
78        with open(self.log_file, "r") as f:
79            contents = f.read()
80
81        # check that it is still there
82        self.assertEqual(contents.find("bacon"), 0)
83
84    # Enable all log options and check that nothing crashes.
85    @skipIfWindows
86    def test_all_log_options(self):
87        if os.path.exists(self.log_file):
88            os.remove(self.log_file)
89
90        self.runCmd(
91            "log enable -v -s -T -p -n -S -F -f '%s' lldb commands" % self.log_file
92        )
93        self.runCmd("help log")
94        self.runCmd("log disable lldb")
95
96        self.assertTrue(os.path.isfile(self.log_file))
97