Skip to content

Commit f3e37e2

Browse files
committed
log enhance
1 parent 144de46 commit f3e37e2

File tree

2 files changed

+56
-20
lines changed

2 files changed

+56
-20
lines changed

log.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
)
7+
8+
var flags = log.Ldate | log.Lshortfile
9+
var logger = log.New(os.Stdout, "", flags)
10+
var Gdebug = false
11+
12+
func EnableDebug() {
13+
Gdebug = true
14+
log.SetFlags(log.Ldate | log.Lmicroseconds)
15+
}
16+
17+
func DebugPrint(fmt string, args ...interface{}) {
18+
if Gdebug {
19+
log.Printf("[\033[34;1m DEBUG \033[0m] "+fmt, args...)
20+
}
21+
}
22+
23+
func InfoPrint(fmt string, args ...interface{}) {
24+
log.Printf("[\033[37;1m INFO \033[0m] "+fmt, args...)
25+
}
26+
27+
func ErrorPrint(fmt string, args ...interface{}) {
28+
log.Printf("[\033[31;1m ERROR \033[0m] "+fmt, args...)
29+
}
30+
31+
func WarnPrint(fmt string, args ...interface{}) {
32+
log.Printf("[\033[33;1mWARNING\033[0m] "+fmt, args...)
33+
}
34+
35+
func FatalPrint(fmt string, args ...interface{}) {
36+
log.Fatalf("[\033[31;1m FATAL \033[0m] "+fmt, args...)
37+
}

main.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ func main() {
6565
defer cleanup()
6666
if err != nil {
6767
fmt.Fprintln(sess, err)
68-
log.Println(err)
68+
ErrorPrint(err.Error())
6969
}
7070
sess.Exit(int(status))
7171
})
7272

73-
log.Printf("starting ssh server on %s...", *bindAddress)
73+
InfoPrint("starting ssh server on %s...", *bindAddress)
7474
log.Fatal(ssh.ListenAndServe(*bindAddress, nil))
7575
}
7676

7777
func imageExistsLocally(ctx context.Context, imageName string, cli *client.Client) bool {
7878
images, err := cli.ImageList(ctx, image.ListOptions{})
7979
if err != nil {
80-
log.Printf("Error listing images: %v", err)
80+
ErrorPrint("Error listing images: %v", err)
8181
return false
8282
}
8383

@@ -106,19 +106,19 @@ func waitForContainerReady(ctx context.Context, sess ssh.Session, cli *client.Cl
106106
// If a health check is defined, ensure it's healthy
107107
if containerJSON.State.Health != nil {
108108
if containerJSON.State.Health.Status == "healthy" {
109-
log.Println("Container is running and healthy.")
109+
InfoPrint("Container %s is running and healthy.", containerID)
110110
return nil
111111
} else if containerJSON.State.Health.Status == "unhealthy" {
112112
return fmt.Errorf("container is unhealthy")
113113
}
114114
} else {
115-
log.Println("Container is running.")
115+
InfoPrint("Container %s is running.", containerID)
116116
return nil
117117
}
118118
}
119119

120120
sess.Write([]byte("Waiting for container to become ready...\n"))
121-
log.Printf("Waiting for container %s to be ready...", containerID)
121+
InfoPrint("Waiting for container %s to be ready...", containerID)
122122
time.Sleep(2 * time.Second)
123123
}
124124

@@ -133,7 +133,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
133133
cleanup = func() {}
134134
ctx := context.Background()
135135

136-
log.Printf("User: %s", sess.User())
136+
InfoPrint("Image: %s", sess.User())
137137
cImage := sess.User()
138138

139139
networkingConfig := network.NetworkingConfig{}
@@ -143,36 +143,36 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
143143
// Variant: "minimal",
144144
}
145145
if imageExistsLocally(ctx, cImage, docker) != true {
146-
sess.Write([]byte("Fetching Image from repository .."))
146+
sess.Write([]byte("Fetching Image from repository ..\n"))
147147
reader, pullerr := docker.ImagePull(ctx, cImage, image.PullOptions{})
148148
if pullerr != nil {
149-
sess.Write([]byte("Unable to pull requested image" + string(pullerr.Error()) + "\n"))
150-
log.Printf("Error pulling image: %v", pullerr)
149+
sess.Write([]byte("Unable to pull requested image [" + cImage + "]: [" + string(pullerr.Error()) + "]\n"))
150+
ErrorPrint("Unable to pull requested image [%s]: %v", cImage, pullerr)
151151
cleanup = func() {}
152152
return
153153
}
154154
defer reader.Close()
155155
if _, err := io.Copy(os.Stdout, reader); err != nil {
156-
log.Printf("Error reading pull output: %v", pullerr)
156+
ErrorPrint("Unable to read pull output: %v", pullerr)
157157
}
158158
}
159159

160160
resp, err := docker.ContainerCreate(ctx, cfg, hostcfg, &networkingConfig, &platformConfig, "")
161161
if err != nil {
162-
log.Printf("Unable to create container: %v", err)
162+
ErrorPrint("Unable to create container: %v", err)
163163
return
164164
}
165-
log.Printf("Created container: %s", resp.ID)
165+
InfoPrint("Created container: %s", resp.ID)
166166
cleanup = func() {
167167
docker.ContainerRemove(ctx, resp.ID, container.RemoveOptions{})
168168
}
169169
startErr := docker.ContainerStart(ctx, resp.ID, container.StartOptions{})
170170
if startErr != nil {
171-
log.Printf("Unable to start container: %v", err)
171+
ErrorPrint("Unable to start container: %v", err)
172172
sess.Write([]byte("Unable to pull requested image" + string(startErr.Error()) + "\n"))
173173
return
174174
}
175-
log.Printf("Wait for container %s to be ready", resp.ID)
175+
InfoPrint("Wait for container %s to be ready", resp.ID)
176176
err = waitForContainerReady(ctx, sess, docker, resp.ID, 30*time.Second)
177177
if err != nil {
178178
sess.Write([]byte("container failed to become ready"))
@@ -189,7 +189,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
189189
if err != nil {
190190
return
191191
}
192-
log.Printf("Attaching container: %s", resp.ID)
192+
InfoPrint("Attaching container: %s", resp.ID)
193193
stream, err := docker.ContainerExecAttach(ctx, execResp.ID, container.ExecStartOptions{
194194
Tty: true,
195195
})
@@ -220,7 +220,7 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
220220
Width: uint(win.Width),
221221
})
222222
if err != nil {
223-
log.Println(err)
223+
ErrorPrint(err.Error())
224224
break
225225
}
226226
}
@@ -229,12 +229,11 @@ func dockerRun(cfg *container.Config, hostcfg *container.HostConfig, sess ssh.Se
229229
select {
230230
case <-outputErr:
231231
cleanup = func() {
232-
log.Printf("Killing container: %s", resp.ID)
232+
InfoPrint("Killing container: %s", resp.ID)
233233
docker.ContainerKill(ctx, resp.ID, "9")
234-
log.Printf("Removing container: %s", resp.ID)
234+
InfoPrint("Removing container: %s", resp.ID)
235235
docker.ContainerRemove(ctx, resp.ID, container.RemoveOptions{})
236236
}
237-
log.Println("Exit..")
238237
return
239238
}
240239
}

0 commit comments

Comments
 (0)