Support for truncate/append on log files

Summary:
Presently, if a log file already exists, lldb simply starts overwriting bits of it, without
truncating or anything. This patch makes it use eFileOptionFileTruncate by default. It also adds
an --append option, which will append to the file without truncating. A test is included.

Reviewers: clayborg

Subscribers: lldb-commits

Differential Revision: http://reviews.llvm.org/D8450

llvm-svn: 232801
diff --git a/lldb/test/logging/TestLogging.py b/lldb/test/logging/TestLogging.py
index 11d4321..aea077d 100644
--- a/lldb/test/logging/TestLogging.py
+++ b/lldb/test/logging/TestLogging.py
@@ -2,7 +2,7 @@
 Test lldb logging.  This test just makes sure logging doesn't crash, and produces some output.
 """
 
-import os, time
+import os, time, string
 import unittest2
 import lldb
 from lldbtest import *
@@ -10,6 +10,15 @@
 class LogTestCase(TestBase):
 
     mydir = TestBase.compute_mydir(__file__)
+    append_log_file = "lldb-commands-log-append.txt"
+    truncate_log_file = "lldb-commands-log-truncate.txt"
+
+
+    @classmethod
+    def classCleanup(cls):
+        """Cleanup the test byproducts."""
+        cls.RemoveTempFile(cls.truncate_log_file)
+        cls.RemoveTempFile(cls.append_log_file)
 
     @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
     @dsym_test
@@ -56,6 +65,48 @@
 
         self.assertTrue(log_lines > 0, "Something was written to the log file.")
 
+    # Check that lldb truncates its log files
+    def test_log_truncate (self):
+        if (os.path.exists (self.truncate_log_file)):
+            os.remove (self.truncate_log_file)
+
+        # put something in our log file
+        with open(self.truncate_log_file, "w") as f:
+            for i in range(1, 1000):
+                f.write("bacon\n")
+
+        self.runCmd ("log enable -t -f '%s' lldb commands" % (self.truncate_log_file))
+        self.runCmd ("help log")
+        self.runCmd ("log disable lldb")
+
+        self.assertTrue (os.path.isfile (self.truncate_log_file))
+        with open(self.truncate_log_file, "r") as f:
+            contents = f.read ()
+
+        # check that it got removed
+        self.assertTrue(string.find(contents, "bacon") == -1)
+
+    # Check that lldb can append to a log file
+    def test_log_append (self):
+        if (os.path.exists (self.append_log_file)):
+            os.remove (self.append_log_file)
+
+        # put something in our log file
+        with open(self.append_log_file, "w") as f:
+            f.write("bacon\n")
+
+        self.runCmd ("log enable -t -a -f '%s' lldb commands" % (self.append_log_file))
+        self.runCmd ("help log")
+        self.runCmd ("log disable lldb")
+
+        self.assertTrue (os.path.isfile (self.append_log_file))
+        with open(self.append_log_file, "r") as f:
+            contents = f.read ()
+
+        # check that it is still there
+        self.assertTrue(string.find(contents, "bacon") == 0)
+
+
 if __name__ == '__main__':
     import atexit
     lldb.SBDebugger.Initialize()