forked from bargenson/docker-filebeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemux.py
31 lines (24 loc) · 904 Bytes
/
demux.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# This script is unused because filebeat has been configured to work with
# containers having a TTY and thus not needed docker logs demultiplexing.
# In case you need to demultiplex them, this is more or less what is
# needed.errrrr
import sys, struct
def process_header(data: bytes) -> int:
type = int(data[0])
length = struct.unpack_from(">I", data, 4)
return type, length[0]
while True:
h_data = sys.stdin.buffer.read(8)
if h_data:
type, body_len = process_header(h_data)
# sys.stdout.write(f"type {type}, len {body_len}")
# sys.stdout.flush()
b_data = sys.stdin.buffer.read(body_len)
if b_data and type == 1:
# stdout
sys.stdout.buffer.write(b_data)
sys.stdout.flush()
if b_data and type == 2:
# stderr
sys.stderr.buffer.write(b_data)
sys.stderr.flush()