github.com/misseven0/notify@v0.0.0-20230519123055-c1422e46da05/debug.go (about)

     1  // Copyright (c) 2014-2015 The Notify Authors. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be
     3  // found in the LICENSE file.
     4  
     5  package notify
     6  
     7  import (
     8  	"log"
     9  	"os"
    10  	"runtime"
    11  	"strings"
    12  )
    13  
    14  var dbgprint func(...interface{})
    15  
    16  var dbgprintf func(string, ...interface{})
    17  
    18  var dbgcallstack func(max int) []string
    19  
    20  func init() {
    21  	if _, ok := os.LookupEnv("NOTIFY_DEBUG"); ok || debugTag {
    22  		log.SetOutput(os.Stdout)
    23  		log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
    24  		dbgprint = func(v ...interface{}) {
    25  			v = append([]interface{}{"[D] "}, v...)
    26  			log.Println(v...)
    27  		}
    28  		dbgprintf = func(format string, v ...interface{}) {
    29  			format = "[D] " + format
    30  			log.Printf(format, v...)
    31  		}
    32  		dbgcallstack = func(max int) []string {
    33  			pc, stack := make([]uintptr, max), make([]string, 0, max)
    34  			runtime.Callers(2, pc)
    35  			for _, pc := range pc {
    36  				if f := runtime.FuncForPC(pc); f != nil {
    37  					fname := f.Name()
    38  					idx := strings.LastIndex(fname, string(os.PathSeparator))
    39  					if idx != -1 {
    40  						stack = append(stack, fname[idx+1:])
    41  					} else {
    42  						stack = append(stack, fname)
    43  					}
    44  				}
    45  			}
    46  			return stack
    47  		}
    48  		return
    49  	}
    50  	dbgprint = func(v ...interface{}) {}
    51  	dbgprintf = func(format string, v ...interface{}) {}
    52  	dbgcallstack = func(max int) []string { return nil }
    53  }