Add SIGUSR1 traceback (made by jimbo, committed by TPG)
[uccvend-vendserver.git] / TracebackPrinter.py
1 """
2 TracebackPrinter.py - Prints the current stack trace to a specified
3 file when SIGUSR1 is received.
4 ---
5 Author: James Arcus <jimbo@ucc.asn.au>
6 Based-On: python-traceback-signal
7           <https://github.com/angelcam/python-traceback-signal>
8 Author: Angelcam <dev@angelcam.com>
9 """
10
11 import os
12 import sys
13 import traceback
14 import signal
15
16 def print_traceback(wfd, frame):
17     msg = "Traceback signal received.\nTraceback (most recent call last):\n"
18     msg += ''.join(traceback.format_stack(frame))
19     os.write(wfd, msg)
20
21 def register_sigusr(wfd):
22     def sigusr_handler(_, frame):
23         # first param is which signal
24         print_traceback(wfd, frame)
25
26     signal.signal(signal.SIGUSR1, sigusr_handler)
27
28 def traceback_init(f):
29     wfd = os.open(f, os.O_WRONLY | os.O_APPEND | os.O_CREAT, 0o600)
30     register_sigusr(wfd)

UCC git Repository :: git.ucc.asn.au