Improved silly logging.
authorSebastien Douheret <sebastien.douheret@iot.bzh>
Tue, 5 Dec 2017 09:52:24 +0000 (10:52 +0100)
committerSebastien Douheret <sebastien.douheret@iot.bzh>
Tue, 5 Dec 2017 09:52:24 +0000 (10:52 +0100)
.vscode/settings.json
lib/agent/agent.go
lib/agent/apiv1-exec.go
lib/agent/events.go
lib/agent/sessions.go

index f5c711f..524a7d8 100644 (file)
@@ -40,7 +40,8 @@
     "iosk", "CIFS", "IPROJECT", "unregister", "conv", "PATHMAP", "nospace",
     "graphx", "Truthy", "darkviolet", "dwnl", "topnav", "leftbar", "urfave",
     "unmarshall", "sebd", "priv", "evts", "gdbserver", "tabset", "pageview",
-    "subpath", "prebuild", "reflectme", "franciscocpg", "xsapiv", "xaapiv"
+    "subpath", "prebuild", "reflectme", "franciscocpg", "xsapiv", "xaapiv",
+    "Sillyf"
   ],
   // codelyzer
   "tslint.rulesDirectory": "./webapp/node_modules/codelyzer",
index d1a3d05..58a2ba0 100644 (file)
@@ -41,6 +41,7 @@ type Context struct {
        Config        *xdsconfig.Config
        Log           *logrus.Logger
        LogLevelSilly bool
+       LogSillyf     func(format string, args ...interface{})
        SThg          *st.SyncThing
        SThgCmd       *exec.Cmd
        SThgInotCmd   *exec.Cmd
@@ -71,13 +72,21 @@ func NewAgent(cliCtx *cli.Context) *Context {
        }
        log.Formatter = &logrus.TextFormatter{}
 
+       // Support silly logging (printed on log.debug)
        sillyVal, sillyLog := os.LookupEnv("XDS_LOG_SILLY")
+       logSilly := sillyLog && sillyVal == "1"
+       sillyFunc := func(format string, args ...interface{}) {
+               if logSilly {
+                       log.Debugf("SILLY: "+format, args...)
+               }
+       }
 
        // Define default configuration
        ctx := Context{
                ProgName:      cliCtx.App.Name,
                Log:           log,
-               LogLevelSilly: (sillyLog && sillyVal == "1"),
+               LogLevelSilly: logSilly,
+               LogSillyf:     sillyFunc,
                Exit:          make(chan os.Signal, 1),
 
                webServer:  nil,
index 2f38b63..ace2cdc 100644 (file)
@@ -81,9 +81,7 @@ func (s *APIService) execCmd(c *gin.Context) {
        for _, evName := range evtInList {
                evN := evName
                err := (*sock).On(evN, func(stdin string) {
-                       if s.LogLevelSilly {
-                               s.Log.Debugf("EXEC EVENT IN (%s) <<%v>>", evN, stdin)
-                       }
+                       s.LogSillyf("EXEC EVENT IN (%s) <<%v>>", evN, stdin)
                        svr.EventEmit(evN, stdin)
                })
                if err != nil {
@@ -115,9 +113,7 @@ func (s *APIService) execCmd(c *gin.Context) {
                        // Add sessionID to event Data
                        reflectme.SetField(evData, "sessionID", sid)
 
-                       if s.LogLevelSilly {
-                               s.Log.Debugf("EXEC EVENT OUT (%s) <<%v>>", evN, evData)
-                       }
+                       s.LogSillyf("EXEC EVENT OUT (%s) <<%v>>", evN, evData)
 
                        // Forward event to Client/Dashboard
                        (*so).Emit(evN, evData)
index c316db8..df7015a 100644 (file)
@@ -95,9 +95,7 @@ func (e *Events) Emit(evName string, data interface{},fromSid string) error {
                return fmt.Errorf("Unsupported event type")
        }
 
-       if e.LogLevelSilly {
-               e.Log.Debugf("Emit Event %s: %v", evName, data)
-       }
+               e.LogSillyf("Emit Event %s: %v", evName, data)
 
        firstErr = nil
        evm := e.eventsMap[evName]
index d02ea3c..3655eef 100644 (file)
@@ -225,10 +225,8 @@ func (s *Sessions) monitorSessMap() {
                        s.Log.Debugln("Stop monitorSessMap")
                        return
                case <-time.After(sessionMonitorTime * time.Second):
-                       if s.LogLevelSilly {
-                               s.Log.Debugf("Sessions Map size: %d", len(s.sessMap))
-                               s.Log.Debugf("Sessions Map : %v", s.sessMap)
-                       }
+                       s.LogSillyf("Sessions Map size: %d", len(s.sessMap))
+                       s.LogSillyf("Sessions Map : %v", s.sessMap)
 
                        if len(s.sessMap) > maxSessions {
                                s.Log.Errorln("TOO MUCH sessions, cleanup old ones !")
@@ -237,9 +235,7 @@ func (s *Sessions) monitorSessMap() {
                        s.mutex.Lock()
                        for _, ss := range s.sessMap {
                                if ss.expireAt.Sub(time.Now()) < 0 {
-                                       if s.LogLevelSilly {
-                                               s.Log.Debugf("Delete expired session id: %s", ss.ID)
-                                       }
+                                       s.LogSillyf("Delete expired session id: %s", ss.ID)
                                        delete(s.sessMap, ss.ID)
                                }
                        }