1#!/usr/bin/env python
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
19import time, sys
20import singletonmixin
21
22class Log(singletonmixin.Singleton):
23
24    def __init__(self, file):
25        """_file: filename or open file"""
26
27        if type(file) is str:
28            self._file = open(file, "a")
29        else:
30            self._file = file
31
32    def _getTime(self):
33        tm = time.time()
34        return "%s:%.2d" % (time.strftime('%m/%d/%Y %H:%M:%S',
35                                          time.localtime(tm)),
36                            int((tm - int(tm)) * 100))
37
38    def _log(self, *logstrs):
39        timeStr = self._getTime()
40        for ln in " ".join(map(str, logstrs)).split("\n"):
41            self._file.write("%s %s\n" % (timeStr, ln))
42        self._file.flush()
43
44    def debug(self, *logstrs):
45        self._log("D", *logstrs)
46    def info(self, *logstrs):
47        self._log("I", *logstrs)
48    def warn(self, *logstrs):
49        self._log("W", *logstrs)
50    def error(self, *logstrs):
51        self._log("E", *logstrs)
52
53    # default to info
54    log = info
55    __call__ = log
56