Repository: incubator-htrace
Updated Branches:
  refs/heads/master 50707f06a -> c7f645c3c


HTRACE-125. htraced: log when htraced terminates on a signal, and optionally 
log configuration when starting up (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/c7f645c3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/c7f645c3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/c7f645c3

Branch: refs/heads/master
Commit: c7f645c3cf6008084b1a757e12018af9ddd05d75
Parents: 50707f0
Author: Colin P. Mccabe <[email protected]>
Authored: Mon Mar 2 15:31:46 2015 -0800
Committer: Colin P. Mccabe <[email protected]>
Committed: Mon Mar 2 15:34:48 2015 -0800

----------------------------------------------------------------------
 .../src/go/src/org/apache/htrace/common/log.go  |  6 +-
 .../go/src/org/apache/htrace/common/process.go  | 67 ++++++++++++++++++++
 .../src/go/src/org/apache/htrace/conf/config.go | 37 +++++++----
 .../src/org/apache/htrace/conf/config_test.go   |  6 +-
 .../src/go/src/org/apache/htrace/htrace/cmd.go  |  2 +-
 .../go/src/org/apache/htrace/htraced/htraced.go |  3 +-
 6 files changed, 100 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/c7f645c3/htrace-core/src/go/src/org/apache/htrace/common/log.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/common/log.go 
b/htrace-core/src/go/src/org/apache/htrace/common/log.go
index 5ced1be..c5f495d 100644
--- a/htrace-core/src/go/src/org/apache/htrace/common/log.go
+++ b/htrace-core/src/go/src/org/apache/htrace/common/log.go
@@ -177,13 +177,13 @@ func LevelFromString(str string) (Level, error) {
 
 type Logger struct {
        sink  *logSink
-       level Level
+       Level Level
 }
 
 func NewLogger(faculty string, cnf *conf.Config) *Logger {
        path, level := parseConf(faculty, cnf)
        sink := getOrCreateLogSink(path)
-       return &Logger{sink: sink, level: level}
+       return &Logger{sink: sink, Level: level}
 }
 
 func parseConf(faculty string, cnf *conf.Config) (string, Level) {
@@ -256,7 +256,7 @@ func (lg *Logger) Errorf(format string, v ...interface{}) 
error {
 }
 
 func (lg *Logger) write(level Level, str string) {
-       if level >= lg.level {
+       if level >= lg.Level {
                lg.sink.write(time.Now().Format(time.RFC3339) + " " +
                        level.LogString() + ": " + str)
        }

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/c7f645c3/htrace-core/src/go/src/org/apache/htrace/common/process.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/common/process.go 
b/htrace-core/src/go/src/org/apache/htrace/common/process.go
new file mode 100644
index 0000000..d138178
--- /dev/null
+++ b/htrace-core/src/go/src/org/apache/htrace/common/process.go
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package common
+
+import (
+       "bufio"
+       "org/apache/htrace/conf"
+       "os"
+       "os/signal"
+       "syscall"
+)
+
+func LoadApplicationConfig() *conf.Config {
+       cnf, dlog := conf.LoadApplicationConfig()
+       lg := NewLogger("conf", cnf)
+       defer lg.Close()
+       if lg.Level <= DEBUG {
+               // Print out the debug information from loading the 
configuration.
+               scanner := bufio.NewScanner(dlog)
+               for scanner.Scan() {
+                       lg.Debugf(scanner.Text() + "\n")
+               }
+       }
+       return cnf
+}
+
+func InstallSignalHandlers(cnf *conf.Config) {
+       fatalSigs := []os.Signal{
+               os.Interrupt,
+               os.Kill,
+               syscall.SIGINT,
+               syscall.SIGABRT,
+               syscall.SIGALRM,
+               syscall.SIGBUS,
+               syscall.SIGFPE,
+               syscall.SIGILL,
+               syscall.SIGQUIT,
+               syscall.SIGSEGV,
+               syscall.SIGTERM,
+       }
+       sigChan := make(chan os.Signal, len(fatalSigs))
+       signal.Notify(sigChan, fatalSigs...)
+       lg := NewLogger("exit", cnf)
+       go func() {
+               sig := <-sigChan
+               lg.Errorf("Terminating on signal: %v\n", sig)
+               lg.Close()
+               os.Exit(1)
+       }()
+}

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/c7f645c3/htrace-core/src/go/src/org/apache/htrace/conf/config.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/conf/config.go 
b/htrace-core/src/go/src/org/apache/htrace/conf/config.go
index f9784c5..6093649 100644
--- a/htrace-core/src/go/src/org/apache/htrace/conf/config.go
+++ b/htrace-core/src/go/src/org/apache/htrace/conf/config.go
@@ -21,11 +21,13 @@ package conf
 
 import (
        "bufio"
+       "bytes"
        "fmt"
        "io"
        "log"
        "os"
        "path/filepath"
+       "sort"
        "strconv"
        "strings"
        "syscall"
@@ -66,8 +68,9 @@ type Builder struct {
        Argv []string
 }
 
-func getHTracedConfDirs() []string {
+func getHTracedConfDirs(dlog io.Writer) []string {
        confDir := os.Getenv("HTRACED_CONF_DIR")
+       io.WriteString(dlog, fmt.Sprintf("HTRACED_CONF_DIR=%s\n", confDir))
        paths := filepath.SplitList(confDir)
        if len(paths) < 1 {
                return []string{"."}
@@ -77,11 +80,9 @@ func getHTracedConfDirs() []string {
 
 // Load a configuration from the application's argv, configuration file, and 
the standard
 // defaults.
-func LoadApplicationConfig() *Config {
-       reader, err := openFile(CONFIG_FILE_NAME, getHTracedConfDirs())
-       if err != nil {
-               log.Fatal("Error opening config file: " + err.Error())
-       }
+func LoadApplicationConfig() (*Config, io.Reader) {
+       dlog := new(bytes.Buffer)
+       reader := openFile(CONFIG_FILE_NAME, getHTracedConfDirs(dlog), dlog)
        bld := Builder{}
        if reader != nil {
                defer reader.Close()
@@ -89,30 +90,38 @@ func LoadApplicationConfig() *Config {
        }
        bld.Argv = os.Args[1:]
        bld.Defaults = DEFAULTS
-       var cnf *Config
-       cnf, err = bld.Build()
+       cnf, err := bld.Build()
        if err != nil {
                log.Fatal("Error building configuration: " + err.Error())
        }
        os.Args = append(os.Args[0:1], bld.Argv...)
-       return cnf
+       keys := make(sort.StringSlice, 0, 20)
+       for k, _ := range cnf.settings {
+               keys = append(keys, k)
+       }
+       sort.Sort(keys)
+       for i := range keys {
+               io.WriteString(dlog, fmt.Sprintf("%s = %s\n",
+                       keys[i], cnf.settings[keys[i]]))
+       }
+       return cnf, dlog
 }
 
 // Attempt to open a configuration file somewhere on the provided list of 
paths.
-func openFile(cnfName string, paths []string) (io.ReadCloser, error) {
+func openFile(cnfName string, paths []string, dlog io.Writer) io.ReadCloser {
        for p := range paths {
                path := fmt.Sprintf("%s%c%s", paths[p], os.PathSeparator, 
cnfName)
                file, err := os.Open(path)
                if err == nil {
-                       log.Println("Reading configuration from " + path)
-                       return file, nil
+                       io.WriteString(dlog, fmt.Sprintf("Reading configuration 
from %s.\n", path))
+                       return file
                }
                if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
                        continue
                }
-               log.Println("Error opening " + path + " for read: " + 
err.Error())
+               io.WriteString(dlog, fmt.Sprintf("Error opening %s for read: 
%s\n", path, err.Error()))
        }
-       return nil, nil
+       return nil
 }
 
 // Try to parse a command-line element as a key=value pair.

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/c7f645c3/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go 
b/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
index 1e94ed7..42c1c71 100644
--- a/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
+++ b/htrace-core/src/go/src/org/apache/htrace/conf/config_test.go
@@ -20,6 +20,7 @@
 package conf
 
 import (
+       "bytes"
        "os"
        "strings"
        "testing"
@@ -124,12 +125,13 @@ func TestXmlConfigurationFile(t *testing.T) {
 // Test our handling of the HTRACE_CONF_DIR environment variable.
 func TestGetHTracedConfDirs(t *testing.T) {
        os.Setenv("HTRACED_CONF_DIR", "")
-       dirs := getHTracedConfDirs()
+       dlog := new(bytes.Buffer)
+       dirs := getHTracedConfDirs(dlog)
        if len(dirs) != 1 || dirs[0] != "." {
                t.Fatal()
        }
        os.Setenv("HTRACED_CONF_DIR", "/foo/bar:/baz")
-       dirs = getHTracedConfDirs()
+       dirs = getHTracedConfDirs(dlog)
        if len(dirs) != 2 || dirs[0] != "/foo/bar" || dirs[1] != "/baz" {
                t.Fatal()
        }

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/c7f645c3/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go 
b/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
index a38e314..0317237 100644
--- a/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
+++ b/htrace-core/src/go/src/org/apache/htrace/htrace/cmd.go
@@ -51,7 +51,7 @@ the defaults will be used.
 
 func main() {
        // Load htraced configuration
-       cnf := conf.LoadApplicationConfig()
+       cnf := common.LoadApplicationConfig()
 
        // Parse argv
        app := kingpin.New(os.Args[0], USAGE)

http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/c7f645c3/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go 
b/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
index 191b68e..c3432b4 100644
--- a/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
+++ b/htrace-core/src/go/src/org/apache/htrace/htraced/htraced.go
@@ -63,7 +63,8 @@ func main() {
                        os.Exit(0)
                }
        }
-       cnf := conf.LoadApplicationConfig()
+       cnf := common.LoadApplicationConfig()
+       common.InstallSignalHandlers(cnf)
        lg := common.NewLogger("main", cnf)
        defer lg.Close()
        store, err := CreateDataStore(cnf, nil)

Reply via email to