1111import fcntl
1212import shlex
1313import logging
14+ import sys
1415
1516logging .getLogger ("werkzeug" ).setLevel (logging .ERROR )
1617
2021app .config ["SECRET_KEY" ] = "secret!"
2122app .config ["fd" ] = None
2223app .config ["child_pid" ] = None
23- socketio = SocketIO (app , logger = False , engineio_logger = False )
24+ socketio = SocketIO (app )
2425
2526
2627def set_winsize (fd , row , col , xpix = 0 , ypix = 0 ):
28+ logging .debug ("setting window size with termios" )
2729 winsize = struct .pack ("HHHH" , row , col , xpix , ypix )
2830 fcntl .ioctl (fd , termios .TIOCSWINSZ , winsize )
2931
@@ -51,20 +53,21 @@ def pty_input(data):
5153 terminal.
5254 """
5355 if app .config ["fd" ]:
54- # print("writing to pty : %s" % data["input"])
56+ logging . debug ( "received input from browser : %s" % data ["input" ])
5557 os .write (app .config ["fd" ], data ["input" ].encode ())
5658
5759
5860@socketio .on ("resize" , namespace = "/pty" )
5961def resize (data ):
6062 if app .config ["fd" ]:
63+ logging .debug (f"Resizing window to { data ['rows' ]} x{ data ['cols' ]} " )
6164 set_winsize (app .config ["fd" ], data ["rows" ], data ["cols" ])
6265
6366
6467@socketio .on ("connect" , namespace = "/pty" )
6568def connect ():
6669 """new client connected"""
67-
70+ logging . info ( "new client connected" )
6871 if app .config ["child_pid" ]:
6972 # already started child process, don't start another
7073 return
@@ -83,13 +86,13 @@ def connect():
8386 app .config ["child_pid" ] = child_pid
8487 set_winsize (fd , 50 , 50 )
8588 cmd = " " .join (shlex .quote (c ) for c in app .config ["cmd" ])
86- print ("child pid is" , child_pid )
87- print (
89+ logging . info ("child pid is " + child_pid )
90+ logging . info (
8891 f"starting background task with command `{ cmd } ` to continously read "
8992 "and forward pty output to client"
9093 )
9194 socketio .start_background_task (target = read_and_forward_pty_output )
92- print ("task started" )
95+ logging . info ("task started" )
9396
9497
9598def main ():
@@ -120,8 +123,16 @@ def main():
120123 if args .version :
121124 print (__version__ )
122125 exit (0 )
123- print (f"serving on http://127.0.0.1:{ args .port } " )
124126 app .config ["cmd" ] = [args .command ] + shlex .split (args .cmd_args )
127+ green = "\033 [92m"
128+ end = "\033 [0m"
129+ log_format = green + "pyxtermjs > " + end + "%(levelname)s (%(funcName)s:%(lineno)s) %(message)s"
130+ logging .basicConfig (
131+ format = log_format ,
132+ stream = sys .stdout ,
133+ level = logging .DEBUG if args .debug else logging .INFO ,
134+ )
135+ logging .info (f"serving on http://127.0.0.1:{ args .port } " )
125136 socketio .run (app , debug = args .debug , port = args .port , host = args .host )
126137
127138
0 commit comments