log stats consistently, fixes #526

prune and create now both require --verbose --stats to show stats.
it was implemented in this way (and not with print) so you can feed the stats data
into the logging system, too.

delete now says "Archive deleted" in verbose mode (for consistency,
it already said "Repository deleted" when deleting a repo).

also: add helpers.log_multi to comfortably and prettily output a block of log lines
This commit is contained in:
Thomas Waldmann 2016-01-12 00:41:06 +01:00
parent c5dcf46d44
commit 84672f7081
2 changed files with 35 additions and 11 deletions

View File

@ -21,7 +21,7 @@ from .helpers import Error, location_validator, format_time, format_file_size, \
get_cache_dir, get_keys_dir, prune_within, prune_split, unhexlify, \
Manifest, remove_surrogates, update_excludes, format_archive, check_extension_modules, Statistics, \
dir_is_tagged, bigint_to_int, ChunkerParams, CompressionSpec, is_slow_msgpack, yes, sysinfo, \
EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR
EXIT_SUCCESS, EXIT_WARNING, EXIT_ERROR, log_multi
from .logger import create_logger, setup_logging
logger = create_logger()
from .compress import Compressor, COMPR_BUFFER
@ -37,6 +37,8 @@ has_lchflags = hasattr(os, 'lchflags')
# default umask, overriden by --umask, defaults to read/write only for owner
UMASK_DEFAULT = 0o077
DASHES = '-' * 78
class ToggleAction(argparse.Action):
"""argparse action to handle "toggle" flags easily
@ -187,12 +189,12 @@ class Archiver:
archive.stats.show_progress(final=True)
if args.stats:
archive.end = datetime.now()
print('-' * 78)
print(str(archive))
print()
print(str(archive.stats))
print(str(cache))
print('-' * 78)
log_multi(DASHES,
str(archive),
DASHES,
str(archive.stats),
str(cache),
DASHES)
return self.exit_code
def _process(self, archive, cache, excludes, exclude_caches, exclude_if_present,
@ -339,9 +341,12 @@ class Archiver:
manifest.write()
repository.commit(save_space=args.save_space)
cache.commit()
logger.info("Archive deleted.")
if args.stats:
logger.info(stats.summary.format(label='Deleted data:', stats=stats))
logger.info(str(cache))
log_multi(DASHES,
stats.summary.format(label='Deleted data:', stats=stats),
str(cache),
DASHES)
else:
if not args.cache_only:
msg = []
@ -495,8 +500,10 @@ class Archiver:
repository.commit(save_space=args.save_space)
cache.commit()
if args.stats:
logger.info(stats.summary.format(label='Deleted data:', stats=stats))
logger.info(str(cache))
log_multi(DASHES,
stats.summary.format(label='Deleted data:', stats=stats),
str(cache),
DASHES)
return self.exit_code
def do_upgrade(self, args):

View File

@ -18,6 +18,10 @@ import platform
import time
import unicodedata
import logging
from .logger import create_logger
logger = create_logger()
from datetime import datetime, timezone, timedelta
from fnmatch import translate
from operator import attrgetter
@ -971,3 +975,16 @@ def sysinfo():
info.append('Python: %s %s' % (platform.python_implementation(), platform.python_version()))
info.append('')
return '\n'.join(info)
def log_multi(*msgs, level=logging.INFO):
"""
log multiple lines of text, each line by a separate logging call for cosmetic reasons
each positional argument may be a single or multiple lines (separated by \n) of text.
"""
lines = []
for msg in msgs:
lines.extend(msg.splitlines())
for line in lines:
logger.log(level, line)