1# Copyright (c) 2011, Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7# * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9# * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13# * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29import os
30import re
31import sys
32
33
34def _is_crash_reporter(process_name):
35 return re.match(r"ReportCrash", process_name)
36
37
38class CrashLog(object):
39 def __init__(self, executive, filesystem, process_name):
40 self._executive = executive
41 self._filesystem = filesystem
42 self._log = None
43 if sys.platform == "darwin":
44 self._capture_log_darwin(process_name)
45
46 def log(self):
47 return self._log
48
49 def _log_directory_darwin(self):
50 log_directory = self._filesystem.expanduser("~")
51 log_directory = os.path.join(log_directory, "Library", "Logs")
52 if self._filesystem.exists(os.path.join(log_directory, "DiagnosticReports")):
53 log_directory = os.path.join(log_directory, "DiagnosticReports")
54 else:
55 log_directory = os.path.join(log_directory, "CrashReporter")
56 return log_directory
57
58 def _capture_log_darwin(self, process_name):
59 def is_crash_log(fs, dirpath, basename):
60 return basename.startswith(process_name + "_") and basename.endswith(".crash")
61
62 log_directory = self._log_directory_darwin()
63 logs = self._filesystem.files_under(log_directory, file_filter=is_crash_log)
64 if not logs:
65 return
66
67 self._executive.wait_newest(_is_crash_reporter)
68 self._log = self._filesystem.read_binary_file(sorted(logs)[-1])