Skip to content

Commit

Permalink
added secret thread_dump command in cli server for debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Fallen-Breath committed Mar 10, 2022
1 parent b4c862c commit ae1ad20
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions chatbridge/impl/cli/cli_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import threading
import time
import traceback

from chatbridge.core.config import ServerConfig
from chatbridge.core.network.basic import Address
Expand All @@ -17,6 +19,19 @@ class CLIServerConfig(ServerConfig):
CHAT_LOGGING_FILE = 'chat.log'


def thread_dump() -> str:
from sys import _current_frames
lines = []
name_map = dict([(thread.ident, thread.name) for thread in threading.enumerate()])
for thread_id, stack in _current_frames().items():
lines.append("# Thread {} ({})".format(name_map.get(thread_id, 'unknown'), thread_id))
for filename, lineno, name, line in traceback.extract_stack(stack):
lines.append(' File "{}", line {}, in {}'.format(filename, lineno, name))
if line:
lines.append(' {}'.format(line.strip()))
return '\n'.join(lines)


class CLIServer(ChatBridgeServer):
def on_chat(self, sender: str, content: ChatPayload):
if config.show_chat:
Expand Down Expand Up @@ -52,6 +67,8 @@ def console_loop(self):
elif text == 'debug off':
self.logger.set_debug_all(False)
self.logger.info('Debug logging off')
elif text == 'thread_dump':
self.logger.info(thread_dump())
else:
self.logger.info('stop": stop the server')
self.logger.info('stop <client_name>": stop a client')
Expand Down

0 comments on commit ae1ad20

Please sign in to comment.