/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/trace.py

  • Committer: Jelmer Vernooij
  • Date: 2020-07-05 12:50:01 UTC
  • mfrom: (7490.40.46 work)
  • mto: (7490.40.48 work)
  • mto: This revision was merged to the branch mainline in revision 7519.
  • Revision ID: jelmer@jelmer.uk-20200705125001-7s3vo0p55szbbws7
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
form.
46
46
"""
47
47
 
 
48
from __future__ import absolute_import
 
49
 
48
50
# FIXME: Unfortunately it turns out that python's logging module
49
51
# is quite expensive, even when the message is not printed by any handlers.
50
52
# We should perhaps change back to just simply doing it here.
55
57
# that.
56
58
 
57
59
import errno
58
 
from io import StringIO
59
60
import logging
60
61
import os
61
62
import sys
81
82
    errors,
82
83
    )
83
84
 
 
85
from .sixish import (
 
86
    PY3,
 
87
    StringIO,
 
88
    text_type,
 
89
    )
 
90
 
84
91
 
85
92
# global verbosity for breezy; controls the log level for stderr; 0=normal; <0
86
93
# is quiet; >0 is verbose.
155
162
        fmt = fmt.decode('ascii', 'replace')
156
163
 
157
164
    if args:
 
165
        if not PY3:
 
166
            args = tuple(
 
167
                _Bytes(arg) if isinstance(arg, bytes) else arg for arg in args)
158
168
        out = fmt % args
159
169
    else:
160
170
        out = fmt
201
211
    :return: A path to the log file
202
212
    :raise EnvironmentError: If the cache directory could not be created
203
213
    """
204
 
    brz_log = os.environ.get('BRZ_LOG')
 
214
    brz_log = osutils.path_from_environ('BRZ_LOG')
205
215
    if brz_log:
206
216
        return brz_log
207
217
    return os.path.join(bedding.cache_dir(), 'brz.log')
296
306
        r'%Y-%m-%d %H:%M:%S')
297
307
    # after hooking output into brz_log, we also need to attach a stderr
298
308
    # handler, writing only at level info and with encoding
299
 
    stderr_handler = logging.StreamHandler(stream=sys.stderr)
 
309
    if sys.version_info[0] == 2:
 
310
        stderr_handler = EncodedStreamHandler(
 
311
            sys.stderr, osutils.get_terminal_encoding(), 'replace',
 
312
            level=logging.INFO)
 
313
    else:
 
314
        stderr_handler = logging.StreamHandler(stream=sys.stderr)
300
315
    logging.getLogger('brz').addHandler(stderr_handler)
301
316
    return memento
302
317
 
509
524
    elif not getattr(exc_object, 'internal_error', True):
510
525
        report_user_error(exc_info, err_file)
511
526
        return errors.EXIT_ERROR
512
 
    elif isinstance(exc_object, EnvironmentError):
 
527
    elif osutils.is_environment_error(exc_object):
513
528
        if getattr(exc_object, 'errno', None) == errno.EPIPE:
514
529
            err_file.write("brz: broken pipe\n")
515
530
            return errors.EXIT_ERROR
600
615
 
601
616
    def emit(self, record):
602
617
        try:
603
 
            if not isinstance(record.msg, str):
 
618
            if not isinstance(record.msg, text_type):
604
619
                msg = record.msg.decode("utf-8")
605
 
                record.msg = msg
 
620
                if PY3:
 
621
                    record.msg = msg
606
622
            line = self.format(record)
607
 
            if not isinstance(line, str):
 
623
            if not isinstance(line, text_type):
608
624
                line = line.decode("utf-8")
609
625
            self.stream.write(line.encode(self.encoding, self.errors) + b"\n")
610
626
        except Exception: