def do_some_logs_multiprocessing(again): bxilog.cleanup() bxilog.basicConfig(filename=FILENAME, filemode='a', level=bxilog.LOWEST) while again.value: bxilog.out("Doing a simple log: %s", again) time.sleep(0.1) bxilog.out("Termination requested. Exiting.")
def setUp(self): """Definition of the context. """ print("Whole unit test suite file outputs: %s" % FILENAME) bxilog.basicConfig(filename=FILENAME, filemode='a', level=bxilog.LOWEST)
def my_function(logfilename): # Since a fork() has been made, the logging module must be initialized bxilog.basicConfig(filename=logfilename, filemode='w') # Using the bxilog.multiprocessing_target decorator guarantees that # uncaught exception will be reported by the logging system # and that the logging system will be cleanup properly (and therefore # flushed). bxilog.out("In subprocess") bxilog.flush() # Test 1 : simple trace message bxilog.trace("A simple trace message") # Test 2 : simple error message bxilog.error("A simple error message") # Test 3 : catch warning error try: raise ValueError("An expected exception in first subprocess") except: bxilog.exception("Handling an exception in subprocess", level=bxilog.WARNING) # Test 4 : # This will be catched thanks to the bxilog.multiprocessing_target() decorator # Otherwise, the exception will appear on the standard error as usual raise ValueError("An unexpected exception in second subprocess")
def test_non_existing_dir(self): """Test logging into a non existing tmpdir - this should raise an error""" tmpdir = tempfile.mkdtemp(".bxilog", "test_") os.rmdir(tmpdir) name = os.path.join(tmpdir, 'dummy.bxilog') bxilog.basicConfig(filename=name) # Raise an error because filename provided to basicConfig doesn't exist self.assertRaises(bxierr.BXICError, bxilog.output, "One log on non-existent (deleted) directory: %s", name) bxilog.cleanup() self.assertFalse(os.path.exists(name))
def test_existing_file(self): """Test logging into an existing file""" fd, name = tempfile.mkstemp(".bxilog", "test_") print("Overriding file output to " "%s for %s.%s()" % (name, __name__, BXILogTest.test_existing_file.__name__)) self.assertEquals(os.stat(name).st_size, 0) os.close(fd) bxilog.basicConfig(filename=name) self._check_log_produced(name, bxilog.output, "One log on file: %s", name) os.remove(name) bxilog.cleanup()
def threads_in_process(again): global __LOOP_AGAIN__ __LOOP_AGAIN__ = True bxilog.cleanup() bxilog.basicConfig(filename=FILENAME, filemode='a', level=bxilog.LOWEST) threads = [] for i in xrange(multiprocessing.cpu_count()): thread = threading.Thread(target=do_some_logs_threading) bxilog.out("Starting new thread") thread.start() threads.append(thread) while again.value: time.sleep(0.05) bxilog.out("Requesting termination of %s threads", len(threads)) __LOOP_AGAIN__ = False for thread in threads: try: thread.join(5) except Error as e: bxilog.out("Exception: %s", e)
_LOGGER.warning("%s terminated with %s: %s", node, ret, task.node_buffer(node)) if task.num_timeout(): _LOGGER.warning("%d timeout reached: %s", task.num_timeout(), ", ".join(task.iter_keys_timeout())) rc = 1 return rc if "__main__" == __name__: import bxi.base.posless as argparse PARSER = argparse.ArgumentParser() PARSER.add_argument("--timeout", "-t", default=5, type=int) PARSER.add_argument("--loglevel", "-l", default=":debug,bxi:output", type=str) PARSER.add_argument("cmd", default="uname -r", type=str) PARSER.add_argument("--topo", type=str, default="") PARSER.add_argument("--nodes", "-w", type=str, default="") PARSER.add_argument("--no-remote", action="store_true", default=False) ARGS = PARSER.parse_args() bxilog.basicConfig(cfg_items=ARGS.loglevel) del ARGS.loglevel tasked(**vars(ARGS))
N[id] = n MIN[id] = min_ MAX[id] = max_ if __name__ == "__main__": if len(sys.argv) != 3: print("Usage: %s threads_nb timeout" % sys.argv[0]) exit(1) FILENAME.value = "/tmp/%s.log" % sys.argv[0] # print("Output file is %s" % FILENAME.value) bxilog.basicConfig(filename=FILENAME.value, append=True, level=bxilog.DEBUG) threads_nb = int(sys.argv[1]) timeout = int(sys.argv[2]) TOTAL = multiprocessing.Array('d', threads_nb, lock=False) N = multiprocessing.Array('I', threads_nb, lock=False) MIN = multiprocessing.Array('d', threads_nb, lock=False) MAX = multiprocessing.Array('d', threads_nb, lock=False) threads = [] for i in xrange(threads_nb): thread = multiprocessing.Process(target=logging_thread, args=(i, )) thread.start() threads.append(thread)
# -*- coding: utf-8 -*- ############################################################################### # Author: Pierre Vignéras <*****@*****.**> ############################################################################### # Copyright (C) 2013 Bull S. A. S. - All rights reserved # Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois # This is not Free or Open Source software. # Please contact Bull S. A. S. for details about its license. ############################################################################### """ Simple program for unit tests of BXI Log Python library. See testbxilog.py for details. """ import os, time, signal import bxi.base.log as bxilog import __main__ basename = os.path.basename(__main__.__file__) FILENAME = "%s.bxilog" % os.path.splitext(basename)[0] if __name__ == "__main__": bxilog.basicConfig(filename=FILENAME) bxilog.output("Logging a message") while True: bxilog.output("Waiting until killed") time.sleep(2.5)
#!/usr/bin/env python # -*- coding: utf-8 -*- ############################################################################### # Author: Pierre Vignéras <*****@*****.**> ############################################################################### # Copyright (C) 2013 Bull S. A. S. - All rights reserved # Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois # This is not Free or Open Source software. # Please contact Bull S. A. S. for details about its license. ############################################################################### """ Simple program for unit tests of BXI Log Python library. See testbxilog.py for details. """ import os import bxi.base.log as bxilog import __main__ basename = os.path.basename(__main__.__file__) FILENAME = "%s.bxilog" % os.path.splitext(basename)[0] if __name__ == "__main__": bxilog.basicConfig(filename=FILENAME, level=bxilog.TRACE) bxilog.output( "Will raise an exception without any try/except, log must catch it") raise ValueError("Normal: this must appears in the file")
############################################################################### # Copyright (C) 2013 Bull S. A. S. - All rights reserved # Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois # This is not Free or Open Source software. # Please contact Bull S. A. S. for details about its license. ############################################################################### """ Simple program for unit tests of BXI Log Python library. See testbxilog.py for details. """ import os, time import bxi.base.log as bxilog import __main__ basename = os.path.basename(__main__.__file__) FILENAME = "%s.bxilog" % os.path.splitext(basename)[0] if __name__ == "__main__": bxilog.basicConfig( filename=FILENAME, level=bxilog.LOWEST, ) bxilog.output("Logging a message") while True: bxilog.output("Waiting until killed") time.sleep(2.5)