/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: 2019-06-03 23:48:08 UTC
  • mfrom: (7316 work)
  • mto: This revision was merged to the branch mainline in revision 7328.
  • Revision ID: jelmer@jelmer.uk-20190603234808-15yk5c7054tj8e2b
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
Messages are classified by severity levels: critical, error, warning, info,
24
24
and debug.
25
25
 
26
 
They can be sent to two places: stderr, and `$XDG_CACHE_HOME/breezy/brz.log`.
27
 
For purposes such as running the test suite, they can also be redirected away
28
 
from both of those two places to another location.
 
26
They can be sent to two places: to stderr, and to ~/.brz.log.  For purposes
 
27
such as running the test suite, they can also be redirected away from both of
 
28
those two places to another location.
29
29
 
30
 
`brz.log` gets all messages, and full tracebacks for uncaught exceptions.
 
30
~/.brz.log gets all messages, and full tracebacks for uncaught exceptions.
31
31
This trace file is always in UTF-8, regardless of the user's default encoding,
32
32
so that we can always rely on writing any message.
33
33
 
72
72
 
73
73
lazy_import(globals(), """
74
74
from breezy import (
75
 
    bedding,
76
75
    debug,
 
76
    errors,
77
77
    osutils,
78
78
    ui,
79
79
    )
80
80
""")
81
 
from . import (
82
 
    errors,
83
 
    )
84
81
 
85
82
from .sixish import (
86
83
    PY3,
99
96
# than push/pop_log_file.
100
97
_trace_file = None
101
98
 
102
 
# Absolute path for brz.log.  Not changed even if the log/trace output is
 
99
# Absolute path for ~/.brz.log.  Not changed even if the log/trace output is
103
100
# redirected elsewhere.  Used to show the location in --version.
104
101
_brz_log_filename = None
105
102
 
206
203
 
207
204
 
208
205
def _get_brz_log_filename():
209
 
    """Return the brz log filename.
210
 
 
211
 
    :return: A path to the log file
212
 
    :raise EnvironmentError: If the cache directory could not be created
213
 
    """
214
206
    brz_log = osutils.path_from_environ('BRZ_LOG')
215
207
    if brz_log:
216
208
        return brz_log
217
 
    return os.path.join(bedding.cache_dir(), 'brz.log')
 
209
    home = osutils.path_from_environ('BRZ_HOME')
 
210
    if home is None:
 
211
        # GZ 2012-02-01: Logging to the home dir is bad, but XDG is unclear
 
212
        #                over what would be better. On windows, bug 240550
 
213
        #                suggests LOCALAPPDATA be used instead.
 
214
        home = osutils._get_home_dir()
 
215
    return os.path.join(home, '.brz.log')
218
216
 
219
217
 
220
218
def _open_brz_log():
221
 
    """Open the brz.log trace file.
 
219
    """Open the .brz.log trace file.
222
220
 
223
221
    If the log is more than a particular length, the old file is renamed to
224
 
    brz.log.old and a new file is started.  Otherwise, we append to the
 
222
    .brz.log.old and a new file is started.  Otherwise, we append to the
225
223
    existing file.
226
224
 
227
225
    This sets the global _brz_log_filename.
253
251
                break
254
252
        return os.fdopen(fd, 'ab', 0)  # unbuffered
255
253
 
 
254
    _brz_log_filename = _get_brz_log_filename()
 
255
    _rollover_trace_maybe(_brz_log_filename)
256
256
    try:
257
 
        _brz_log_filename = _get_brz_log_filename()
258
 
        _rollover_trace_maybe(_brz_log_filename)
259
 
 
260
257
        brz_log_file = _open_or_create_log_file(_brz_log_filename)
261
258
        brz_log_file.write(b'\n')
262
259
        if brz_log_file.tell() <= 2:
283
280
 
284
281
 
285
282
def enable_default_logging():
286
 
    """Configure default logging: messages to stderr and debug to brz.log
 
283
    """Configure default logging: messages to stderr and debug to .brz.log
287
284
 
288
285
    This should only be called once per process.
289
286
 
493
490
 
494
491
 
495
492
def report_exception(exc_info, err_file):
496
 
    """Report an exception to err_file (typically stderr) and to brz.log.
 
493
    """Report an exception to err_file (typically stderr) and to .brz.log.
497
494
 
498
495
    This will show either a full traceback or a short message as appropriate.
499
496
 
500
497
    :return: The appropriate exit code for this error.
501
498
    """
502
 
    # Log the full traceback to brz.log
 
499
    # Log the full traceback to ~/.brz.log
503
500
    log_exception_quietly()
504
501
    if 'error' in debug.debug_flags:
505
502
        print_exception(exc_info, err_file)