package l import ( "encoding/json" "fmt" "sync" ) const ( TError = 1 TWarning = 2 TInfo = 3 TDebug = 4 TTrace = 5 TMax = 99 ) var traceModules = make(map[string]bool) var mutex sync.RWMutex var tracePool = sync.Pool{ New: func() interface{} { return new(TraceObj) }, } type TraceObj struct { module string level int trace bool } // SetTraceModules set trace modules, "all" to trace all. // e.g. SetTraceModules([]string{"inotify", "psnotify"}) // trace level use Logger.verboseLevel func SetTraceModules(modules []string) { mutex.Lock() defer mutex.Unlock() traceModules = make(map[string]bool) for _, module := range modules { traceModules[module] = true } } func T(module string, level int) *TraceObj { obj := tracePool.Get().(*TraceObj) obj.module = module obj.level = level if logger.verboseLevel < level { obj.trace = false return obj } mutex.RLock() defer mutex.RUnlock() if _, ok := traceModules["all"]; ok { obj.trace = true return obj } if _, ok := traceModules[module]; ok { obj.trace = true return obj } obj.trace = false return obj } func (t *TraceObj) Trace(msgs ...interface{}) { defer tracePool.Put(t) if !t.trace { return } if logger.logLevel > LevelDebug { return } tag := fmt.Sprintf("TRACE-%s-%d", t.module, t.level) logMsg(tag, msgs...) } func (t *TraceObj) Tracef(msgs ...interface{}) { defer tracePool.Put(t) if !t.trace { return } if logger.logLevel > LevelDebug { return } tag := fmt.Sprintf("TRACE-%s-%d", t.module, t.level) if len(msgs) < 2 { logMsg(tag, msgs...) return } logMsg(tag, fmt.Sprintf(msgs[0].(string), msgs[1:]...)) } func (t *TraceObj) Tracej(msgs ...interface{}) { defer tracePool.Put(t) if !t.trace { return } if logger.logLevel > LevelDebug { return } jsonMsg := []interface{}{} for _, msg := range msgs { b, err := json.MarshalIndent(msg, "", " ") if err != nil { jsonMsg = append(jsonMsg, msg) } else { jsonMsg = append(jsonMsg, string(b)) } } tag := fmt.Sprintf("TRACE-%s-%d", t.module, t.level) logMsg(tag, jsonMsg...) }