@@ -67,6 +67,7 @@ class PythonDaemon(MethodDispatcher):
6767 """
6868
6969 def __init__ (self , rx , tx ):
70+ self .log = logging .getLogger ("{0}.{1}" .format (self .__class__ .__module__ ,self .__class__ .__name__ ))
7071 self ._jsonrpc_stream_reader = JsonRpcStreamReader (rx )
7172 self ._jsonrpc_stream_writer = JsonRpcStreamWriter (tx )
7273 self ._endpoint = Endpoint (
@@ -78,10 +79,10 @@ def __getitem__(self, item):
7879 """Override getitem to fallback through multiple dispatchers."""
7980 if self ._shutdown and item != "exit" :
8081 # exit is the only allowed method during shutdown
81- log .debug ("Ignoring non-exit method during shutdown: %s" , item )
82+ self . log .debug ("Ignoring non-exit method during shutdown: %s" , item )
8283 raise KeyError
8384
84- log .info ("Execute rpc method %s" , item )
85+ self . log .info ("Execute rpc method %s" , item )
8586 return super ().__getitem__ (item )
8687
8788 def start (self ):
@@ -91,7 +92,7 @@ def start(self):
9192
9293 def m_ping (self , data ):
9394 """ping & pong (check if daemon is alive)."""
94- log .info ("pinged with %s" , data )
95+ self . log .info ("pinged with %s" , data )
9596 return {"pong" : data }
9697
9798 def _execute_and_capture_output (self , func ):
@@ -110,7 +111,7 @@ def _execute_and_capture_output(self, func):
110111 return output
111112
112113 def close (self ):
113- log .info ("Closing rpc channel" )
114+ self . log .info ("Closing rpc channel" )
114115 self ._shutdown = True
115116 self ._endpoint .shutdown ()
116117 self ._jsonrpc_stream_reader .close ()
@@ -122,18 +123,18 @@ def m_exit(self, **_kwargs):
122123 @error_decorator
123124 def m_exec_file (self , file_name , args = [], cwd = None , env = None ):
124125 args = [] if args is None else args
125- log .info ("Exec file %s with args %s" , file_name , args )
126+ self . log .info ("Exec file %s with args %s" , file_name , args )
126127
127128 def exec_file ():
128- log .info ("execute file %s" , file_name )
129+ self . log .info ("execute file %s" , file_name )
129130 runpy .run_path (file_name , globals ())
130131
131132 with change_exec_context (args , cwd , env ):
132133 return self ._execute_and_capture_output (exec_file )
133134
134135 @error_decorator
135136 def m_exec_code (self , code ):
136- log .info ("Exec code %s" , code )
137+ self . log .info ("Exec code %s" , code )
137138
138139 def exec_code ():
139140 eval (code , globals ())
@@ -144,21 +145,21 @@ def exec_code():
144145 def m_exec_file_observable (self , file_name , args = [], cwd = None , env = None ):
145146 args = [] if args is None else args
146147 old_argv , sys .argv = sys .argv , ["" ] + args
147- log .info ("Exec file (observale) %s with args %s" , file_name , args )
148+ self . log .info ("Exec file (observale) %s with args %s" , file_name , args )
148149
149150 with change_exec_context (args , cwd , env ):
150151 runpy .run_path (file_name , globals ())
151152
152153 @error_decorator
153154 def m_exec_module (self , module_name , args = [], cwd = None , env = None ):
154155 args = [] if args is None else args
155- log .info ("Exec module %s with args %s" , module_name , args )
156+ self . log .info ("Exec module %s with args %s" , module_name , args )
156157 if args [- 1 ] == "--version" :
157158 return self ._get_module_version (module_name , args )
158159
159160 def exec_module ():
160161
161- log .info ("execute module %s" , module_name )
162+ self . log .info ("execute module %s" , module_name )
162163 runpy .run_module (module_name , globals (), run_name = "__main__" )
163164
164165 with change_exec_context (args , cwd , env ):
@@ -167,7 +168,7 @@ def exec_module():
167168 @error_decorator
168169 def m_exec_module_observable (self , module_name , args = None , cwd = None , env = None ):
169170 args = [] if args is None else args
170- log .info ("Exec module (observable) %s with args %s" , module_name , args )
171+ self . log .info ("Exec module (observable) %s with args %s" , module_name , args )
171172
172173 with change_exec_context (args , cwd , env ):
173174 runpy .run_module (module_name , globals (), run_name = "__main__" )
@@ -184,7 +185,7 @@ def _get_module_version(self, module_name, args):
184185 module_name = args [0 ]
185186
186187 try :
187- log .info ("getting module_version %s" , module_name )
188+ self . log .info ("getting module_version %s" , module_name )
188189 m = importlib .import_module (module_name )
189190 return {"stdout" : m .__version__ }
190191 except Exception :
@@ -209,7 +210,7 @@ def m_is_module_installed(self, module_name=None):
209210 return {"exists" : False }
210211
211212 @classmethod
212- def start_daemon (cls ):
213+ def start_daemon (cls , logging_queue_handler = None ):
213214 """ Starts the daemon. """
214215 if not issubclass (cls , PythonDaemon ):
215216 raise ValueError ("Handler class must be an instance of PythonDaemon" )
@@ -224,4 +225,7 @@ def on_write_stderr(output):
224225 stdin , stdout = get_io_buffers ()
225226 server = cls (stdin , stdout )
226227 redirect_output (on_write_stdout , on_write_stderr )
228+ # Set up the queue handler that'll send log messages over to the client.
229+ if logging_queue_handler is not None :
230+ logging_queue_handler .set_server (server )
227231 server .start ()
0 commit comments