From fca18717fb31598e4257a9e80464e409f4e4c37b Mon Sep 17 00:00:00 2001 From: Mike Date: Mon, 5 Aug 2019 01:02:48 -0700 Subject: [PATCH] Upgrade the logging methods --- mikes-backup | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/mikes-backup b/mikes-backup index 08c67db..6e5799b 100755 --- a/mikes-backup +++ b/mikes-backup @@ -31,6 +31,8 @@ class MikesBackup: __force_full = False __force_differential = False + __log_file_handle = None + CONST_FULL_DIRECTORY_NAME = "Full" CONST_DIFFERENTIAL_DIRECTORY_NAME = "Differential" @@ -39,6 +41,11 @@ class MikesBackup: self.parse_args() + # + def __del__(self): + + self.close_log_file() + # def __str__(self): @@ -67,14 +74,41 @@ class MikesBackup: to_log = "[MikesBackup][" + the_date + "] " + s + # Print the log line print(to_log) + # Append the log line to the log file + log_file_path = self.make_log_path() + if log_file_path: + f = open(log_file_path, "a+") + f.write(to_log + "\n") + f.close() + # Recurse in order to print whatever o is outputting, if anything if o is not None: o_lines = str(o).split("\n") for line in o_lines: self.log(line) + # + def open_log_file(self): + + if self.__log_file_handle: + return self.__log_file_handle + + log_file_path = self.make_log_path() + if log_file_path: + self.__log_file_handle = open(log_file_path, "w") + + return self.__log_file_handle + + # + def close_log_file(self): + + if self.__log_file_handle: + self.__log_file_handle.close() + self.__log_file_handle = None + # def eprint(*args, **kwargs): @@ -352,7 +386,7 @@ class MikesBackup: # log_dir = self.__log_dir if log_dir is None: - self.log("No log directory specified; Defaulting to current working directory") + print("No log directory specified; Defaulting to current working directory") log_dir = "." return log_dir