diff --git a/madgraph/iolibs/template_files/mg7/madevent.py b/madgraph/iolibs/template_files/mg7/madevent.py index 2ab4b2f4c..6114a4871 100644 --- a/madgraph/iolibs/template_files/mg7/madevent.py +++ b/madgraph/iolibs/template_files/mg7/madevent.py @@ -81,7 +81,16 @@ ) print() -logger = logging.getLogger("madevent7") +logger = logging.getLogger("madgraph7") +LOG_LEVEL_MAP = { + ms.Logger.LogLevel.level_debug: logging.DEBUG, + ms.Logger.LogLevel.level_info: logging.INFO, + ms.Logger.LogLevel.level_warning: logging.WARNING, + ms.Logger.LogLevel.level_error: logging.ERROR, +} +def ms_log_handler(level: ms.Logger.LogLevel, message: str): + logger.log(LOG_LEVEL_MAP[level], message) +ms.Logger.set_log_handler(ms_log_handler) def get_start_time(): @@ -1784,7 +1793,7 @@ def _off(value) -> bool: _TOOL_LOGGING_READY = False -def _setup_tool_logging(): +def _setup_logging(): """Make the reused madevent tool drivers' progress visible on screen. The drivers already narrate what they do through logger.info / @@ -1804,7 +1813,7 @@ def _setup_tool_logging(): handler = logging.StreamHandler(sys.stdout) handler.setFormatter(formatter) handler._mg7_tool_handler = True - for name in ("madgraph", "madevent", "cmdprint"): + for name in ("madgraph", "madevent", "cmdprint", "madgraph7"): lg = logging.getLogger(name) if not any(getattr(h, "_mg7_tool_handler", False) for h in lg.handlers): lg.addHandler(handler) @@ -1838,9 +1847,6 @@ def run_selected_tools(switch, process) -> None: process.run_path, ", ".join(sorted(active))) return - # surface the reused madevent drivers' own progress messages on screen - _setup_tool_logging() - run_name = os.path.basename(os.path.dirname(os.path.abspath(lhe_path))) run_dir = os.path.dirname(os.path.abspath(lhe_path)) @@ -2266,10 +2272,13 @@ def force_lhe_output_if_needed(switch) -> None: def main() -> None: + _setup_logging() + parser = argparse.ArgumentParser() parser.add_argument("-f", action="store_false", dest="ask_edit_cards") args = parser.parse_args() switch = {} + if args.ask_edit_cards: switch = ask_edit_cards() force_lhe_output_if_needed(switch) diff --git a/madspace/include/madspace/driver/logger.hpp b/madspace/include/madspace/driver/logger.hpp index 93ada928b..70789e233 100644 --- a/madspace/include/madspace/driver/logger.hpp +++ b/madspace/include/madspace/driver/logger.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "madspace/util.hpp" @@ -34,12 +35,14 @@ class Logger { println("[ERROR] {}", message); break; } + std::cout << std::flush; } static void debug(const std::string& message) { log(level_debug, message); } static void info(const std::string& message) { log(level_info, message); } static void warning(const std::string& message) { log(level_warning, message); } static void error(const std::string& message) { log(level_error, message); } static void set_log_handler(LogHandlerFunc func) { _log_handler = func; } + static void clear_log_handler() { _log_handler = std::nullopt; } private: static inline std::optional _log_handler = std::nullopt; diff --git a/madspace/src/python/madspace.cpp b/madspace/src/python/madspace.cpp index 035b19588..72c029e7c 100644 --- a/madspace/src/python/madspace.cpp +++ b/madspace/src/python/madspace.cpp @@ -1764,7 +1764,13 @@ PYBIND11_MODULE(_madspace_py, m) { .def_static("info", &Logger::info, py::arg("message")) .def_static("warning", &Logger::warning, py::arg("message")) .def_static("error", &Logger::error, py::arg("message")) - .def_static("set_log_handler", &Logger::set_log_handler, py::arg("func")); + .def_static("set_log_handler", &Logger::set_log_handler, py::arg("func")) + .def_static("clear_log_handler", &Logger::clear_log_handler); + + // prevent memory error due to static lifetime of log handler + py::module_::import("atexit").attr("register")(py::cpp_function([]() { + Logger::clear_log_handler(); + })); m.def( "initialize_vegas_grid",