Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFern2 committed Mar 1, 2021
1 parent be86672 commit f022acd
Showing 1 changed file with 37 additions and 22 deletions.
59 changes: 37 additions & 22 deletions data_preserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@
file_extension = ''
comm = PLC()
CODE_VERSION = "1.3.0"
log = open("log.txt", "a+")
log_file = "log.txt"
log_formatter = logging.Formatter('%(asctime)s - %(message)s')
log_handler = RotatingFileHandler(log_file, mode='a', maxBytes=5*1024*1024,
backupCount=2, encoding=None, delay=0)
log_handler.setFormatter(log_formatter)
log_handler.setLevel(logging.DEBUG)
app_log = logging.getLogger('root')
app_log.setLevel(logging.DEBUG)
app_log.addHandler(log_handler)

now = datetime.datetime.now()
checkErrorLog = False

Expand Down Expand Up @@ -123,7 +132,7 @@ def check_multiple(lines, file_name):
# search if there are more than two
for index in range(len(lines)):
if lines[index].count("|") > 2:
log.write("%s Save Info: %s line %s Multiple tags in one line\n" % (now.strftime("%c"), file_name, index+1))
app_log.info("Save Info: %s line %s Multiple tags in one line" % (file_name, index+1))
# process line here, and split into more items
clean_list.extend(split_tag_lines(lines[index]))
else:
Expand Down Expand Up @@ -164,7 +173,7 @@ def process_line_save(line, line_number, file_name):
tags_list.append(put_string)

except ValueError as e:
log.write("%s Save Error: %s line %s tag %s %s\n" % (now.strftime("%c"), file_name, line_number, plc_tag, e))
app_log.error("Save Error: %s line %s tag %s %s" % (file_name, line_number, plc_tag, e))
checkErrorLog = True


Expand All @@ -186,7 +195,7 @@ def process_line_load(line, line_number, file_name):
ret = comm.Write(plc_tag, dp_value)

if ret.Status != "Success":
log.write("%s Load Error: %s line %s tag %s %s\n" % (now.strftime("%c"), file_name, line_number, plc_tag, ret.Status))
app_log.error("Load Error: %s line %s tag %s %s" % (file_name, line_number, plc_tag, ret.Status))
checkErrorLog = True


Expand All @@ -198,15 +207,15 @@ def process_line_verification(line, line_number, file_name):
try:
str_tag = str(read_tag(plc_tag))
except ValueError as e:
log.write("%s Verify Error: %s line %s tag %s %s\n" % (now.strftime("%c"), file_name, line_number, plc_tag, e))
app_log.error("Verify Error: %s line %s tag %s %s" % (file_name, line_number, plc_tag, e))
str_tag = "None"
checkErrorLog = True

# if it matches return true
if str_tag == dp_value:
return True
else:
log.write("%s Compare Error: %s line %s tag %s online value %s - dp value %s\n" % (now.strftime("%c"), file_name, line_number, plc_tag, str_tag, dp_value))
app_log.error("Compare Error: %s line %s tag %s online value %s - dp value %s" % (file_name, line_number, plc_tag, str_tag, dp_value))
checkErrorLog = True
return False

Expand Down Expand Up @@ -316,11 +325,29 @@ def print_header():
proceed_to_load = True

if proceed_to_load:
head, tail = os.path.split(dp_save_remote_path)
path = os.getcwd() + "\\" + tail
print_header()

# copy util root dir to remote path
if args.copy_to_remote_dir and os.path.exists(dp_save_remote_path):
if os.path.exists(path):
copytree2(path, dp_save_remote_path)
else:
print("Please save data preserve first with -c or --copy-to-local-dir argument")
sys.exit()
if args.copy_to_remote_dir and not os.path.exists(dp_save_remote_path):
if os.path.exists(path):
if not os.path.exists(dp_save_remote_path):
os.mkdir(dp_save_remote_path)
copytree2(path, dp_save_remote_path)
else:
print("Please save data preserve first with -c or --copy-to-local-dir argument")
sys.exit()

# ensure remote file names from ini have their save counterparts
# also checks if files exist on util root dir
for config_file in remote_files:
head, tail = os.path.split(dp_save_remote_path)
for config_file in remote_files:
path = os.getcwd() + "\\" + tail
root_file = Path(path + "\\" + config_file + "_save." + file_extension)
temp_file = Path(dp_save_remote_path + "\\" + config_file + "_save." + file_extension)
Expand All @@ -338,23 +365,16 @@ def print_header():
print("Loading data preserve...\n")
start = time.time()

for config_file in remote_files:
head, tail = os.path.split(dp_save_remote_path)
for config_file in remote_files:
path = os.getcwd() + "\\" + tail
if not os.path.exists(dp_save_remote_path):
os.mkdir(dp_save_remote_path)
# copy util root dir to remote path
if args.copy_to_remote_dir and os.path.exists(path):
copytree2(path, dp_save_remote_path)
load_verify_data_preserve(dp_save_remote_path, config_file)

for config_file in local_files:
load_verify_data_preserve(dp_save_local_path, config_file)

# copy folders from root to config path on load
for config_path in paths_on_load:
# make dir on util root dir
head, tail = os.path.split(config_path)
# make dir on util root dir
path = os.getcwd() + "\\" + tail
if not os.path.exists(path):
os.mkdir(path)
Expand All @@ -372,10 +392,8 @@ def print_header():
time.sleep(args.auto_close)
else:
input("Press Enter to exit...")
log.close()
else:
print("Exiting...")
log.close()
sys.exit()

if args.save:
Expand Down Expand Up @@ -433,7 +451,6 @@ def print_header():
time.sleep(args.auto_close)
else:
input("Press Enter to exit...")
log.close()

if args.verify:
print_header()
Expand Down Expand Up @@ -464,12 +481,10 @@ def print_header():
print("%s Time Elapsed" % (convert(end - start)))

if checkErrorLog:
#print("Check log.txt in root directory for errors!")
print("Check %s\\log.txt for errors!" % (os.getcwd()))

if args.auto_close:
print("Cmd prompt auto closing in %d seconds" % (args.auto_close))
time.sleep(args.auto_close)
else:
input("Press Enter to exit...")
log.close()

0 comments on commit f022acd

Please sign in to comment.