github.com/developest/gtm-core@v1.0.4-0.20220111132249-cc80a3372c3f/util/profile_on.go (about) 1 // +build profile 2 3 // Copyright 2016 Michael Schenk. All rights reserved. 4 // Use of this source code is governed by a MIT-style 5 // license that can be found in the LICENSE file. 6 7 package util 8 9 import ( 10 "log" 11 "os" 12 "os/user" 13 "path/filepath" 14 "time" 15 ) 16 17 // Profile is a profile logger 18 var Profile func(s ...string) func() 19 20 func init() { 21 u, err := user.Current() 22 if err != nil { 23 return 24 } 25 26 w, err := os.OpenFile(filepath.Join(u.HomeDir, "gtm-profile.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) 27 if err != nil { 28 return 29 } 30 31 profileLog := NewContextLogger(log.New(w, "profile ", log.Lmicroseconds), 4) 32 33 Profile = func(s ...string) func() { 34 start := time.Now() 35 label := "" 36 if len(s) > 0 { 37 label = s[0] 38 } 39 return func() { 40 t := time.Since(start) 41 if label == "" { 42 profileLog.Printf("%s", t) 43 } else { 44 profileLog.Printf("%s %s", label, t) 45 } 46 } 47 } 48 }