Skip to content

Commit 2998082

Browse files
committed
Use tqdm logging_redirect_tqdm to not break progress bars (#294)
1 parent 4f33e85 commit 2998082

6 files changed

Lines changed: 38 additions & 20 deletions

File tree

Changelog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Version 2.41
22
---------
33
* Disable building rust bindings when creating venv (#293)
44
* Small code cleanups
5-
5+
* Use tqdm logging_redirect_tqdm to not break progress bars (#294) 
6+
67
Version 2.40
78
---------
89
* Add OpenSUSE 16 to build scripts

libvirtnbdbackup/backup/disk.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def _getExtentHandler(args: Namespace, nbdClient):
5252
"""Query dirty blocks either via qemu client or self
5353
implemented extend handler"""
5454
if args.qemu:
55-
logging.info("Using qemu tools to query extents")
55+
lib.safeInfo("Using qemu tools to query extents")
5656
extentHandler = extenthandler.ExtentHandler(
5757
qemu.util(nbdClient.cType.exportName),
5858
nbdClient.cType,
@@ -94,7 +94,7 @@ def backup( # pylint: disable=too-many-arguments,too-many-branches, too-many-lo
9494
raise exceptions.DiskBackupFailed("Failed to start NBD server.")
9595

9696
if disk.discardOption is not None:
97-
logging.info("Virtual disk discard option: [%s]", disk.discardOption)
97+
lib.safeInfo("Virtual disk discard option: [%s]", disk.discardOption)
9898

9999
connection = server.connect(args, disk, metaContext, remoteIP, port, virtClient)
100100

@@ -107,17 +107,17 @@ def backup( # pylint: disable=too-many-arguments,too-many-branches, too-many-lo
107107
return 0, False
108108

109109
thinBackupSize = sum(extent.length for extent in extents if extent.data is True)
110-
logging.info("Got %s extents to backup.", len(extents))
110+
lib.safeInfo("Got %s extents to backup.", len(extents))
111111
logging.debug("%s", lib.dumpExtentJson(extents))
112-
logging.info("%s bytes [%s] virtual disk size", diskSize, lib.humanize(diskSize))
113-
logging.info(
112+
lib.safeInfo("%s bytes [%s] virtual disk size", diskSize, lib.humanize(diskSize))
113+
lib.safeInfo(
114114
"%s bytes [%s] of data extents to backup",
115115
thinBackupSize,
116116
lib.humanize(thinBackupSize),
117117
)
118118

119119
if args.level in ("inc", "diff") and thinBackupSize == 0:
120-
logging.info("No dirty blocks found")
120+
lib.safeInfo("No dirty blocks found")
121121
args.noprogress = True
122122

123123
targetFile, targetFilePartial = target.Set(args, disk)
@@ -132,10 +132,10 @@ def backup( # pylint: disable=too-many-arguments,too-many-branches, too-many-lo
132132
writer = target.get(args, fileStream, targetFile, targetFilePartial)
133133

134134
if streamType == "raw":
135-
logging.info("Creating full provisioned raw backup image")
135+
lib.safeInfo("Creating full provisioned raw backup image")
136136
writer.truncate(diskSize)
137137
else:
138-
logging.info("Creating thin provisioned stream backup image")
138+
lib.safeInfo("Creating thin provisioned stream backup image")
139139
header = dStream.dumpMetadata(
140140
args,
141141
diskSize,
@@ -213,15 +213,15 @@ def backup( # pylint: disable=too-many-arguments,too-many-branches, too-many-lo
213213
connection.disconnect()
214214

215215
if args.offline is True and virtClient.remoteHost == "":
216-
logging.info("Stopping NBD Service.")
216+
lib.safeInfo("Stopping NBD Service.")
217217
lib.killProc(nbdProc.pid)
218218

219219
if args.offline is True:
220220
lib.remove(args, nbdProc.pidFile)
221221

222222
if not args.stdout:
223223
if args.noprogress is True:
224-
logging.info(
224+
lib.safeInfo(
225225
"Backup of disk [%s] finished, file: [%s]", disk.target, targetFile
226226
)
227227
partialfile.rename(targetFilePartial, targetFile)

libvirtnbdbackup/backup/metadata.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from libvirtnbdbackup.qemu.exceptions import ProcessError
2828
from libvirtnbdbackup.ssh.exceptions import sshError
2929
from libvirtnbdbackup.output.exceptions import OutputException
30+
from libvirtnbdbackup.common import safeInfo
3031

3132

3233
log = logging.getLogger()
@@ -36,9 +37,9 @@ def backupChecksum(fileStream, targetFile):
3637
"""Save the calculated adler32 checksum, it can be verified
3738
by virtnbdbrestore's verify function.'"""
3839
checksum = fileStream.checksum()
39-
log.info("Checksum for file: [%s]:[%s]", targetFile, checksum)
40+
safeInfo("Checksum for file: [%s]:[%s]", targetFile, checksum)
4041
chksumfile = f"{targetFile}.chksum"
41-
log.info("Saving checksum to: [%s]", chksumfile)
42+
safeInfo("Saving checksum to: [%s]", chksumfile)
4243
with output.openfile(chksumfile, "w") as cf:
4344
cf.write(f"{checksum}")
4445

libvirtnbdbackup/backup/target.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import BinaryIO
2020
from argparse import Namespace
2121
from libvirtnbdbackup.virt.client import DomainDisk
22-
from libvirtnbdbackup.common import getIdent
22+
from libvirtnbdbackup.common import getIdent, safeInfo
2323

2424

2525
def get(
@@ -30,7 +30,7 @@ def get(
3030
logging.info("Writing data to zip archive.")
3131
fileStream.open(targetFile)
3232
else:
33-
logging.info("Write data to target file: [%s].", targetFilePartial)
33+
safeInfo("Write data to target file: [%s].", targetFilePartial)
3434
fileStream.open(targetFilePartial)
3535

3636
return fileStream

libvirtnbdbackup/common.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@ def getLogFile(fileName: str) -> Optional[logging.FileHandler]:
107107
return None
108108

109109

110+
def safeInfo(msg, *args, **kwargs):
111+
"""Use tqdm redirect to not destroy progress bars"""
112+
rootlog = logging.getLogger("")
113+
kwargs.setdefault("stacklevel", 2)
114+
try:
115+
from tqdm.contrib.logging import ( # pylint: disable=import-outside-toplevel
116+
logging_redirect_tqdm,
117+
)
118+
except ModuleNotFoundError:
119+
rootlog.info(msg, *args, **kwargs)
120+
return
121+
122+
with logging_redirect_tqdm():
123+
rootlog.info(msg, *args, **kwargs)
124+
125+
110126
def configLogger(
111127
args: Namespace, fileLog: Optional[logging.FileHandler], counter: logCount
112128
):

libvirtnbdbackup/extenthandler/extenthandler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import List, Any, Generator, Dict
2020
from nbd import CONTEXT_BASE_ALLOCATION
2121
from libvirtnbdbackup.objects import Extent, _ExtentObj
22-
from libvirtnbdbackup.common import humanize
22+
from libvirtnbdbackup.common import humanize, safeInfo
2323

2424
log = logging.getLogger("extenthandler")
2525

@@ -249,7 +249,7 @@ def overlap(self, extents: List[Extent]) -> List[Extent]:
249249
j += 1
250250

251251
if totalLength > 0:
252-
log.info(
252+
safeInfo(
253253
"Detected %d bytes [%s] non-sparse blocks for current bitmap.",
254254
totalLength,
255255
humanize(totalLength),
@@ -261,9 +261,9 @@ def queryBlockStatus(self) -> List[Extent]:
261261
"""Check the status for each extent, whether if it is
262262
real data or zeroes, return a list of extent objects
263263
"""
264-
log.info("Start receiving backup extents.")
264+
safeInfo("Start receiving backup extents.")
265265
extents = self.queryExtents()
266-
log.info("Finished receiving extents.")
266+
safeInfo("Finished receiving extents.")
267267
extentList: List[Extent] = []
268268
start: int = 0
269269
baseStart: int = 0
@@ -290,7 +290,7 @@ def queryBlockStatus(self) -> List[Extent]:
290290
extObj.offset + extObj.length,
291291
)
292292
if self.no_sparse_detection is True:
293-
log.info("Skipping detection of sparse/fstrimmed blocks.")
293+
safeInfo("Skipping detection of sparse/fstrimmed blocks.")
294294
return extentList
295295

296296
if self._metaContext != CONTEXT_BASE_ALLOCATION:

0 commit comments

Comments
 (0)