Skip to content
Snippets Groups Projects
Commit a844c7ee authored by Stefano Borini's avatar Stefano Borini
Browse files

Removed print statements. Using log file

parent 5d3abacb
No related branches found
No related tags found
1 merge request!108Introduced log file option
...@@ -12,6 +12,8 @@ from .factory_registry_plugin import FactoryRegistryPlugin ...@@ -12,6 +12,8 @@ from .factory_registry_plugin import FactoryRegistryPlugin
from .core_evaluation_driver import CoreEvaluationDriver from .core_evaluation_driver import CoreEvaluationDriver
from .core_mco_driver import CoreMCODriver from .core_mco_driver import CoreMCODriver
log = logging.getLogger(__name__)
class BDSSApplication(Application): class BDSSApplication(Application):
"""Main application for the BDSS. """Main application for the BDSS.
...@@ -47,7 +49,7 @@ class BDSSApplication(Application): ...@@ -47,7 +49,7 @@ class BDSSApplication(Application):
try: try:
mgr.map(functools.partial(_import_extensions, plugins)) mgr.map(functools.partial(_import_extensions, plugins))
except NoMatches: except NoMatches:
logging.info("No extensions found") log.info("No extensions found")
super(BDSSApplication, self).__init__(plugins=plugins) super(BDSSApplication, self).__init__(plugins=plugins)
...@@ -56,7 +58,7 @@ def _import_extensions(plugins, ext): ...@@ -56,7 +58,7 @@ def _import_extensions(plugins, ext):
"""Service routine extracted for testing. """Service routine extracted for testing.
Imports the extension in the plugins argument. Imports the extension in the plugins argument.
""" """
logging.info("Found extension {}".format(ext.obj)) log.info("Found extension {}".format(ext.obj))
plugins.append(ext.obj) plugins.append(ext.obj)
...@@ -65,7 +67,8 @@ def _load_failure_callback(plugins, manager, entry_point, exception): ...@@ -65,7 +67,8 @@ def _load_failure_callback(plugins, manager, entry_point, exception):
Reports failure to load a module through stevedore, using the Reports failure to load a module through stevedore, using the
on_load_failure_callback option. on_load_failure_callback option.
""" """
logging.error( log.error(
"Unable to load plugin {}. Exception: {}. Message: {}".format( "Unable to load plugin {}. Exception: {}. Message: {}".format(
entry_point, exception.__class__.__name__, exception) entry_point, exception.__class__.__name__, exception)
) )
log.exception(exception)
import logging
import click import click
from ..bdss_application import BDSSApplication from ..bdss_application import BDSSApplication
...@@ -10,12 +11,26 @@ push_exception_handler(reraise_exceptions=True) ...@@ -10,12 +11,26 @@ push_exception_handler(reraise_exceptions=True)
@click.command() @click.command()
@click.option("--evaluate", is_flag=True) @click.option("--evaluate", is_flag=True)
@click.option("--logfile",
type=click.Path(exists=False),
help="If specified, the log filename. "
" If unspecified, the log will be written to stdout.")
@click.argument('workflow_filepath', type=click.Path(exists=True)) @click.argument('workflow_filepath', type=click.Path(exists=True))
def run(evaluate, workflow_filepath): def run(evaluate, logfile, workflow_filepath):
application = BDSSApplication( logging_config = {}
evaluate=evaluate, if logfile is not None:
workflow_filepath=workflow_filepath logging_config["filename"] = logfile
)
application.run() logging.basicConfig(**logging_config)
log = logging.getLogger(__name__)
try:
application = BDSSApplication(
evaluate=evaluate,
workflow_filepath=workflow_filepath
)
application.run()
except Exception as e:
log.exception(e)
from __future__ import print_function
import sys import sys
import logging import logging
...@@ -14,6 +12,8 @@ from .io.workflow_reader import ( ...@@ -14,6 +12,8 @@ from .io.workflow_reader import (
CORE_EVALUATION_DRIVER_ID = plugin_id("core", "CoreEvaluationDriver") CORE_EVALUATION_DRIVER_ID = plugin_id("core", "CoreEvaluationDriver")
log = logging.getLogger(__name__)
class CoreEvaluationDriver(BaseCoreDriver): class CoreEvaluationDriver(BaseCoreDriver):
"""Main plugin that handles the execution of the MCO """Main plugin that handles the execution of the MCO
...@@ -26,12 +26,12 @@ class CoreEvaluationDriver(BaseCoreDriver): ...@@ -26,12 +26,12 @@ class CoreEvaluationDriver(BaseCoreDriver):
try: try:
workflow = self.workflow workflow = self.workflow
except (InvalidVersionException, InvalidFileException) as e: except (InvalidVersionException, InvalidFileException) as e:
print(str(e), file=sys.stderr) log.exception(e)
sys.exit(1) sys.exit(1)
mco_model = workflow.mco mco_model = workflow.mco
if mco_model is None: if mco_model is None:
print("No MCO defined. Nothing to do. Exiting.") log.info("No MCO defined. Nothing to do. Exiting.")
sys.exit(0) sys.exit(0)
mco_factory = mco_model.factory mco_factory = mco_model.factory
...@@ -105,7 +105,7 @@ def _compute_layer_results(environment_data_values, ...@@ -105,7 +105,7 @@ def _compute_layer_results(environment_data_values,
in_slots) in_slots)
# execute data source, passing only relevant data values. # execute data source, passing only relevant data values.
logging.info("Evaluating for Data Source {}".format( log.info("Evaluating for Data Source {}".format(
factory.name)) factory.name))
res = evaluator.run(model, passed_data_values) res = evaluator.run(model, passed_data_values)
...@@ -118,7 +118,7 @@ def _compute_layer_results(environment_data_values, ...@@ -118,7 +118,7 @@ def _compute_layer_results(environment_data_values,
len(res), factory.name, len(out_slots) len(res), factory.name, len(out_slots)
) )
logging.error(error_txt) log.error(error_txt)
raise RuntimeError(error_txt) raise RuntimeError(error_txt)
if len(res) != len(model.output_slot_names): if len(res) != len(model.output_slot_names):
...@@ -133,7 +133,7 @@ def _compute_layer_results(environment_data_values, ...@@ -133,7 +133,7 @@ def _compute_layer_results(environment_data_values,
len(model.output_slot_names) len(model.output_slot_names)
) )
logging.error(error_txt) log.error(error_txt)
raise RuntimeError(error_txt) raise RuntimeError(error_txt)
# At this point, the returned data values are unnamed. # At this point, the returned data values are unnamed.
...@@ -173,7 +173,7 @@ def _get_data_values_from_mco(model, communicator): ...@@ -173,7 +173,7 @@ def _get_data_values_from_mco(model, communicator):
" file is corrupted.").format( " file is corrupted.").format(
len(mco_data_values), len(model.parameters) len(mco_data_values), len(model.parameters)
) )
logging.error(error_txt) log.error(error_txt)
raise RuntimeError(error_txt) raise RuntimeError(error_txt)
# The data values obtained by the communicator are unnamed. # The data values obtained by the communicator are unnamed.
......
from __future__ import print_function
import sys import sys
import logging import logging
...@@ -44,12 +42,12 @@ class CoreMCODriver(BaseCoreDriver): ...@@ -44,12 +42,12 @@ class CoreMCODriver(BaseCoreDriver):
try: try:
workflow = self.workflow workflow = self.workflow
except (InvalidVersionException, InvalidFileException) as e: except (InvalidVersionException, InvalidFileException) as e:
print(str(e), file=sys.stderr) log.exception(e)
sys.exit(1) sys.exit(1)
mco_model = workflow.mco mco_model = workflow.mco
if mco_model is None: if mco_model is None:
print("No MCO defined. Nothing to do. Exiting.") log.info("No MCO defined. Nothing to do. Exiting.")
sys.exit(0) sys.exit(0)
mco_factory = mco_model.factory mco_factory = mco_model.factory
......
...@@ -53,7 +53,6 @@ class TestWorkflowWriter(unittest.TestCase): ...@@ -53,7 +53,6 @@ class TestWorkflowWriter(unittest.TestCase):
fp = StringIO() fp = StringIO()
wf = self._create_mock_workflow() wf = self._create_mock_workflow()
wfwriter.write(wf, fp) wfwriter.write(wf, fp)
print(fp.getvalue())
fp.seek(0) fp.seek(0)
wfreader = WorkflowReader(self.mock_registry) wfreader = WorkflowReader(self.mock_registry)
wf_result = wfreader.read(fp) wf_result = wfreader.read(fp)
......
...@@ -7,6 +7,8 @@ from force_bdss.core.input_slot_map import InputSlotMap ...@@ -7,6 +7,8 @@ from force_bdss.core.input_slot_map import InputSlotMap
from force_bdss.core.workflow import Workflow from force_bdss.core.workflow import Workflow
from ..factory_registry_plugin import IFactoryRegistryPlugin from ..factory_registry_plugin import IFactoryRegistryPlugin
log = logging.getLogger(__name__)
SUPPORTED_FILE_VERSIONS = ["1"] SUPPORTED_FILE_VERSIONS = ["1"]
...@@ -69,12 +71,12 @@ class WorkflowReader(HasStrictTraits): ...@@ -69,12 +71,12 @@ class WorkflowReader(HasStrictTraits):
try: try:
version = json_data["version"] version = json_data["version"]
except KeyError: except KeyError:
logging.error("File missing version information") log.error("File missing version information")
raise InvalidFileException("Corrupted input file, no version" raise InvalidFileException("Corrupted input file, no version"
" specified") " specified")
if version not in SUPPORTED_FILE_VERSIONS: if version not in SUPPORTED_FILE_VERSIONS:
logging.error( log.error(
"File contains version {} that is not in the " "File contains version {} that is not in the "
"list of supported versions {}".format( "list of supported versions {}".format(
version, SUPPORTED_FILE_VERSIONS) version, SUPPORTED_FILE_VERSIONS)
...@@ -92,7 +94,7 @@ class WorkflowReader(HasStrictTraits): ...@@ -92,7 +94,7 @@ class WorkflowReader(HasStrictTraits):
wf.notification_listeners[:] = \ wf.notification_listeners[:] = \
self._extract_notification_listeners(wf_data) self._extract_notification_listeners(wf_data)
except KeyError as e: except KeyError as e:
logging.exception("Could not read file {}".format(file)) log.exception("Could not read file {}".format(file))
raise InvalidFileException("Could not read file {}. " raise InvalidFileException("Could not read file {}. "
"Unable to find key {}".format(file, e)) "Unable to find key {}".format(file, e))
return wf return wf
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment