...
1
2
3
4 package stdio
5
6 import (
7 "os"
8
9 "github.com/Microsoft/hcsshim/internal/guest/transport"
10 "github.com/pkg/errors"
11 "github.com/sirupsen/logrus"
12 )
13
14
15
16 type ConnectionSettings struct {
17 StdIn *uint32
18 StdOut *uint32
19 StdErr *uint32
20 }
21
22 type logConnection struct {
23 con transport.Connection
24 port uint32
25 }
26
27 func (lc *logConnection) Read(b []byte) (int, error) {
28 return lc.con.Read(b)
29 }
30
31 func (lc *logConnection) Write(b []byte) (int, error) {
32 return lc.con.Write(b)
33 }
34
35 func (lc *logConnection) Close() error {
36 logrus.WithFields(logrus.Fields{
37 "port": lc.port,
38 }).Debug("opengcs::logConnection::Close - closing connection")
39
40 return lc.con.Close()
41 }
42
43 func (lc *logConnection) CloseRead() error {
44 logrus.WithFields(logrus.Fields{
45 "port": lc.port,
46 }).Debug("opengcs::logConnection::Close - closing read connection")
47
48 return lc.con.CloseRead()
49 }
50
51 func (lc *logConnection) CloseWrite() error {
52 logrus.WithFields(logrus.Fields{
53 "port": lc.port,
54 }).Debug("opengcs::logConnection::Close - closing write connection")
55
56 return lc.con.CloseWrite()
57 }
58
59 func (lc *logConnection) File() (*os.File, error) {
60 return lc.con.File()
61 }
62
63 var _ = (transport.Connection)(&logConnection{})
64
65
66
67
68 func Connect(tport transport.Transport, settings ConnectionSettings) (_ *ConnectionSet, err error) {
69 connSet := &ConnectionSet{}
70 defer func() {
71 if err != nil {
72 connSet.Close()
73 }
74 }()
75 if settings.StdIn != nil {
76 c, err := tport.Dial(*settings.StdIn)
77 if err != nil {
78 return nil, errors.Wrap(err, "failed creating stdin Connection")
79 }
80 connSet.In = &logConnection{
81 con: c,
82 port: *settings.StdIn,
83 }
84 }
85 if settings.StdOut != nil {
86 c, err := tport.Dial(*settings.StdOut)
87 if err != nil {
88 return nil, errors.Wrap(err, "failed creating stdout Connection")
89 }
90 connSet.Out = &logConnection{
91 con: c,
92 port: *settings.StdOut,
93 }
94 }
95 if settings.StdErr != nil {
96 c, err := tport.Dial(*settings.StdErr)
97 if err != nil {
98 return nil, errors.Wrap(err, "failed creating stderr Connection")
99 }
100 connSet.Err = &logConnection{
101 con: c,
102 port: *settings.StdErr,
103 }
104 }
105 return connSet, nil
106 }
107
View as plain text