Commit 9a11413a authored by Aaron Raddon's avatar Aaron Raddon

Add Context helper functions

parent 6d4a4b33
......@@ -47,7 +47,6 @@ func firstNonWsRune(by []byte) (r rune, ok bool) {
}
return r, true
}
return 0, false
}
// Determines if the bytes is a json array, only looks at prefix
......@@ -259,6 +258,9 @@ func jsonEntry(name string, v interface{}) (interface{}, bool) {
// jh.Get("name.arrayname[1]")
// jh.Get("name.arrayname[]")
func (j JsonHelper) Get(n string) interface{} {
if len(j) == 0 {
return nil
}
var parts []string
if strings.Contains(n, "/") {
parts = strings.Split(n, "/")
......@@ -286,7 +288,6 @@ func (j JsonHelper) Get(n string) interface{} {
} else {
root, ok = jsonEntry(name, root)
}
//Debug(isList, listEntry, " ", name, " ", root, " ", ok, err)
if !ok {
if len(parts) > 0 {
// lets ensure the actual json-value doesn't have period in key
......@@ -294,7 +295,6 @@ func (j JsonHelper) Get(n string) interface{} {
if !ok {
return nil
} else {
//Warnf("returning root %T %#v", root, root)
return root
}
} else {
......
package gou
import (
"context"
"fmt"
"log"
"os"
......@@ -60,6 +61,7 @@ var (
INFO: "[INFO] ",
DEBUG: "[DEBUG] ",
}
logContextKey = "log_prefix"
escapeNewlines bool = false
postFix = "" //\033[0m
LogLevelWords map[string]int = map[string]int{"fatal": 0, "error": 1, "warn": 2, "info": 3, "debug": 4, "none": -1}
......@@ -172,6 +174,17 @@ func LogLevelSet(levelWord string) {
}
}
// NewContext returns a new Context carrying Log context prefix.
func NewContext(ctx context.Context, msg string) context.Context {
return context.WithValue(ctx, logContextKey, msg)
}
// FromContext extracts the Log Context prefix from context
func FromContext(ctx context.Context) string {
logContext, _ := ctx.Value(logContextKey).(string)
return logContext
}
// Log at debug level
func Debug(v ...interface{}) {
if LogLevel >= 4 {
......@@ -514,7 +527,7 @@ const (
_TIOCGWINSZ = 0x5413 // OSX 1074295912
)
//http://play.golang.org/p/5LIA41Iqfp
// http://play.golang.org/p/5LIA41Iqfp
// Dummy discard, satisfies io.Writer without importing io or os.
type DevNull struct{}
......@@ -522,8 +535,8 @@ func (DevNull) Write(p []byte) (int, error) {
return len(p), nil
}
//Replace standard newline characters with escaped newlines so long msgs will
//remain one line.
// Replace standard newline characters with escaped newlines so long msgs will
// remain one line.
func EscapeNewlines(str string) string {
return strings.Replace(str, "\n", "\\n", -1)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment