Browse code

Initial steps to fix Issue #936

Use utils.Errorf instead of utils.Debugf

Karl Grzeszczak authored on 2013/10/08 16:54:47
Showing 10 changed files
... ...
@@ -95,6 +95,7 @@ Joseph Anthony Pasquale Holsten <joseph@josephholsten.com>
95 95
 Julien Barbier <write0@gmail.com>
96 96
 Jérôme Petazzoni <jerome.petazzoni@dotcloud.com>
97 97
 Karan Lyons <karan@karanlyons.com>
98
+Karl Grzeszczak <karl@karlgrz.com>
98 99
 Kawsar Saiyeed <kawsar.saiyeed@projiris.com>
99 100
 Keli Hu <dev@keli.hu>
100 101
 Ken Cochrane <kencochrane@gmail.com>
... ...
@@ -69,9 +69,12 @@ func httpError(w http.ResponseWriter, err error) {
69 69
 		statusCode = http.StatusUnauthorized
70 70
 	} else if strings.Contains(err.Error(), "hasn't been activated") {
71 71
 		statusCode = http.StatusForbidden
72
-	}
73
-	utils.Debugf("[error %d] %s", statusCode, err)
74
-	http.Error(w, err.Error(), statusCode)
72
+	}	
73
+	
74
+	if err != nil {
75
+		utils.Errorf("HTTP Error: statusCode=%d %s", statusCode, err.Error())
76
+		http.Error(w, err.Error(), statusCode)		
77
+	}	
75 78
 }
76 79
 
77 80
 func writeJSON(w http.ResponseWriter, code int, v interface{}) error {
... ...
@@ -102,7 +105,7 @@ func getBoolParam(value string) (bool, error) {
102 102
 func matchesContentType(contentType, expectedType string) bool {
103 103
 	mimetype, _, err := mime.ParseMediaType(contentType)
104 104
 	if err != nil {
105
-		utils.Debugf("Error parsing media type: %s error: %s", contentType, err.Error())
105
+		utils.Errorf("Error parsing media type: %s error: %s", contentType, err.Error())
106 106
 	}
107 107
 	return err == nil && mimetype == expectedType
108 108
 }
... ...
@@ -147,7 +150,7 @@ func getContainersExport(srv *Server, version float64, w http.ResponseWriter, r
147 147
 	name := vars["name"]
148 148
 
149 149
 	if err := srv.ContainerExport(name, w); err != nil {
150
-		utils.Debugf("%s", err)
150
+		utils.Errorf("%s", err)
151 151
 		return err
152 152
 	}
153 153
 	return nil
... ...
@@ -192,7 +195,7 @@ func getEvents(srv *Server, version float64, w http.ResponseWriter, r *http.Requ
192 192
 		_, err = wf.Write(b)
193 193
 		if err != nil {
194 194
 			// On error, evict the listener
195
-			utils.Debugf("%s", err)
195
+			utils.Errorf("%s", err)
196 196
 			srv.Lock()
197 197
 			delete(srv.listeners, r.RemoteAddr)
198 198
 			srv.Unlock()
... ...
@@ -347,7 +350,7 @@ func postCommit(srv *Server, version float64, w http.ResponseWriter, r *http.Req
347 347
 	}
348 348
 	config := &Config{}
349 349
 	if err := json.NewDecoder(r.Body).Decode(config); err != nil {
350
-		utils.Debugf("%s", err)
350
+		utils.Errorf("%s", err)
351 351
 	}
352 352
 	repo := r.Form.Get("repo")
353 353
 	tag := r.Form.Get("tag")
... ...
@@ -792,7 +795,7 @@ func wsContainersAttach(srv *Server, version float64, w http.ResponseWriter, r *
792 792
 		defer ws.Close()
793 793
 
794 794
 		if err := srv.ContainerAttach(name, logs, stream, stdin, stdout, stderr, ws, ws, ws); err != nil {
795
-			utils.Debugf("Error: %s", err)
795
+			utils.Errorf("Error: %s", err)
796 796
 		}
797 797
 	})
798 798
 	h.ServeHTTP(w, r)
... ...
@@ -938,7 +941,7 @@ func postContainersCopy(srv *Server, version float64, w http.ResponseWriter, r *
938 938
 	}
939 939
 
940 940
 	if err := srv.ContainerCopy(name, copyData.Resource, w); err != nil {
941
-		utils.Debugf("%s", err.Error())
941
+		utils.Errorf("%s", err.Error())
942 942
 		return err
943 943
 	}
944 944
 	return nil
... ...
@@ -983,7 +986,7 @@ func makeHttpHandler(srv *Server, logging bool, localMethod string, localRoute s
983 983
 		}
984 984
 
985 985
 		if err := handlerFunc(srv, version, w, r, mux.Vars(r)); err != nil {
986
-			utils.Debugf("Error: %s", err)
986
+			utils.Errorf("Error: %s", err)
987 987
 			httpError(w, err)
988 988
 		}
989 989
 	}
... ...
@@ -176,7 +176,7 @@ func (b *buildFile) CmdEnv(args string) error {
176 176
 func (b *buildFile) CmdCmd(args string) error {
177 177
 	var cmd []string
178 178
 	if err := json.Unmarshal([]byte(args), &cmd); err != nil {
179
-		utils.Debugf("Error unmarshalling: %s, setting cmd to /bin/sh -c", err)
179
+		utils.Errorf("Error unmarshalling: %s, setting cmd to /bin/sh -c", err)
180 180
 		cmd = []string{"/bin/sh", "-c", args}
181 181
 	}
182 182
 	if err := b.commit("", cmd, fmt.Sprintf("CMD %v", cmd)); err != nil {
... ...
@@ -296,7 +296,7 @@ func (b *buildFile) addContext(container *Container, orig, dest string) error {
296 296
 		}
297 297
 		// First try to unpack the source as an archive
298 298
 	} else if err := UntarPath(origPath, destPath); err != nil {
299
-		utils.Debugf("Couldn't untar %s to %s: %s", origPath, destPath, err)
299
+		utils.Errorf("Couldn't untar %s to %s: %s", origPath, destPath, err)
300 300
 		// If that fails, just copy it as a regular file
301 301
 		if err := os.MkdirAll(path.Dir(destPath), 0755); err != nil {
302 302
 			return err
... ...
@@ -418,7 +418,7 @@ func (cli *DockerCli) CmdVersion(args ...string) error {
418 418
 	var out APIVersion
419 419
 	err = json.Unmarshal(body, &out)
420 420
 	if err != nil {
421
-		utils.Debugf("Error unmarshal: body: %s, err: %s\n", body, err)
421
+		utils.Errorf("Error unmarshal: body: %s, err: %s\n", body, err)
422 422
 		return err
423 423
 	}
424 424
 	if out.Version != "" {
... ...
@@ -1533,7 +1533,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
1533 1533
 	if config.AttachStdin || config.AttachStdout || config.AttachStderr {
1534 1534
 		if config.Tty {
1535 1535
 			if err := cli.monitorTtySize(runResult.ID); err != nil {
1536
-				utils.Debugf("Error monitoring TTY size: %s\n", err)
1536
+				utils.Errorf("Error monitoring TTY size: %s\n", err)
1537 1537
 			}
1538 1538
 		}
1539 1539
 
... ...
@@ -1798,11 +1798,11 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.Rea
1798 1798
 		}
1799 1799
 		if tcpc, ok := rwc.(*net.TCPConn); ok {
1800 1800
 			if err := tcpc.CloseWrite(); err != nil {
1801
-				utils.Debugf("Couldn't send EOF: %s\n", err)
1801
+				utils.Errorf("Couldn't send EOF: %s\n", err)
1802 1802
 			}
1803 1803
 		} else if unixc, ok := rwc.(*net.UnixConn); ok {
1804 1804
 			if err := unixc.CloseWrite(); err != nil {
1805
-				utils.Debugf("Couldn't send EOF: %s\n", err)
1805
+				utils.Errorf("Couldn't send EOF: %s\n", err)
1806 1806
 			}
1807 1807
 		}
1808 1808
 		// Discard errors due to pipe interruption
... ...
@@ -1811,14 +1811,14 @@ func (cli *DockerCli) hijack(method, path string, setRawTerminal bool, in io.Rea
1811 1811
 
1812 1812
 	if stdout != nil {
1813 1813
 		if err := <-receiveStdout; err != nil {
1814
-			utils.Debugf("Error receiveStdout: %s", err)
1814
+			utils.Errorf("Error receiveStdout: %s", err)
1815 1815
 			return err
1816 1816
 		}
1817 1817
 	}
1818 1818
 
1819 1819
 	if !cli.isTerminal {
1820 1820
 		if err := <-sendStdin; err != nil {
1821
-			utils.Debugf("Error sendStdin: %s", err)
1821
+			utils.Errorf("Error sendStdin: %s", err)
1822 1822
 			return err
1823 1823
 		}
1824 1824
 	}
... ...
@@ -1832,7 +1832,7 @@ func (cli *DockerCli) getTtySize() (int, int) {
1832 1832
 	}
1833 1833
 	ws, err := term.GetWinsize(cli.terminalFd)
1834 1834
 	if err != nil {
1835
-		utils.Debugf("Error getting size: %s", err)
1835
+		utils.Errorf("Error getting size: %s", err)
1836 1836
 		if ws == nil {
1837 1837
 			return 0, 0
1838 1838
 		}
... ...
@@ -1849,7 +1849,7 @@ func (cli *DockerCli) resizeTty(id string) {
1849 1849
 	v.Set("h", strconv.Itoa(height))
1850 1850
 	v.Set("w", strconv.Itoa(width))
1851 1851
 	if _, _, err := cli.call("POST", "/containers/"+id+"/resize?"+v.Encode(), nil); err != nil {
1852
-		utils.Debugf("Error resize: %s", err)
1852
+		utils.Errorf("Error resize: %s", err)
1853 1853
 	}
1854 1854
 }
1855 1855
 
... ...
@@ -461,7 +461,7 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
461 461
 					_, err = io.Copy(cStdin, stdin)
462 462
 				}
463 463
 				if err != nil {
464
-					utils.Debugf("[error] attach stdin: %s\n", err)
464
+					utils.Errorf("[error] attach stdin: %s\n", err)
465 465
 				}
466 466
 				// Discard error, expecting pipe error
467 467
 				errors <- nil
... ...
@@ -486,7 +486,7 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
486 486
 				}
487 487
 				_, err := io.Copy(stdout, cStdout)
488 488
 				if err != nil {
489
-					utils.Debugf("[error] attach stdout: %s\n", err)
489
+					utils.Errorf("[error] attach stdout: %s\n", err)
490 490
 				}
491 491
 				errors <- err
492 492
 			}()
... ...
@@ -498,7 +498,7 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
498 498
 			}
499 499
 
500 500
 			if cStdout, err := container.StdoutPipe(); err != nil {
501
-				utils.Debugf("Error stdout pipe")
501
+				utils.Errorf("Error stdout pipe")
502 502
 			} else {
503 503
 				io.Copy(&utils.NopWriter{}, cStdout)
504 504
 			}
... ...
@@ -522,7 +522,7 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
522 522
 				}
523 523
 				_, err := io.Copy(stderr, cStderr)
524 524
 				if err != nil {
525
-					utils.Debugf("[error] attach stderr: %s\n", err)
525
+					utils.Errorf("[error] attach stderr: %s\n", err)
526 526
 				}
527 527
 				errors <- err
528 528
 			}()
... ...
@@ -534,7 +534,7 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
534 534
 			}
535 535
 
536 536
 			if cStderr, err := container.StderrPipe(); err != nil {
537
-				utils.Debugf("Error stdout pipe")
537
+				utils.Errorf("Error stdout pipe")
538 538
 			} else {
539 539
 				io.Copy(&utils.NopWriter{}, cStderr)
540 540
 			}
... ...
@@ -553,7 +553,7 @@ func (container *Container) Attach(stdin io.ReadCloser, stdinCloser io.Closer, s
553 553
 		for i := 0; i < nJobs; i += 1 {
554 554
 			utils.Debugf("Waiting for job %d/%d\n", i+1, nJobs)
555 555
 			if err := <-errors; err != nil {
556
-				utils.Debugf("Job %d returned error %s. Aborting all jobs\n", i+1, err)
556
+				utils.Errorf("Job %d returned error %s. Aborting all jobs\n", i+1, err)
557 557
 				return err
558 558
 			}
559 559
 			utils.Debugf("Job %d completed successfully\n", i+1)
... ...
@@ -958,12 +958,12 @@ func (container *Container) monitor(hostConfig *HostConfig) {
958 958
 	// If the command does not exists, try to wait via lxc
959 959
 	if container.cmd == nil {
960 960
 		if err := container.waitLxc(); err != nil {
961
-			utils.Debugf("%s: Process: %s", container.ID, err)
961
+			utils.Errorf("%s: Process: %s", container.ID, err)
962 962
 		}
963 963
 	} else {
964 964
 		if err := container.cmd.Wait(); err != nil {
965 965
 			// Discard the error as any signals or non 0 returns will generate an error
966
-			utils.Debugf("%s: Process: %s", container.ID, err)
966
+			utils.Errorf("%s: Process: %s", container.ID, err)
967 967
 		}
968 968
 	}
969 969
 	utils.Debugf("Process finished")
... ...
@@ -984,19 +984,19 @@ func (container *Container) monitor(hostConfig *HostConfig) {
984 984
 	container.releaseNetwork()
985 985
 	if container.Config.OpenStdin {
986 986
 		if err := container.stdin.Close(); err != nil {
987
-			utils.Debugf("%s: Error close stdin: %s", container.ID, err)
987
+			utils.Errorf("%s: Error close stdin: %s", container.ID, err)
988 988
 		}
989 989
 	}
990 990
 	if err := container.stdout.CloseWriters(); err != nil {
991
-		utils.Debugf("%s: Error close stdout: %s", container.ID, err)
991
+		utils.Errorf("%s: Error close stdout: %s", container.ID, err)
992 992
 	}
993 993
 	if err := container.stderr.CloseWriters(); err != nil {
994
-		utils.Debugf("%s: Error close stderr: %s", container.ID, err)
994
+		utils.Errorf("%s: Error close stderr: %s", container.ID, err)
995 995
 	}
996 996
 
997 997
 	if container.ptyMaster != nil {
998 998
 		if err := container.ptyMaster.Close(); err != nil {
999
-			utils.Debugf("%s: Error closing Pty master: %s", container.ID, err)
999
+			utils.Errorf("%s: Error closing Pty master: %s", container.ID, err)
1000 1000
 		}
1001 1001
 	}
1002 1002
 
... ...
@@ -12,7 +12,7 @@ import (
12 12
 
13 13
 func Unmount(target string) error {
14 14
 	if err := exec.Command("auplink", target, "flush").Run(); err != nil {
15
-		utils.Debugf("[warning]: couldn't run auplink before unmount: %s", err)
15
+		utils.Errorf("[warning]: couldn't run auplink before unmount: %s", err)
16 16
 	}
17 17
 	if err := syscall.Unmount(target, 0); err != nil {
18 18
 		return err
... ...
@@ -103,7 +103,7 @@ func (proxy *TCPProxy) Run() {
103 103
 	for {
104 104
 		client, err := proxy.listener.Accept()
105 105
 		if err != nil {
106
-			utils.Debugf("Stopping proxy on tcp/%v for tcp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error())
106
+			utils.Errorf("Stopping proxy on tcp/%v for tcp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error())
107 107
 			return
108 108
 		}
109 109
 		go proxy.clientLoop(client.(*net.TCPConn), quit)
... ...
@@ -205,7 +205,7 @@ func (proxy *UDPProxy) Run() {
205 205
 			// NOTE: Apparently ReadFrom doesn't return
206 206
 			// ECONNREFUSED like Read do (see comment in
207 207
 			// UDPProxy.replyLoop)
208
-			utils.Debugf("Stopping proxy on udp/%v for udp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error())
208
+			utils.Errorf("Stopping proxy on udp/%v for udp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error())
209 209
 			break
210 210
 		}
211 211
 
... ...
@@ -235,7 +235,7 @@ func (runtime *Runtime) restore() error {
235 235
 			fmt.Printf("\b%c", wheel[i%4])
236 236
 		}
237 237
 		if err != nil {
238
-			utils.Debugf("Failed to load container %v: %v", id, err)
238
+			utils.Errorf("Failed to load container %v: %v", id, err)
239 239
 			continue
240 240
 		}
241 241
 		utils.Debugf("Loaded container %v", container.ID)
... ...
@@ -429,7 +429,7 @@ func (srv *Server) pullImage(r *registry.Registry, out io.Writer, imgID, endpoin
429 429
 
430 430
 		// ensure no two downloads of the same layer happen at the same time
431 431
 		if err := srv.poolAdd("pull", "layer:"+id); err != nil {
432
-			utils.Debugf("Image (id: %s) pull is already running, skipping: %v", id, err)
432
+			utils.Errorf("Image (id: %s) pull is already running, skipping: %v", id, err)
433 433
 			return nil
434 434
 		}
435 435
 		defer srv.poolRemove("pull", "layer:"+id)
... ...
@@ -478,7 +478,7 @@ func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, localName
478 478
 	utils.Debugf("Retrieving the tag list")
479 479
 	tagsList, err := r.GetRemoteTags(repoData.Endpoints, remoteName, repoData.Tokens)
480 480
 	if err != nil {
481
-		utils.Debugf("%v", err)
481
+		utils.Errorf("%v", err)
482 482
 		return err
483 483
 	}
484 484
 
... ...
@@ -526,7 +526,7 @@ func (srv *Server) pullRepository(r *registry.Registry, out io.Writer, localName
526 526
 
527 527
 			// ensure no two downloads of the same image happen at the same time
528 528
 			if err := srv.poolAdd("pull", "img:"+img.ID); err != nil {
529
-				utils.Debugf("Image (id: %s) pull is already running, skipping: %v", img.ID, err)
529
+				utils.Errorf("Image (id: %s) pull is already running, skipping: %v", img.ID, err)
530 530
 				if parallel {
531 531
 					errors <- nil
532 532
 				}
... ...
@@ -1191,25 +1191,25 @@ func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, std
1191 1191
 		cLog, err := container.ReadLog("json")
1192 1192
 		if err != nil && os.IsNotExist(err) {
1193 1193
 			// Legacy logs
1194
-			utils.Debugf("Old logs format")
1194
+			utils.Errorf("Old logs format")
1195 1195
 			if stdout {
1196 1196
 				cLog, err := container.ReadLog("stdout")
1197 1197
 				if err != nil {
1198
-					utils.Debugf("Error reading logs (stdout): %s", err)
1198
+					utils.Errorf("Error reading logs (stdout): %s", err)
1199 1199
 				} else if _, err := io.Copy(outStream, cLog); err != nil {
1200
-					utils.Debugf("Error streaming logs (stdout): %s", err)
1200
+					utils.Errorf("Error streaming logs (stdout): %s", err)
1201 1201
 				}
1202 1202
 			}
1203 1203
 			if stderr {
1204 1204
 				cLog, err := container.ReadLog("stderr")
1205 1205
 				if err != nil {
1206
-					utils.Debugf("Error reading logs (stderr): %s", err)
1206
+					utils.Errorf("Error reading logs (stderr): %s", err)
1207 1207
 				} else if _, err := io.Copy(errStream, cLog); err != nil {
1208
-					utils.Debugf("Error streaming logs (stderr): %s", err)
1208
+					utils.Errorf("Error streaming logs (stderr): %s", err)
1209 1209
 				}
1210 1210
 			}
1211 1211
 		} else if err != nil {
1212
-			utils.Debugf("Error reading logs (json): %s", err)
1212
+			utils.Errorf("Error reading logs (json): %s", err)
1213 1213
 		} else {
1214 1214
 			dec := json.NewDecoder(cLog)
1215 1215
 			for {
... ...
@@ -1218,7 +1218,7 @@ func (srv *Server) ContainerAttach(name string, logs, stream, stdin, stdout, std
1218 1218
 				if err := dec.Decode(l); err == io.EOF {
1219 1219
 					break
1220 1220
 				} else if err != nil {
1221
-					utils.Debugf("Error streaming logs: %s", err)
1221
+					utils.Errorf("Error streaming logs: %s", err)
1222 1222
 					break
1223 1223
 				}
1224 1224
 				if l.Stream == "stdout" && stdout {
... ...
@@ -45,24 +45,31 @@ func Download(url string, stderr io.Writer) (*http.Response, error) {
45 45
 	return resp, nil
46 46
 }
47 47
 
48
+func logf(level string, format string, a ...interface{}) {
49
+	// Retrieve the stack infos
50
+	_, file, line, ok := runtime.Caller(2)
51
+	if !ok {
52
+		file = "<unknown>"
53
+		line = -1
54
+	} else {
55
+		file = file[strings.LastIndex(file, "/")+1:]
56
+	}
57
+
58
+	fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] %s:%d %s\n", level, file, line, format), a...)
59
+}
60
+
48 61
 // Debug function, if the debug flag is set, then display. Do nothing otherwise
49 62
 // If Docker is in damon mode, also send the debug info on the socket
50 63
 func Debugf(format string, a ...interface{}) {
51 64
 	if os.Getenv("DEBUG") != "" {
52
-
53
-		// Retrieve the stack infos
54
-		_, file, line, ok := runtime.Caller(1)
55
-		if !ok {
56
-			file = "<unknown>"
57
-			line = -1
58
-		} else {
59
-			file = file[strings.LastIndex(file, "/")+1:]
60
-		}
61
-
62
-		fmt.Fprintf(os.Stderr, fmt.Sprintf("[debug] %s:%d %s\n", file, line, format), a...)
65
+		logf("debug", format, a...)
63 66
 	}
64 67
 }
65 68
 
69
+func Errorf(format string, a ...interface{}) {
70
+	logf("error", format, a...)
71
+}
72
+
66 73
 // Reader with progress bar
67 74
 type progressReader struct {
68 75
 	reader       io.ReadCloser // Stream to read from
... ...
@@ -319,7 +326,7 @@ func NewWriteBroadcaster() *WriteBroadcaster {
319 319
 
320 320
 func GetTotalUsedFds() int {
321 321
 	if fds, err := ioutil.ReadDir(fmt.Sprintf("/proc/%d/fd", os.Getpid())); err != nil {
322
-		Debugf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
322
+		Errorf("Error opening /proc/%d/fd: %s", os.Getpid(), err)
323 323
 	} else {
324 324
 		return len(fds)
325 325
 	}
... ...
@@ -771,7 +778,7 @@ func IsGIT(str string) bool {
771 771
 func GetResolvConf() ([]byte, error) {
772 772
 	resolv, err := ioutil.ReadFile("/etc/resolv.conf")
773 773
 	if err != nil {
774
-		Debugf("Error openning resolv.conf: %s", err)
774
+		Errorf("Error openning resolv.conf: %s", err)
775 775
 		return nil, err
776 776
 	}
777 777
 	return resolv, nil