refs #1059 add ogEcho(). Implement ogGlobals

pull/1/head
Natalia Serrano 2024-10-28 17:50:50 +01:00
parent 5532fb9eec
commit eaee15b7d4
3 changed files with 59 additions and 31 deletions

2
.gitignore vendored 100644
View File

@ -0,0 +1,2 @@
__pycache__/
*.swp

View File

@ -1,9 +1,11 @@
import subprocess
import datetime
from zoneinfo import ZoneInfo
import sys
import os
import shutil
import ogGlobals
from DiskLib import *
from CacheLib import *
from StringLib import *
@ -14,40 +16,53 @@ print (">>>>>>>>>>>>>>>>>>>> Load ", __name__, " <<<<<<<<<<<<<<<<<<<<<<")
#OG_ERR_REPO, OG_ERR_NOTOS, OG_ERR_NOGPT, OG_ERR_OUTOFLIMIT, OG_ERR_IMAGE, OG_ERR_CACHE
#OGLOGSESSION, OGLOGCOMMAND, OGLOGFILE, OG_ERR_LOCKED, OG_ERR_PARTITION, OG_ERR_FORMAT, OG_ERR_NOTEXEC, OG_ERR_NOTFOUND
def ogEcho(*args):
# Variables locales
CONT = 1
LOGS = ""
LOGLEVEL = ""
DATETIME = ""
def _logtype2logfile (t):
if 'log' == t: return ogGlobals.OGLOGFILE
elif 'command' == t: return ogGlobals.OGLOGCOMMAND
elif 'session' == t: return ogGlobals.OGLOGSESSION
else: raise Exception (f'unknown log type ({t})')
#/**
# ogEcho [str_logtype ...] [str_loglevel] "str_message" ...
#@brief Muestra mensajes en consola y lo registra en fichero de incidencias.
#@param str_logtype tipo de registro de incidencias ("log", "command", "session")
#@param str_loglevel nivel de registro de incidencias ("info", "warning", "error")
#@param str_message mensaje (puede recibir más de 1 parámetro.
#@return Mensaje mostrado.
#*/
## zero or more logtypes can be specified
## zero or one loglevel can be specified
## ogEcho ([], None, msg)
## ogEcho ('log', None, msg)
## ogEcho ('session', None, msg)
## ogEcho (['log', 'session'], None, msg)
## ogEcho ([], None, 'info', msg)
## ogEcho ('log', None, 'info', msg)
## ogEcho ('session', None, 'info', msg)
## ogEcho (['log', 'session'], 'info', msg)
def ogEcho (logtypes, loglevel, msg):
logfiles = []
if type (logtypes) is list:
for l in logtypes:
logfiles.append (_logtype2logfile (l))
else: ## string
logfiles.append (_logtype2logfile (logtypes))
# Selección de ficheros de registro de incidencias.
while CONT:
arg = args.pop(0).lower()
if arg == "log":
LOGS += " " + OGLOGFILE
elif arg == "command":
LOGS += " " + OGLOGCOMMAND
elif arg == "session":
LOGS += " " + OGLOGSESSION
else:
CONT = 0
if loglevel is None or 'help' == loglevel:
for f in logfiles:
with open (f, 'a') as fd:
fd.write (msg + '\n')
return
# Selección del nivel de registro (opcional).
arg = args.pop(0).lower()
if arg == "help":
pass
elif arg == "info" or arg == "warning" or arg == "error":
LOGLEVEL = arg
if 'info' == loglevel or 'warning' == loglevel or 'error' == loglevel:
DATETIME = datetime.datetime.now(ZoneInfo(ogGlobals.TZ)).strftime("%F %T %Z")
if ogGlobals.DEBUG.lower() != "no":
logfiles.append (ogGlobals.OGLOGFILE)
if LOGLEVEL:
DATETIME = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Registrar mensajes en fichero de log si la depuración no está desactivada.
if DEBUG.lower() != "no":
LOGS += " " + OGLOGFILE
subprocess.call(f"logger -t OpenGnsys {LOGLEVEL} {DATETIME} {' '.join(args)}", shell=True)
for f in logfiles:
with open (f, 'a') as fd:
fd.write (f"OpenGnsys {loglevel} {DATETIME} {msg}\n")
else:
print(' '.join(args))
raise Exception (f'unknown loglevel ({loglevel})')
def ogExecAndLog(*args):
# Variables locales
@ -298,4 +313,4 @@ def ogIsVirtualMachine():
if "KVM" in output or "VirtualBox" in output:
return 1
else:
return 0
return 0

View File

@ -0,0 +1,11 @@
#!/usr/bin/python3
#OPENGNSYS='/opt/opengnsys'
OPENGNSYS='/tmp/opengnsys'
OGLOG=f'{OPENGNSYS}/log'
OGLOGFILE=f'{OGLOG}/192.168.42.42' ## TODO
OGLOGCOMMAND='/tmp/command.log'
OGLOGSESSION='/tmp/session.log'
DEBUG='yes'
TZ='Europe/Madrid'