]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/helpers.py
Introduce BUP_DEBUG, --debug, and tone down the log messages a lot.
[bup.git] / lib / bup / helpers.py
index daaa0c4402aa605f55b304e4f84816983e6e382a..34067006f099b07695da943087cfc327d4702c2a 100644 (file)
@@ -3,6 +3,17 @@ import sys, os, pwd, subprocess, errno, socket, select, mmap, stat, re
 from bup import _version
 
 
+def atoi(s):
+    """Convert the string 's' to an integer. Return 0 if s is not a number."""
+    try:
+        return int(s or '0')
+    except ValueError:
+        return 0
+
+
+buglvl = atoi(os.environ.get('BUP_DEBUG', 0))
+
+
 # Write (blockingly) to sockets that may or may not be in blocking mode.
 # We need this because our stderr is sometimes eaten by subprocesses
 # (probably ssh) that sometimes make it nonblocking, if only temporarily,
@@ -26,6 +37,16 @@ def log(s):
     _hard_write(sys.stderr.fileno(), s)
 
 
+def debug1(s):
+    if buglvl >= 1:
+        log(s)
+
+
+def debug2(s):
+    if buglvl >= 2:
+        log(s)
+
+
 def mkdirp(d, mode=None):
     """Recursively create directories on path 'd'.
 
@@ -305,14 +326,6 @@ def count(l):
     return reduce(lambda x,y: x+1, l)
 
 
-def atoi(s):
-    """Convert the string 's' to an integer. Return 0 if s is not a number."""
-    try:
-        return int(s or '0')
-    except ValueError:
-        return 0
-
-
 saved_errors = []
 def add_error(e):
     """Append an error message to the list of saved errors.