1# Copyright (c) 2009, Google Inc. All rights reserved.
2# Copyright (c) 2009 Apple Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import codecs
31import os
32import sys
33
34
35# Simple class to split output between multiple destinations
36class Tee:
37    def __init__(self, *files):
38        self.files = files
39
40    # Callers should pass an already encoded string for writing.
41    def write(self, bytes):
42        for file in self.files:
43            file.write(bytes)
44
45
46class OutputTee:
47    def __init__(self):
48        self._original_stdout = None
49        self._original_stderr = None
50        self._files_for_output = []
51
52    def add_log(self, path):
53        log_file = self._open_log_file(path)
54        self._files_for_output.append(log_file)
55        self._tee_outputs_to_files(self._files_for_output)
56        return log_file
57
58    def remove_log(self, log_file):
59        self._files_for_output.remove(log_file)
60        self._tee_outputs_to_files(self._files_for_output)
61        log_file.close()
62
63    @staticmethod
64    def _open_log_file(log_path):
65        (log_directory, log_name) = os.path.split(log_path)
66        if log_directory and not os.path.exists(log_directory):
67            os.makedirs(log_directory)
68        return codecs.open(log_path, "a+", "utf-8")
69
70    def _tee_outputs_to_files(self, files):
71        if not self._original_stdout:
72            self._original_stdout = sys.stdout
73            self._original_stderr = sys.stderr
74        if files and len(files):
75            sys.stdout = Tee(self._original_stdout, *files)
76            sys.stderr = Tee(self._original_stderr, *files)
77        else:
78            sys.stdout = self._original_stdout
79            sys.stderr = self._original_stderr
80