Skip to content

Commit ab08b48

Browse files
authored
Merge pull request #47 from NCAR/hua-work-common
write_message()
2 parents 9a24ab2 + 2e51077 commit ab08b48

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "rda_python_common"
7-
version = "1.0.40"
7+
version = "1.0.41"
88
authors = [
99
{ name="Zaihua Ji", email="[email protected]" },
1010
]

src/rda_python_common/PgLOG.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -440,35 +440,42 @@ def pglog(msg, logact = MSGLOG):
440440
if not logact&NOTLOG:
441441
if logact&ERRLOG:
442442
if not PGLOG['ERRFILE']: PGLOG['ERRFILE'] = re.sub(r'.log$', '.err', PGLOG['LOGFILE'])
443-
try:
444-
ERR = open("{}/{}".format(PGLOG['LOGPATH'], PGLOG['ERRFILE']), 'a')
445-
except FileNotFoundError:
446-
ERR = open("error.log", 'a')
447-
ERR.write(f"Error File not found: {PGLOG['LOGPATH']}/{PGLOG['ERRFILE']}")
448-
ERR.write(msg)
449-
if not logact&(EMLALL|SKPTRC): ERR.write(get_call_trace())
450-
ERR.close()
443+
write_message(msg, f"{PGLOG['LOGPATH']}/{PGLOG['ERRFILE']}", logact)
451444
if logact&EXITLG:
452-
LOG = open("{}/{}".format(PGLOG['LOGPATH'], PGLOG['LOGFILE']), "a")
453-
LOG.write(cmdstr)
454-
LOG.close()
445+
write_message(cmdstr, f"{PGLOG['LOGPATH']}/{PGLOG['LOGFILE']}", logact)
455446
else:
456-
LOG = open("{}/{}".format(PGLOG['LOGPATH'], PGLOG['LOGFILE']), 'a')
457-
LOG.write(msg)
458-
LOG.close()
447+
write_message(msg, f"{PGLOG['LOGPATH']}/{PGLOG['LOGFILE']}", logact)
459448

460449
if not PGLOG['BCKGRND'] and logact&(ERRLOG|WARNLG):
461-
OUT = sys.stderr if logact&(ERRLOG|EXITLG) else sys.stdout
462-
if logact&BRKLIN: OUT.write("\n")
463-
if logact&SEPLIN: OUT.write(PGLOG['SEPLINE'])
464-
OUT.write(msg)
465-
450+
write_message(msg, None, logact)
466451

467452
if logact&EXITLG:
468453
pgexit(1)
469454
else:
470455
return (retmsg if retmsg else FAILURE)
471456

457+
#
458+
# write a log message
459+
#
460+
def write_message(msg, file, logact):
461+
462+
doclose = False
463+
errlog = logact&ERRLOG
464+
if file:
465+
try:
466+
OUT = open(file, 'a')
467+
doclose = True
468+
except FileNotFoundError:
469+
OUT = sys.stderr if logact&(ERRLOG|EXITLG) else sys.stdout
470+
OUT.write(f"Log File not found: {file}")
471+
else:
472+
OUT = sys.stderr if logact&(ERRLOG|EXITLG) else sys.stdout
473+
if logact&BRKLIN: OUT.write("\n")
474+
if logact&SEPLIN: OUT.write(PGLOG['SEPLINE'])
475+
OUT.write(msg)
476+
if errlog and not logact&(EMLALL|SKPTRC): OUT.write(get_call_trace())
477+
if doclose: OUT.close()
478+
472479
#
473480
# check and disconnet database before exit
474481
#

0 commit comments

Comments
 (0)