diff --git a/schaincli/cli.py b/schaincli/cli.py index 9ec418f..5b03de2 100644 --- a/schaincli/cli.py +++ b/schaincli/cli.py @@ -8,7 +8,7 @@ save_stdout = sys.stdout sys.stdout = open('trash', 'w') from multiprocessing import cpu_count from schaincli import templates -from schainpy import controller_api +from schainpy.controller import Project from schainpy.model import Operation, ProcessingUnit from schainpy.utils import log from importlib import import_module @@ -150,18 +150,8 @@ def test(): def runFromXML(filename): - controller = controller_api.ControllerThread() + controller = Project() if not controller.readXml(filename): return - - plotterObj = controller.useExternalPlotter() - controller.start() - plotterObj.start() - - cliLogger("Finishing all processes") - - controller.join(5) - - cliLogger("End of script") return diff --git a/schainpy/controller.py b/schainpy/controller.py index 431296b..1921268 100644 --- a/schainpy/controller.py +++ b/schainpy/controller.py @@ -4,6 +4,12 @@ Created on September , 2012 ''' import sys + +# save_stdout = sys.stdout +# logToFile = newLogger() +# sys.stdout = logToFile +# sys.stderr = logToFile + import ast import datetime import traceback @@ -13,6 +19,7 @@ from multiprocessing import Process, Queue, cpu_count import schainpy import schainpy.admin +from schainpy.utils.log import logToFile from xml.etree.ElementTree import ElementTree, Element, SubElement, tostring from xml.dom import minidom @@ -904,7 +911,6 @@ class ReadUnitConf(ProcUnitConf): self.endTime = opConfObj.getParameterValue('endTime') class Project(Process): - id = None name = None description = None @@ -916,16 +922,17 @@ class Project(Process): plotterQueue = None - def __init__(self, plotter_queue=None): + def __init__(self, plotter_queue=None, logfile=None): Process.__init__(self) self.id = None self.name = None self.description = None - + if logfile is not None: + logToFile(logfile) self.plotterQueue = plotter_queue self.procUnitConfObjDict = {} - + def __getNewId(self): idList = self.procUnitConfObjDict.keys() @@ -1321,5 +1328,3 @@ class Project(Process): for procKey in keyList: procUnitConfObj = self.procUnitConfObjDict[procKey] procUnitConfObj.close() - - print "Process finished" diff --git a/schainpy/utils/log.py b/schainpy/utils/log.py index 8dec425..5a08497 100644 --- a/schainpy/utils/log.py +++ b/schainpy/utils/log.py @@ -1,4 +1,4 @@ -""". +""" SCHAINPY - LOG Simple helper for log standarization Usage: @@ -14,7 +14,8 @@ SCHAINPY - LOG [NEVER GONNA] - give you up with color red as background and white as foreground. """ - +import os +import sys import click def warning(message): @@ -37,3 +38,22 @@ def makelogger(topic, bg='reset', fg='reset'): click.echo(click.style('[{}] - '.format(topic.upper()) + message, bg=bg, fg=fg)) return func + +class LoggerForFile(): + def __init__(self, filename): + self.old_stdout=sys.stdout + cwd = os.getcwd() + self.log_file = open(os.path.join(cwd, filename), 'w+') + def write(self, text): + text = text.rstrip() + if not text: + return + self.log_file.write(text + '\n') + self.old_stdout.write(text + '\n') + def flush(self): + self.old_stdout.flush() + +def logToFile(filename='log.log'): + logger = LoggerForFile(filename) + sys.stdout = logger +