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) { ...@@ -47,7 +47,6 @@ func firstNonWsRune(by []byte) (r rune, ok bool) {
} }
return r, true return r, true
} }
return 0, false
} }
// Determines if the bytes is a json array, only looks at prefix // Determines if the bytes is a json array, only looks at prefix
...@@ -259,6 +258,9 @@ func jsonEntry(name string, v interface{}) (interface{}, bool) { ...@@ -259,6 +258,9 @@ func jsonEntry(name string, v interface{}) (interface{}, bool) {
// jh.Get("name.arrayname[1]") // jh.Get("name.arrayname[1]")
// jh.Get("name.arrayname[]") // jh.Get("name.arrayname[]")
func (j JsonHelper) Get(n string) interface{} { func (j JsonHelper) Get(n string) interface{} {
if len(j) == 0 {
return nil
}
var parts []string var parts []string
if strings.Contains(n, "/") { if strings.Contains(n, "/") {
parts = strings.Split(n, "/") parts = strings.Split(n, "/")
...@@ -286,7 +288,6 @@ func (j JsonHelper) Get(n string) interface{} { ...@@ -286,7 +288,6 @@ func (j JsonHelper) Get(n string) interface{} {
} else { } else {
root, ok = jsonEntry(name, root) root, ok = jsonEntry(name, root)
} }
//Debug(isList, listEntry, " ", name, " ", root, " ", ok, err)
if !ok { if !ok {
if len(parts) > 0 { if len(parts) > 0 {
// lets ensure the actual json-value doesn't have period in key // lets ensure the actual json-value doesn't have period in key
...@@ -294,7 +295,6 @@ func (j JsonHelper) Get(n string) interface{} { ...@@ -294,7 +295,6 @@ func (j JsonHelper) Get(n string) interface{} {
if !ok { if !ok {
return nil return nil
} else { } else {
//Warnf("returning root %T %#v", root, root)
return root return root
} }
} else { } else {
......
package gou package gou
import ( import (
"context"
"fmt" "fmt"
"log" "log"
"os" "os"
...@@ -60,6 +61,7 @@ var ( ...@@ -60,6 +61,7 @@ var (
INFO: "[INFO] ", INFO: "[INFO] ",
DEBUG: "[DEBUG] ", DEBUG: "[DEBUG] ",
} }
logContextKey = "log_prefix"
escapeNewlines bool = false escapeNewlines bool = false
postFix = "" //\033[0m postFix = "" //\033[0m
LogLevelWords map[string]int = map[string]int{"fatal": 0, "error": 1, "warn": 2, "info": 3, "debug": 4, "none": -1} 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) { ...@@ -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 // Log at debug level
func Debug(v ...interface{}) { func Debug(v ...interface{}) {
if LogLevel >= 4 { if LogLevel >= 4 {
...@@ -514,7 +527,7 @@ const ( ...@@ -514,7 +527,7 @@ const (
_TIOCGWINSZ = 0x5413 // OSX 1074295912 _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. // Dummy discard, satisfies io.Writer without importing io or os.
type DevNull struct{} type DevNull struct{}
...@@ -522,8 +535,8 @@ func (DevNull) Write(p []byte) (int, error) { ...@@ -522,8 +535,8 @@ func (DevNull) Write(p []byte) (int, error) {
return len(p), nil return len(p), nil
} }
//Replace standard newline characters with escaped newlines so long msgs will // Replace standard newline characters with escaped newlines so long msgs will
//remain one line. // remain one line.
func EscapeNewlines(str string) string { func EscapeNewlines(str string) string {
return strings.Replace(str, "\n", "\\n", -1) 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