diff options
| author | Elisa Sohier <elisa.sohier@art-software.fr> | 2019-08-02 10:27:27 +0200 |
|---|---|---|
| committer | Elisa Sohier <elisa.sohier@art-software.fr> | 2019-08-02 10:27:27 +0200 |
| commit | 71dc2cfeb47ced82d1766c2ceb5068e148e46aef (patch) | |
| tree | 53e4df2458075aa4835bd912bb83a4a71921936d | |
| parent | 7525f6558c5891e43f228b1e37f9c71d7a84cdf9 (diff) | |
Changed logging system
| -rw-r--r-- | autosync/__init__.py | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/autosync/__init__.py b/autosync/__init__.py index 0ef4705..039baa1 100644 --- a/autosync/__init__.py +++ b/autosync/__init__.py @@ -7,10 +7,28 @@ import time from collections import deque server = False +loglevel = { + 0: ("DEBUG", "3;240"), + 1: ("INFO", "0;37"), + 2: ("NOTIF", "1;37"), + 3: ("WARN", "1;33"), + 4: ("ERROR", "1;31") +} + +LOGLEVEL=1 def msg(data): print("\n\033[1;34m:: \033[1;37m{}\033[0m".format(data)) +def log(level, facility, text): + if level not in loglevel.keys(): + raise Exception("Loglevel is not valid: " + str(level)) + if level < LOGLEVEL: + return + print("\033[{1}m[{2}] {0} {3}: {4}\033[0m".format(*loglevel[level], time.strftime("%FT%T%z"), facility, text)) + with open(os.path.join(os.environ["HOME"], ".autoSync.log"), "a") as f: + f.write("[{2}] {0} {3}: {4}\n".format(*loglevel[level], time.strftime("%FT%T%z"), facility, text)) + def sync(source, dest, ignoreFile=None, verbose=False): """Synchronizes data between a source and a target.""" args = ["rsync", "--delete", "-zHaAXS", source, dest] @@ -35,13 +53,13 @@ def getTab(name="", elements=[]): name, src, dst = filter(lambda i: len(i) > 0, line.rstrip("\n").split("\t")) tabData[name] = (os.path.expandvars(os.path.expanduser(src)), os.path.expandvars(os.path.expanduser(dst))) except FileNotFoundError: - print("The tabfile @{} (path '{}') could not be read.".format( + log(4, "Tab", "The tabfile @{} (path '{}') could not be read.".format( name, tabFile )) return [] except PermissionError: - print("The tabfile @{} (path '{}') is not readable to me.".format( + log(4, "Tab", "The tabfile @{} (path '{}') is not readable to me.".format( name, tabFile )) @@ -60,9 +78,11 @@ def getTimestamp(source, dest): destTS = os.path.join(dest, ".lastsync") if sync(sourceTS, "/tmp/source.ts") != 0: + log(0, "ts", "Source timestamp could not be retrieved, assuming zero") with open("/tmp/source.ts", "w") as f: f.write("0") if sync(destTS, "/tmp/dest.ts") != 0: + log(0, "ts", "Destination timestamp could not be retrieved, assuming zero") with open("/tmp/dest.ts", "w") as f: f.write("0") @@ -76,7 +96,10 @@ def getTimestamp(source, dest): return tsTuple def run(): - global server + global server, LOGLEVEL + if "LOGLEVEL" in os.environ: + LOGLEVEL = int(os.environ["LOGLEVEL"]) + args = deque(sys.argv[1:]) if len(args) < 1: print("{0} @ – list available tab names\n{0} @@tabname – list available entries under tabname\n{0}[ @tabname] entry1 entry2 – synchronizes entry1 and entry2 from tabname (or default)\n{0} @tabname – synchronizes every entry found in tabname\n{0} -s [@tabname][ entry1[ entry2[ …]]] – synchronizes in server mode (ie don't refresh timestamp)\n\nIf tabname is omitted, will use default file located at ~/.autoSync.tab (also available through the @default tabname)".format(os.path.basename(sys.argv[0]))) @@ -86,6 +109,7 @@ def run(): if firstArg == "-s": server = True firstArg = args.popleft() + log(1, "main", "Activated server mode") tabname = "" @@ -113,8 +137,10 @@ def run(): else: tabname = firstArg.lstrip("@") + log(0, "main", "tabname given, setting to {}".format(tabname)) else: args.appendleft(firstArg) + log(0, "main", "no tabname given, using default") for name, src, dst in getTab(tabname, args): srcT, dstT = getTimestamp(src, dst) @@ -125,23 +151,23 @@ def run(): tabname = "default" status = None if srcT >= dstT: - msg("[{} @ {}] sending data to destination".format(name, tabname)) + log(2, "sync {}@{}".format(name, tabname), "Source is more recent than destination, sending data") if not server: with open(os.path.join(src, ".lastsync"), "w") as f: f.write(str(time.time())) else: - print("\033[2;3;37mServer mode, won't update timestamp\033[0m") + log(1, "sync {}@{}".format(name, tabname), "Server mode is on, will not update timestamps") status = sync(src, dst, ignoreFile, True) else: + log(2, "sync {}@{}".format(name, tabname), "Destination is more recent than source, fetching data") if server: - print("\033[2;3;37mServer mode, won't update timestamp\033[0m") - msg("[{} @ {}] gathering data from destination".format(name, tabname)) + log(1, "sync {}@{}".format(name, tabname), "Server mode is on, will not update timestamps") status = sync(dst, src, ignoreFile, True) if not server and status == 0: with open(os.path.join(src, ".lastsync"), "w") as f: f.write(str(time.time())) if status != 0 and status is not None: - print("Error while syncing target {} @ {}, aborting".format(name, tabname)) + log(4, "Error while syncing target {} @ {}, aborting".format(name, tabname)) return if os.path.exists("/tmp/source.ts"): |
