You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by ne...@apache.org on 2016/11/07 19:30:05 UTC

[19/21] incubator-trafficcontrol git commit: Add TM2 log.Close to log io.Closers

Add TM2 log.Close to log io.Closers

Adds `log.Close` and `log.Closef`, which call `Close` on a given
`io.Closer`, and log any error, along with given context. This allows
succinctly calling `defer log.Close(resp.Body` to log, while retaining
the convenience, brevity, and idiomaticy of `defer`.


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/f12f0639
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/f12f0639
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/f12f0639

Branch: refs/heads/master
Commit: f12f0639ce2b11248319dbabbef6abb31639db6e
Parents: 1aa0c0b
Author: Robert Butts <ro...@gmail.com>
Authored: Thu Nov 3 13:58:43 2016 -0600
Committer: Dave Neuman <ne...@apache.org>
Committed: Mon Nov 7 12:29:08 2016 -0700

----------------------------------------------------------------------
 traffic_monitor/experimental/common/log/log.go | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/f12f0639/traffic_monitor/experimental/common/log/log.go
----------------------------------------------------------------------
diff --git a/traffic_monitor/experimental/common/log/log.go b/traffic_monitor/experimental/common/log/log.go
index 596c733..cf5144f 100644
--- a/traffic_monitor/experimental/common/log/log.go
+++ b/traffic_monitor/experimental/common/log/log.go
@@ -48,3 +48,20 @@ func Debugf(format string, v ...interface{}) {
 func Debugln(v ...interface{}) {
 	Debug.Output(3, time.Now().Format(timeFormat)+": "+fmt.Sprintln(v...))
 }
+
+// Close calls `Close()` on the given Closer, and logs any error. On error, the context is logged, followed by a colon, the error message, and a newline. This is primarily designed to be used in `defer`, for example, `defer log.Close(resp.Body, "readData fetching /foo/bar")`.
+func Close(c io.Closer, context string) {
+	err := c.Close()
+	if err != nil {
+		Errorf("%v: %v", context, err)
+	}
+}
+
+// Closef acts like Close, with a given format string and values, followed by a colon, the error message, and a newline. The given values are not coerced, concatenated, or printed unless an error occurs, so this is more efficient than `Close()`.
+func Closef(c io.Closer, contextFormat string, v ...interface{}) {
+	err := c.Close()
+	if err != nil {
+		Errorf(contextFormat, v...)
+		Errorf(": %v", err)
+	}
+}