aboutsummaryrefslogtreecommitdiff
blob: 798350cb654a0dc44b49e48fe570f0185a5302b2 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# (Be in -*- python -*- mode.)
#
# ====================================================================
# Copyright (c) 2000-2008 CollabNet.  All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.  The terms
# are also available at http://subversion.tigris.org/license-1.html.
# If newer versions of this license are posted there, you may use a
# newer version instead, at your option.
#
# This software consists of voluntary contributions made by many
# individuals.  For exact contribution history, see the revision
# history and logs, available at http://cvs2svn.tigris.org/.
# ====================================================================

"""This module contains a simple logging facility for cvs2svn."""


import sys
import time
import threading


class Log:
  """A Simple logging facility.

  If self.log_level is DEBUG or higher, each line will be timestamped
  with the number of wall-clock seconds since the time when this
  module was first imported.

  If self.use_timestamps is True, each line will be timestamped with a
  human-readable clock time.

  The public methods of this class are thread-safe.

  This class is a Borg; see
  http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531."""

  # These constants represent the log levels that this class supports.
  # The increase_verbosity() and decrease_verbosity() methods rely on
  # these constants being consecutive integers:
  ERROR = -2
  WARN = -1
  QUIET = 0
  NORMAL = 1
  VERBOSE = 2
  DEBUG = 3

  start_time = time.time()

  __shared_state = {}

  def __init__(self):
    self.__dict__ = self.__shared_state
    if self.__dict__:
      return

    self.log_level = Log.NORMAL

    # Set this to True if you want to see timestamps on each line output.
    self.use_timestamps = False

    # The output file to use for errors:
    self._err = sys.stderr

    # The output file to use for lower-priority messages:
    self._out = sys.stdout

    # Lock to serialize writes to the log:
    self.lock = threading.Lock()

  def increase_verbosity(self):
    self.lock.acquire()
    try:
      self.log_level = min(self.log_level + 1, Log.DEBUG)
    finally:
      self.lock.release()

  def decrease_verbosity(self):
    self.lock.acquire()
    try:
      self.log_level = max(self.log_level - 1, Log.ERROR)
    finally:
      self.lock.release()

  def is_on(self, level):
    """Return True iff messages at the specified LEVEL are currently on.

    LEVEL should be one of the constants Log.WARN, Log.QUIET, etc."""

    return self.log_level >= level

  def _timestamp(self):
    """Return a timestamp if needed, as a string with a trailing space."""

    retval = []

    if self.log_level >= Log.DEBUG:
      retval.append('%f: ' % (time.time() - self.start_time,))

    if self.use_timestamps:
      retval.append(time.strftime('[%Y-%m-%d %I:%M:%S %Z] - '))

    return ''.join(retval)

  def _write(self, out, *args):
    """Write a message to OUT.

    If there are multiple ARGS, they will be separated by spaces.  If
    there are multiple lines, they will be output one by one with the
    same timestamp prefix."""

    timestamp = self._timestamp()
    s = ' '.join(map(str, args))
    lines = s.split('\n')
    if lines and not lines[-1]:
      del lines[-1]

    self.lock.acquire()
    try:
      for s in lines:
        out.write('%s%s\n' % (timestamp, s,))
      # Ensure that log output doesn't get out-of-order with respect to
      # stderr output.
      out.flush()
    finally:
      self.lock.release()

  def write(self, *args):
    """Write a message to SELF._out.

    This is a public method to use for writing to the output log
    unconditionally."""

    self._write(self._out, *args)

  def error(self, *args):
    """Log a message at the ERROR level."""

    if self.is_on(Log.ERROR):
      self._write(self._err, *args)

  def warn(self, *args):
    """Log a message at the WARN level."""

    if self.is_on(Log.WARN):
      self._write(self._out, *args)

  def quiet(self, *args):
    """Log a message at the QUIET level."""

    if self.is_on(Log.QUIET):
      self._write(self._out, *args)

  def normal(self, *args):
    """Log a message at the NORMAL level."""

    if self.is_on(Log.NORMAL):
      self._write(self._out, *args)

  def verbose(self, *args):
    """Log a message at the VERBOSE level."""

    if self.is_on(Log.VERBOSE):
      self._write(self._out, *args)

  def debug(self, *args):
    """Log a message at the DEBUG level."""

    if self.is_on(Log.DEBUG):
      self._write(self._out, *args)