4 # Copyright 2007, The Android Open Source Project
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 """Simple logging utility. Dumps log messages to stdout, and optionally, to a
21 Init(path) must be called to enable logging to a file
30 def Init(log_file_path
):
31 """Set the path to the log file"""
33 _LOG_FILE
= log_file_path
34 print "Using log file: %s" % _LOG_FILE
37 """Returns the path and name of the Log file"""
42 """Appends new_str to the end of _LOG_FILE and prints it to stdout.
45 # new_str is a string.
46 new_str: 'some message to log'
48 msg
= _PrependTimeStamp(new_str
)
54 if _LOG_FILE
is not None:
55 file_handle
= file(_LOG_FILE
, 'a')
56 file_handle
.write('\n' + str(msg
))
59 def _PrependTimeStamp(log_string
):
60 """Returns the log_string prepended with current timestamp """
63 return "# %s: %s" % (datetime
.datetime
.now().strftime("%m/%d/%y %H:%M:%S"),
66 # timestamp logging disabled
69 def SilentLog(new_str
):
70 """Silently log new_str. Unless verbose mode is enabled, will log new_str
73 # new_str is a string.
74 new_str: 'some message to log'
77 msg
= _PrependTimeStamp(new_str
)
82 def SetVerbose(new_verbose
=True):
83 """ Enable or disable verbose logging"""
85 _verbose
= new_verbose
87 def SetTimestampLogging(new_timestamp
=True):
88 """ Enable or disable outputting a timestamp with each log entry"""
90 _log_time
= new_timestamp
95 if __name__
== '__main__':