#!/usr/bin/env python # Copyright 2012 Hewlett-Packard Development Company, L.P. # Copyright 2013 OpenStack Foundation # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import logging import logging.config import os import signal import sys import threading import traceback def stack_dump_handler(signum, frame): signal.signal(signal.SIGUSR2, signal.SIG_IGN) log_str = "" threads = {} for t in threading.enumerate(): threads[t.ident] = t for thread_id, stack_frame in sys._current_frames().items(): thread = threads.get(thread_id) if thread: thread_name = thread.name else: thread_name = 'Unknown' label = '%s (%s)' % (thread_name, thread_id) log_str += "Thread: %s\n" % label log_str += "".join(traceback.format_stack(stack_frame)) log = logging.getLogger("nodepool.stack_dump") log.debug(log_str) signal.signal(signal.SIGUSR2, stack_dump_handler) class NodepoolApp(object): def __init__(self): self.args = None def setup_logging(self): if self.args.logconfig: fp = os.path.expanduser(self.args.logconfig) if not os.path.exists(fp): raise Exception("Unable to read logging config file at %s" % fp) logging.config.fileConfig(fp) else: logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s %(name)s: ' '%(message)s')