11
11
import fcntl
12
12
import shlex
13
13
import logging
14
+ import sys
14
15
15
16
logging .getLogger ("werkzeug" ).setLevel (logging .ERROR )
16
17
20
21
app .config ["SECRET_KEY" ] = "secret!"
21
22
app .config ["fd" ] = None
22
23
app .config ["child_pid" ] = None
23
- socketio = SocketIO (app , logger = False , engineio_logger = False )
24
+ socketio = SocketIO (app )
24
25
25
26
26
27
def set_winsize (fd , row , col , xpix = 0 , ypix = 0 ):
28
+ logging .debug ("setting window size with termios" )
27
29
winsize = struct .pack ("HHHH" , row , col , xpix , ypix )
28
30
fcntl .ioctl (fd , termios .TIOCSWINSZ , winsize )
29
31
@@ -51,20 +53,21 @@ def pty_input(data):
51
53
terminal.
52
54
"""
53
55
if app .config ["fd" ]:
54
- # print("writing to pty : %s" % data["input"])
56
+ logging . debug ( "received input from browser : %s" % data ["input" ])
55
57
os .write (app .config ["fd" ], data ["input" ].encode ())
56
58
57
59
58
60
@socketio .on ("resize" , namespace = "/pty" )
59
61
def resize (data ):
60
62
if app .config ["fd" ]:
63
+ logging .debug (f"Resizing window to { data ['rows' ]} x{ data ['cols' ]} " )
61
64
set_winsize (app .config ["fd" ], data ["rows" ], data ["cols" ])
62
65
63
66
64
67
@socketio .on ("connect" , namespace = "/pty" )
65
68
def connect ():
66
69
"""new client connected"""
67
-
70
+ logging . info ( "new client connected" )
68
71
if app .config ["child_pid" ]:
69
72
# already started child process, don't start another
70
73
return
@@ -83,13 +86,13 @@ def connect():
83
86
app .config ["child_pid" ] = child_pid
84
87
set_winsize (fd , 50 , 50 )
85
88
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 (
88
91
f"starting background task with command `{ cmd } ` to continously read "
89
92
"and forward pty output to client"
90
93
)
91
94
socketio .start_background_task (target = read_and_forward_pty_output )
92
- print ("task started" )
95
+ logging . info ("task started" )
93
96
94
97
95
98
def main ():
@@ -120,8 +123,16 @@ def main():
120
123
if args .version :
121
124
print (__version__ )
122
125
exit (0 )
123
- print (f"serving on http://127.0.0.1:{ args .port } " )
124
126
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 } " )
125
136
socketio .run (app , debug = args .debug , port = args .port , host = args .host )
126
137
127
138
0 commit comments