diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7243ae5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.swp diff --git a/client/lib/engine/bin/SystemLib.py b/client/lib/engine/bin/SystemLib.py index 65476ae..b8449a5 100644 --- a/client/lib/engine/bin/SystemLib.py +++ b/client/lib/engine/bin/SystemLib.py @@ -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 \ No newline at end of file + return 0 diff --git a/client/lib/engine/bin/ogGlobals.py b/client/lib/engine/bin/ogGlobals.py new file mode 100644 index 0000000..0f13431 --- /dev/null +++ b/client/lib/engine/bin/ogGlobals.py @@ -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'