github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/hugofs/stacktracer_fs.go (about) 1 // Copyright 2019 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package hugofs 15 16 import ( 17 "fmt" 18 "os" 19 "regexp" 20 "runtime" 21 22 "github.com/gohugoio/hugo/common/types" 23 24 "github.com/spf13/afero" 25 ) 26 27 // Make sure we don't accidentally use this in the real Hugo. 28 var _ types.DevMarker = (*stacktracerFs)(nil) 29 30 // NewStacktracerFs wraps the given fs printing stack traces for file creates 31 // matching the given regexp pattern. 32 func NewStacktracerFs(fs afero.Fs, pattern string) afero.Fs { 33 return &stacktracerFs{Fs: fs, re: regexp.MustCompile(pattern)} 34 } 35 36 // stacktracerFs can be used in hard-to-debug development situations where 37 // you get some input you don't understand where comes from. 38 type stacktracerFs struct { 39 afero.Fs 40 41 // Will print stacktrace for every file creates matching this pattern. 42 re *regexp.Regexp 43 } 44 45 func (fs *stacktracerFs) DevOnly() { 46 } 47 48 func (fs *stacktracerFs) onCreate(filename string) { 49 if fs.re.MatchString(filename) { 50 trace := make([]byte, 1500) 51 runtime.Stack(trace, true) 52 fmt.Printf("\n===========\n%q:\n%s\n", filename, trace) 53 } 54 } 55 56 func (fs *stacktracerFs) Create(name string) (afero.File, error) { 57 f, err := fs.Fs.Create(name) 58 if err == nil { 59 fs.onCreate(name) 60 } 61 return f, err 62 } 63 64 func (fs *stacktracerFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { 65 f, err := fs.Fs.OpenFile(name, flag, perm) 66 if err == nil && isWrite(flag) { 67 fs.onCreate(name) 68 } 69 return f, err 70 }