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