summaryrefslogtreecommitdiff
path: root/unrpyc/renpy/log.py
blob: 37fec9aeffade14e714c7706f82150565c71cec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright 2004-2013 Tom Rothamel <pytom@bishoujo.us>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# This module handles the logging of messages to a file.

import os.path
import codecs
import traceback
import platform
import time
import tempfile

import renpy

# The file events are logged to.
log_file = None

class LogFile(object):
    """
    This manages one of our logfiles.
    """
    
    def __init__(self, name, append=False, developer=False):
        """
        `name`
            The name of the logfile, without the .txt extension.
        `append`
            If true, we will append to the logfile. If false, we will truncate
            it to an empty file the first time we write to it.
        `developer`
            If true, nothing happens if config.developer is not set to True.
        """
        
        self.name = name
        self.append = append
        self.developer = developer
        self.file = None
        
        # File-like attributes.
        self.softspace = 0
        self.newlines = None
        
        # Should we emulate file's write method? We do so if this is True.
        self.raw_write = False
        
    def open(self): #@ReservedAssignment

        if self.file:
            return True

        if self.developer and not renpy.config.developer:
            return False
        
        if not renpy.config.log_enable:
            return False

        try:
            base = os.environ.get("RENPY_LOG_BASE", renpy.config.basedir)
            fn = os.path.join(base, self.name + ".txt")
        
            altfn = os.path.join(tempfile.gettempdir(), "renpy-" + self.name + ".txt")
            
        
            if renpy.android:
                print("Logging to", fn)

            if self.append:
                mode = "a"
            else:
                mode = "w"

            try:        
                self.file = codecs.open(fn, mode, "utf-8")
            except:
                self.file = codecs.open(altfn, mode, "utf-8")

            if self.append:                
                self.write('')
                self.write('=' * 78)
                self.write('')

            self.write("%s", time.ctime())
            self.write("%s", platform.platform())
            self.write("%s", renpy.version)
            self.write("%s %s", renpy.config.name, renpy.config.version)
            self.write("")
            
            return True

        except:
            return False

    def write(self, s, *args):
        """
        Formats `s` with args, and writes it to the logfile.
        """

        if self.open():
        
            if not self.raw_write:
                s = s % args
                s += "\n"

            if not isinstance(s, str):
                s = s.decode("latin-1")

            s = s.replace("\n", "\r\n")            
            
            self.file.write(s)
            self.file.flush()
            
    def exception(self):
        """
        Writes the exception to the logfile.
        """
        
        self.raw_write = True
        traceback.print_exc(None, self)
        self.raw_write = False

# A map from the log name to a log object.
log_cache = { }

def open(name, append=False, developer=False): #@ReservedAssignment
    rv = log_cache.get(name, None)
    
    if rv is None:
        rv = LogFile(name, append=append, developer=developer)
        log_cache[name] = rv
        
    return rv