def setup_ocaml_loggers(): """Set up logging for ocaml: will provide hooks to Python logging into Ocaml's Nlog loggers.""" def logging_nofileinfo(logger,level,msg): # Calls from Ocaml don't provide filename and line number information. # Need to make sure that logging doesn't attempt to get these, so we # disable this temporarily: logging._srcfile_org = logging._srcfile logging._srcfile = None # then do the call logger.log(level, msg) #logger.log(50,"About to flush") #...and restore. (Not Python-thread safe). logging._srcfile = logging._srcfile_org import ocaml # ocaml logger names are ending in '.ocaml' to distinguish in logs # whether the message comes from Python or Ocaml # These are the names of the ocaml loggers as seen from Python (and the user) pythonloggernames = ["nmesh.ocaml", "nfem.ocaml", "ocaml.ocaml"] loggers = {} for loggername in pythonloggernames: loggers[loggername] = logging.getLogger(loggername) # Can't create these closures in a loop, so need to have one per logger def ocamllognmesh(level, msg): logging_nofileinfo(loggers["nmesh.ocaml"], level, msg) # First make sure a logger has been registered with Nlog. # (This will -- if the logger doesn't exist -- create one.) log.debug("Calling ocaml.nlog_setupLogger for nmesh.ocaml") ocaml.nlog_setupLogger("nmesh.ocaml") loggers['nmesh.ocaml'].debug("Test 1") # Now that the logger exists, we can modify it's default handler: ocaml.nlog_register_handler("nmesh.ocaml", ocamllognmesh) loggers['nmesh.ocaml'].debug("Test 2") def ocamllognfem(level, msg): logging_nofileinfo(loggers["nfem.ocaml"], level, msg) ocaml.nlog_setupLogger("nfem.ocaml") ocaml.nlog_register_handler("nfem.ocaml", ocamllognfem) def ocamllogocaml(level, msg): logging_nofileinfo(loggers["ocaml.ocaml"], level, msg) ocaml.nlog_setupLogger("ocaml.ocaml") ocaml.nlog_register_handler("ocaml.ocaml", ocamllogocaml) log.debug("Done registering loggers nmesh.ocaml, nfem.ocaml " "and ocaml.ocaml for ocaml (available in ocaml via Nlog)")
def setup_ocaml_loggers(): """Set up logging for ocaml: will provide hooks to Python logging into Ocaml's Nlog loggers.""" def logging_nofileinfo(logger, level, msg): # Calls from Ocaml don't provide filename and line number information. # Need to make sure that logging doesn't attempt to get these, so we # disable this temporarily: logging._srcfile_org = logging._srcfile logging._srcfile = None # then do the call logger.log(level, msg) #logger.log(50,"About to flush") #...and restore. (Not Python-thread safe). logging._srcfile = logging._srcfile_org import ocaml # ocaml logger names are ending in '.ocaml' to distinguish in logs # whether the message comes from Python or Ocaml # These are the names of the ocaml loggers as seen from Python (and the user) pythonloggernames = ["nmesh.ocaml", "nfem.ocaml", "ocaml.ocaml"] loggers = {} for loggername in pythonloggernames: loggers[loggername] = logging.getLogger(loggername) # Can't create these closures in a loop, so need to have one per logger def ocamllognmesh(level, msg): logging_nofileinfo(loggers["nmesh.ocaml"], level, msg) # First make sure a logger has been registered with Nlog. # (This will -- if the logger doesn't exist -- create one.) log.debug("Calling ocaml.nlog_setupLogger for nmesh.ocaml") ocaml.nlog_setupLogger("nmesh.ocaml") loggers['nmesh.ocaml'].debug("Test 1") # Now that the logger exists, we can modify it's default handler: ocaml.nlog_register_handler("nmesh.ocaml", ocamllognmesh) loggers['nmesh.ocaml'].debug("Test 2") def ocamllognfem(level, msg): logging_nofileinfo(loggers["nfem.ocaml"], level, msg) ocaml.nlog_setupLogger("nfem.ocaml") ocaml.nlog_register_handler("nfem.ocaml", ocamllognfem) def ocamllogocaml(level, msg): logging_nofileinfo(loggers["ocaml.ocaml"], level, msg) ocaml.nlog_setupLogger("ocaml.ocaml") ocaml.nlog_register_handler("ocaml.ocaml", ocamllogocaml) log.debug("Done registering loggers nmesh.ocaml, nfem.ocaml " "and ocaml.ocaml for ocaml (available in ocaml via Nlog)")
import nmag import ocaml ocaml.nlog_setupLogger('test') def mypythonhandler(level,msg): print "mypythonhandler: %d '%s'" % (level,msg) #nmag.setup.pyfeatures.to_ocaml_features() #print "YYYYYYYYY About to call ergister handler" #print "YYYYYYYYY get_feature:",ocaml.snippets_get_feature('nmag','slavelog') ocaml.nlog_register_handler('test',mypythonhandler) #print "ZZZ get_feature:",ocaml.snippets_get_feature('nmag','slavelog') #print message on all nodes ocaml.nlog_log_mpi('test',51,'High priority Test message from all nodes') print "Setting loglevel to 20" ocaml.nlog_setLogLevel('test',20) ocaml.nlog_log_mpi('test',10,'Debug Test message from all nodes (should not be shown)') print "Setting loglevel to 10" ocaml.nlog_setLogLevel('test',10) ocaml.nlog_log_mpi('test',10,'Debug Test2 message from all nodes') nmag.ipython()