• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/python2.4
2#
3#
4# Copyright 2007, The Android Open Source Project
5#
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
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
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.
17
18"""Simple logging utility. Dumps log messages to stdout, and optionally, to a
19log file.
20
21Init(path) must be called to enable logging to a file
22"""
23
24import datetime
25
26_LOG_FILE = None
27_verbose = False
28_log_time = True
29
30def Init(log_file_path):
31  """Set the path to the log file"""
32  global _LOG_FILE
33  _LOG_FILE = log_file_path
34  print "Using log file: %s" % _LOG_FILE
35
36def GetLogFilePath():
37  """Returns the path and name of the Log file"""
38  global _LOG_FILE
39  return _LOG_FILE
40
41def Log(new_str):
42  """Appends new_str to the end of _LOG_FILE and prints it to stdout.
43
44  Args:
45    # new_str is a string.
46    new_str: 'some message to log'
47  """
48  msg = _PrependTimeStamp(new_str)
49  print msg
50  _WriteLog(msg)
51
52def _WriteLog(msg):
53  global _LOG_FILE
54  if _LOG_FILE is not None:
55    file_handle = file(_LOG_FILE, 'a')
56    file_handle.write('\n' + str(msg))
57    file_handle.close()
58
59def _PrependTimeStamp(log_string):
60  """Returns the log_string prepended with current timestamp """
61  global _log_time
62  if _log_time:
63    return "# %s: %s" % (datetime.datetime.now().strftime("%m/%d/%y %H:%M:%S"),
64        log_string)
65  else:
66    # timestamp logging disabled
67    return log_string
68
69def SilentLog(new_str):
70  """Silently log new_str. Unless verbose mode is enabled, will log new_str
71    only to the log file
72  Args:
73    # new_str is a string.
74    new_str: 'some message to log'
75  """
76  global _verbose
77  msg = _PrependTimeStamp(new_str)
78  if _verbose:
79    print msg
80  _WriteLog(msg)
81
82def SetVerbose(new_verbose=True):
83  """ Enable or disable verbose logging"""
84  global _verbose
85  _verbose = new_verbose
86
87def SetTimestampLogging(new_timestamp=True):
88  """ Enable or disable outputting a timestamp with each log entry"""
89  global _log_time
90  _log_time = new_timestamp
91
92def main():
93  pass
94
95if __name__ == '__main__':
96  main()
97