aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-11-17 22:26:16 +0800
committerAaron LI <aaronly.me@outlook.com>2016-11-17 22:26:16 +0800
commit133a406be588b32d61ea22498b2a880bf1645998 (patch)
treebab26e0f381345f1852ba111faa571be9f44c6ba
parent52bd308911c62d5f79b09479c651afe44f988a75 (diff)
downloadfg21sim-133a406be588b32d61ea22498b2a880bf1645998.tar.bz2
webui: WebSocketLogHandler: Support push to multiple websockets
Also change the pushed messages to have "action=push" and "subtype=log"
-rw-r--r--fg21sim/webui/handlers/log.py28
1 files changed, 15 insertions, 13 deletions
diff --git a/fg21sim/webui/handlers/log.py b/fg21sim/webui/handlers/log.py
index 0b457b5..8132497 100644
--- a/fg21sim/webui/handlers/log.py
+++ b/fg21sim/webui/handlers/log.py
@@ -15,33 +15,33 @@ import json
class WebSocketLogHandler(logging.Handler):
"""
- Send logging messages to the WebSocket as JSON-encoded string.
+ Push the logging messages to the client(s) through the WebSocket(s)
+ as JSON-encoded strings.
Parameters
----------
- websocket : `~tornado.websocket.WebSocketHandler`
- An `~tornado.websocket.WebSocketHandler` instance, which has
- the ``write_message()`` method that will be used to send the
- logging messages.
+ websockets : set of `~tornado.websocket.WebSocketHandler`
+ Set of opened websockets, through which the logging messages will
+ be pushed.
msg_type : str, optional
- Set the type of the sent back message, for easier processing
+ Set the type of the pushed logging messages, for easier handling
by the client.
NOTE
----
- The message sent through the WebSocket is a JSON-encoded string
- from a dictionary, e.g.,
+ The pushed logging message is a JSON-encoded string from a dictionary:
``{"type": self.msg_type,
- "action": "log",
+ "subtype": "log",
+ "action": "push",
"levelname": record.levelname,
"levelno": record.levelno,
"name": record.name,
"asctime": record.asctime,
"message": <formatted-message>}``
"""
- def __init__(self, websocket, msg_type=None):
+ def __init__(self, websockets, msg_type=None):
super().__init__()
- self.websocket = websocket
+ self.websockets = websockets
self.msg_type = msg_type
def emit(self, record):
@@ -49,13 +49,15 @@ class WebSocketLogHandler(logging.Handler):
message = self.format(record)
msg = json.dumps({
"type": self.msg_type,
- "action": "log",
+ "subtype": "log",
+ "action": "push",
"levelname": record.levelname,
"levelno": record.levelno,
"name": record.name,
"asctime": record.asctime,
"message": message,
})
- self.websocket.write_message(msg)
+ for ws in self.websockets:
+ ws.write_message(msg)
except Exception:
self.handleError(record)