func Log(verbosity int, a ...interface{}) { val, err := strconv.Atoi(config.Get("dbg.verbosity")) if err != nil { log.Fatal("dbg.Log:", err) } if val >= verbosity { fmt.Fprintln(writers[config.Get("dbg.debugWriter")], a...) } }
func ErrLog(verbosity int, format string, a ...interface{}) { val, err := strconv.Atoi(config.Get("dbg.verbosity")) if err != nil { log.Fatal("dbg.ErrLog:", err) } if val >= verbosity { fmt.Fprintf(os.Stderr, format, a...) } }
func (h fileWriter) Write(p []byte) (n int, err error) { str := config.Get("dbg.logfile") if str == "" { return 0, nil } f, err := os.OpenFile(str, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { log.Fatal("dbg.filewriter:", err) } defer f.Close() return f.Write(p) }
func (h httpWriter) Write(p []byte) (n int, err error) { str := config.Get("dbg.httpUrl") if str == "" { return 0, nil } payload := fmt.Sprintf(string(p[:])) str = fmt.Sprintf("http://%s?%s=%s", str, time.Now().Format("2006-01-02T15:04:05.999Z07:00"), url.QueryEscape(payload)) resp, err := http.Get(str) if err != nil { return 0, fmt.Errorf("dbg.httpWriter:", err) } defer resp.Body.Close() return 0, nil }
func main() { // define all string based options var opts = [][]string{ {"portExternal", "localhost:7000", "external web port"}, {"config.portInternal", "localhost:7100"}, {"dbg.httpUrl", "localhost:7000"}, {"dbg.verbosity", "1"}, } if err = config.ParseArgs(opts); err != nil { fmt.Fprintln(os.Stderr, err) } dbg.Log(2, config.Dump()) dbg.Log(0, "Starting..") fmt.Println("listening on", config.Get("portExternal")) go test() webExternal.Run() }
func Run() { serverExternal := http.NewServeMux() serverExternal.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(config.Get("portExternal"), serverExternal)) }