Repository: incubator-htrace
Updated Branches:
  refs/heads/master 121121682 -> b4c968740


HTRACE-158. htraced: add TraceEnabled, DebugEnabled, etc. functions to log.go 
(cmccabe)


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

Branch: refs/heads/master
Commit: b4c96874060924ec881a1eb9e99e7ff189730d84
Parents: 1211216
Author: Colin P. Mccabe <[email protected]>
Authored: Sun Apr 26 14:04:19 2015 -0700
Committer: Colin P. Mccabe <[email protected]>
Committed: Tue Apr 28 14:19:14 2015 -0700

----------------------------------------------------------------------
 .../src/go/src/org/apache/htrace/common/log.go  | 30 +++++++++++++++++++-
 .../src/org/apache/htrace/htraced/datastore.go  |  4 ++-
 .../go/src/org/apache/htrace/htraced/rest.go    |  8 ++++--
 3 files changed, 38 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/b4c96874/htrace-htraced/src/go/src/org/apache/htrace/common/log.go
----------------------------------------------------------------------
diff --git a/htrace-htraced/src/go/src/org/apache/htrace/common/log.go 
b/htrace-htraced/src/go/src/org/apache/htrace/common/log.go
index c5f495d..edec869 100644
--- a/htrace-htraced/src/go/src/org/apache/htrace/common/log.go
+++ b/htrace-htraced/src/go/src/org/apache/htrace/common/log.go
@@ -104,7 +104,7 @@ func (path logPath) Open() *logSink {
        if path == STDOUT_LOG_PATH {
                return &logSink{path: path, file: os.Stdout}
        }
-       file, err := os.OpenFile(string(path), 
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0777)
+       file, err := os.OpenFile(string(path), 
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
        if err != nil {
                sink := &logSink{path: STDOUT_LOG_PATH, file: os.Stdout}
                fmt.Fprintf(os.Stderr, "Failed to open log file %s: %s\n",
@@ -262,6 +262,34 @@ func (lg *Logger) write(level Level, str string) {
        }
 }
 
+//
+// A few functions which can be used to determine if a certain level of tracing
+// is enabled.  These are useful in situations when evaluating the parameters
+// of a logging function is expensive.  (Note, however, that we don't pay the
+// cost of string concatenation and manipulation when a log message doesn't
+// trigger.)
+//
+
+func (lg *Logger) TraceEnabled() bool {
+       return lg.Level >= TRACE
+}
+
+func (lg *Logger) DebugEnabled() bool {
+       return lg.Level >= DEBUG
+}
+
+func (lg *Logger) InfoEnabled() bool {
+       return lg.Level >= INFO
+}
+
+func (lg *Logger) WarnEnabled() bool {
+       return lg.Level >= WARN
+}
+
+func (lg *Logger) ErrorEnabled() bool {
+       return lg.Level >= ERROR
+}
+
 func (lg *Logger) Close() {
        lg.sink.Unref()
        lg.sink = nil

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/b4c96874/htrace-htraced/src/go/src/org/apache/htrace/htraced/datastore.go
----------------------------------------------------------------------
diff --git a/htrace-htraced/src/go/src/org/apache/htrace/htraced/datastore.go 
b/htrace-htraced/src/go/src/org/apache/htrace/htraced/datastore.go
index faf23cd..a26779c 100644
--- a/htrace-htraced/src/go/src/org/apache/htrace/htraced/datastore.go
+++ b/htrace-htraced/src/go/src/org/apache/htrace/htraced/datastore.go
@@ -913,7 +913,9 @@ func (store *dataStore) HandleQuery(query *common.Query) 
([]*common.Span, error)
                if span == nil {
                        break // the source has no more spans to give
                }
-               lg.Debugf("src.next returned span %s\n", span.ToJson())
+               if lg.DebugEnabled() {
+                       lg.Debugf("src.next returned span %s\n", span.ToJson())
+               }
                satisfied := true
                for predIdx := range preds {
                        if !preds[predIdx].satisfiedBy(span) {

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/b4c96874/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go
----------------------------------------------------------------------
diff --git a/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go 
b/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go
index 1449802..69b316c 100644
--- a/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go
+++ b/htrace-htraced/src/go/src/org/apache/htrace/htraced/rest.go
@@ -63,7 +63,9 @@ func (hand *serverInfoHandler) ServeHTTP(w 
http.ResponseWriter, req *http.Reques
                        fmt.Sprintf("error marshalling ServerInfo: %s\n", 
err.Error()))
                return
        }
-       hand.lg.Debugf("Returned serverInfo %s\n", string(buf))
+       if hand.lg.DebugEnabled() {
+               hand.lg.Debugf("Returned serverInfo %s\n", string(buf))
+       }
        w.Write(buf)
 }
 
@@ -180,7 +182,9 @@ func (hand *writeSpansHandler) ServeHTTP(w 
http.ResponseWriter, req *http.Reques
        hand.lg.Debugf("writeSpansHandler: received %d span(s).  defaultPid = 
%s\n",
                len(spans), defaultPid)
        for spanIdx := range spans {
-               hand.lg.Debugf("writing span %s\n", spans[spanIdx].ToJson())
+               if hand.lg.DebugEnabled() {
+                       hand.lg.Debugf("writing span %s\n", 
spans[spanIdx].ToJson())
+               }
                hand.store.WriteSpan(spans[spanIdx])
        }
 }

Reply via email to