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