From b94feddead6ebb906b17a797477f2d0ff989832e Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sun, 24 Jul 2016 03:08:23 +0200
Subject: client/session specific log prefixes


diff --git a/src/rhimportd/ctrlTelnet.go b/src/rhimportd/ctrlTelnet.go
index c624654..b6093c9 100644
--- a/src/rhimportd/ctrlTelnet.go
+++ b/src/rhimportd/ctrlTelnet.go
@@ -182,7 +182,8 @@ func telnetSet(c *telgo.Client, args []string, conf *rhimport.Config, db *rddb.D
 
 	var ctx *rhimport.Context
 	if c.UserData == nil {
-		c.UserData = rhimport.NewContext(conf, db, rhl, rhdl)
+		logname := "telnet-" + c.Conn.RemoteAddr().String()
+		c.UserData = rhimport.NewContext(conf, db, getStdLog(logname+"-std"), getDbgLog(logname+"-dbg"))
 		ctx = c.UserData.(*rhimport.Context)
 		ctx.Trusted = false
 	} else {
diff --git a/src/rhimportd/ctrlWatchDir.go b/src/rhimportd/ctrlWatchDir.go
index 0fd389d..91b7621 100644
--- a/src/rhimportd/ctrlWatchDir.go
+++ b/src/rhimportd/ctrlWatchDir.go
@@ -110,7 +110,9 @@ func watchDirParseRequest(conf *rhimport.Config, db *rddb.DBChan, req *os.File)
 		return
 	}
 
-	ctx = rhimport.NewContext(conf, db, rhl, rhdl)
+	logname := "watch-" + filepath.Base(req.Name())
+	logname = strings.TrimSuffix(logname, filepath.Ext(logname))
+	ctx = rhimport.NewContext(conf, db, getStdLog(logname+"-std"), getDbgLog(logname+"-dbg"))
 	ctx.UserName = reqdata.UserName
 	ctx.Trusted = true
 	ctx.ShowId = reqdata.ShowId
@@ -162,11 +164,6 @@ func watchDirHandler(conf *rhimport.Config, db *rddb.DBChan, ctx *rhimport.Conte
 		watchDirErrorResponse(filename, http.StatusInternalServerError, err.Error(), res.SourceFile)
 		return
 	}
-	if res.ResponseCode == http.StatusOK {
-		rhl.Println("ImportFile succesfully imported", ctx.SourceFile)
-	} else {
-		rhl.Println("ImportFile import of", ctx.SourceFile, "was unsuccesful")
-	}
 
 	watchDirResponse(filename, res)
 	return
diff --git a/src/rhimportd/ctrlWebSimple.go b/src/rhimportd/ctrlWebSimple.go
index f190f63..8cc2ed0 100644
--- a/src/rhimportd/ctrlWebSimple.go
+++ b/src/rhimportd/ctrlWebSimple.go
@@ -92,7 +92,6 @@ func webSimpleResponse(w http.ResponseWriter, result *rhimport.Result) {
 }
 
 func webSimpleParseRequest(conf *rhimport.Config, db *rddb.DBChan, trusted bool, r *http.Request) (ctx *rhimport.Context, err error) {
-
 	decoder := json.NewDecoder(r.Body)
 	reqdata := newWebSimpleRequestData(conf)
 	if jsonerr := decoder.Decode(reqdata); jsonerr != nil {
@@ -100,7 +99,8 @@ func webSimpleParseRequest(conf *rhimport.Config, db *rddb.DBChan, trusted bool,
 		return
 	}
 
-	ctx = rhimport.NewContext(conf, db, rhl, rhdl)
+	logname := "simple-" + r.RemoteAddr
+	ctx = rhimport.NewContext(conf, db, getStdLog(logname+"-std"), getDbgLog(logname+"-dbg"))
 	if trusted {
 		ctx.UserName = r.Header.Get("X-Forwarded-User")
 	} else {
@@ -123,7 +123,7 @@ func webSimpleParseRequest(conf *rhimport.Config, db *rddb.DBChan, trusted bool,
 }
 
 func webSimpleHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport.SessionStoreChan, trusted bool, w http.ResponseWriter, r *http.Request) {
-	rhdl.Printf("WebSimpleHandler: request for '%s'", html.EscapeString(r.URL.Path))
+	rhdl.Printf("WebSimpleHandler: %s requests '%s'", r.RemoteAddr, html.EscapeString(r.URL.Path))
 
 	var ctx *rhimport.Context
 	var err error
@@ -160,11 +160,6 @@ func webSimpleHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport
 		webSimpleErrorResponse(w, http.StatusInternalServerError, err.Error())
 		return
 	}
-	if res.ResponseCode == http.StatusOK {
-		rhl.Println("ImportFile succesfully imported", ctx.SourceFile)
-	} else {
-		rhl.Println("ImportFile import of", ctx.SourceFile, "was unsuccesful")
-	}
 
 	webSimpleResponse(w, res)
 	return
diff --git a/src/rhimportd/ctrlWebSocket.go b/src/rhimportd/ctrlWebSocket.go
index 48c08fe..74b88e3 100644
--- a/src/rhimportd/ctrlWebSocket.go
+++ b/src/rhimportd/ctrlWebSocket.go
@@ -226,7 +226,7 @@ func webSocketDone(res rhimport.Result, userdata interface{}) bool {
 }
 
 func (self *webSocketSession) startNewSession(reqdata *webSocketRequestData, conf *rhimport.Config, sessions *rhimport.SessionStoreChan) (int, string) {
-	ctx := rhimport.NewContext(conf, nil, rhl, rhdl)
+	ctx := rhimport.NewContext(conf, nil, getStdLog("sess-%s-std"), getDbgLog("sess-%s-dbg")) // session store will take of db, stdlog and dbglog
 	ctx.UserName = reqdata.UserName
 	ctx.Password = reqdata.Password
 	ctx.Trusted = false
@@ -377,7 +377,7 @@ func webSocketSessionHandler(reqchan <-chan webSocketRequestData, ws *websocket.
 }
 
 func webSocketHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport.SessionStoreChan, trusted bool, w http.ResponseWriter, r *http.Request) {
-	rhdl.Printf("WebSocketHandler: request for '%s'", html.EscapeString(r.URL.Path))
+	rhdl.Printf("WebSocketHandler: %s requests '%s'", r.RemoteAddr, html.EscapeString(r.URL.Path))
 
 	ws, err := websocket.Upgrade(w, r, nil, 64*1024, 64*1024)
 	if _, ok := err.(websocket.HandshakeError); ok {
diff --git a/src/rhimportd/main.go b/src/rhimportd/main.go
index 4f12572..6c68962 100644
--- a/src/rhimportd/main.go
+++ b/src/rhimportd/main.go
@@ -37,15 +37,24 @@ import (
 	"time"
 )
 
+func getStdLog(name string) *log.Logger {
+	return log.New(os.Stderr, "["+name+"] ", log.LstdFlags)
+}
+
+func getDbgLog(name string) *log.Logger {
+	dbg := log.New(ioutil.Discard, "["+name+"] ", log.LstdFlags)
+	if _, exists := os.LookupEnv("RHIMPORTD_DEBUG"); exists {
+		dbg.SetOutput(os.Stderr)
+	}
+	return dbg
+}
+
 var (
-	rhl  = log.New(os.Stderr, "[std] ", log.LstdFlags)
-	rhdl = log.New(ioutil.Discard, "[dbg] ", log.LstdFlags)
+	rhl  = getStdLog("std")
+	rhdl = getDbgLog("dbg")
 )
 
 func init() {
-	if _, exists := os.LookupEnv("RHIMPORTD_DEBUG"); exists {
-		rhdl.SetOutput(os.Stderr)
-	}
 	rhimport.Init(rhl, rhdl)
 }
 
diff --git a/src/rhimportd/uploadWeb.go b/src/rhimportd/uploadWeb.go
index a4f0951..0b7ab82 100644
--- a/src/rhimportd/uploadWeb.go
+++ b/src/rhimportd/uploadWeb.go
@@ -58,7 +58,7 @@ func webUploadSuccessResponse(w http.ResponseWriter) {
 }
 
 func webUploadHandler(conf *rhimport.Config, db *rddb.DBChan, sessions *rhimport.SessionStoreChan, trusted bool, w http.ResponseWriter, r *http.Request) {
-	rhdl.Printf("WebUploadHandler: got request for %s", r.URL.String())
+	rhdl.Printf("WebUploadHandler: %s requests '%s'", r.RemoteAddr, r.URL.String())
 
 	if r.Method != "POST" {
 		rhl.Printf("WebUploadHandler: got invalid request method")
-- 
cgit v0.10.2