Example #1
0
def test_log_output_is_captured():
    """Make sure the dev sees the log generated by the library if a
    test fails.
    """

    old_stdout = sys.stdout
    sys.stdout = StringIO()
    try:
        log.reset()

        feedev.testmod()
        sys.stdout.seek(0)
        stdout = sys.stdout.read()
    finally:
        sys.stdout = old_stdout
        log.reset()

    print "stdout was: ", stdout    # already goes to nose again
    assert 'Updating feed #1' in stdout
Example #2
0
    def handle(self, *args, **options):

        # setup the log according to the options requested
        loglevel = options.get('level')
        if loglevel:
            try:
                loglevel = logging._levelNames[loglevel.upper()]
            except KeyError:
                raise CommandError('invalid value for --level: %s' % loglevel)

        logfile = options.get('log')
        if logfile:
            filehandler = logging.FileHandler(logfile)
            filehandler.setFormatter(logging.Formatter(
                '%(asctime)-18s %(name)-12s: %(levelname)-8s %(message)s',
                '%m-%d-%y %H:%M:%S'))
            handlers=[filehandler]
        else:
            handlers = None

        if loglevel or handlers:
            log.reset(level=loglevel, handlers=handlers)


        # determine which daemons are available, and which one to run
        # TODO: better handle duplicate names
        named_daemons = {}
        unnamed_daemons = []
        for addin in addins.get_addins():
            if isinstance(addin, base_daemon):
                if addin.name:
                    named_daemons[addin.name] = addin
                else:
                    unnamed_daemons.append(addin)

        args = list(args)   # make mutable

        if len(named_daemons) + len(unnamed_daemons) == 1:
            # If only one daemon is available, we can start it pretty
            # much right away.
            if named_daemons and args and named_daemons.keys()[0] == args[0]:
                # If the user specified the only existing daemon by
                # name, we make sure that name is not passed along
                # as a subargument to the daemon itself.
                args.remove(args[0])
            daemon_to_start = unnamed_daemons and \
                unnamed_daemons[0] or \
                named_daemons.values()[0]
        # Otherwise, we have to determine which one the use wants to start
        else:
            s_daemon_list = ''
            for name in named_daemons.keys():
                s_daemon_list += '    %s\n' % name
            if unnamed_daemons:
                s_daemon_list += 'Additionally, %d unnamed daemons are '+\
                    'installed.\n' % len(unnamed_daemons)

            if len(args) == 0:
                raise CommandError('multiple daemons are installed, you '
                                   'need to specify which one to run:\n%s' %
                                        s_daemon_list)
            else:
                daemon_name = args[0]
                args.remove(args[0])
                if not daemon_name in named_daemons:
                    raise CommandError('"%s" is not a known daemon, '+
                                       'installed are:\n%s' % s_daemon_list)
                else:
                    daemon_to_start = named_daemons[daemon_name]

        # fork off as daemon, if requested
        if options.get('daemonize'):
            if os.name != 'posix':
                raise CommandError('--daemonize not supported on this platform')
            daemon.daemonize()

        try:
            # If daemon threads make trouble (http://bugs.python.org/issue1856),
            # we can always disable it. It's there for convenience's sake,
            # but our daemons/threads have a stop-flag mechanism that should
            # work just fine as well.
            # TODO: parse the rest of the args, pass along as args/options
            daemon_to_start.start(daemon=True)
            while daemon_to_start.isAlive():
                # join with a timeout, so KeyboardInterrupts get through
                daemon_to_start.join(DEFAULT_LOOP_SLEEP)
        except KeyboardInterrupt:
            daemon_to_start.stop()