You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debug.go 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2015 go-swagger maintainers
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package generator
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "log"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. )
  23. var (
  24. // Debug when the env var DEBUG or SWAGGER_DEBUG is not empty
  25. // the generators will be very noisy about what they are doing
  26. Debug = os.Getenv("DEBUG") != "" || os.Getenv("SWAGGER_DEBUG") != ""
  27. // generatorLogger is a debug logger for this package
  28. generatorLogger *log.Logger
  29. )
  30. func init() {
  31. debugOptions()
  32. }
  33. func debugOptions() {
  34. generatorLogger = log.New(os.Stdout, "generator:", log.LstdFlags)
  35. }
  36. // debugLog wraps log.Printf with a debug-specific logger
  37. func debugLog(frmt string, args ...interface{}) {
  38. if Debug {
  39. _, file, pos, _ := runtime.Caller(1)
  40. generatorLogger.Printf("%s:%d: %s", filepath.Base(file), pos,
  41. fmt.Sprintf(frmt, args...))
  42. }
  43. }
  44. // debugLogAsJSON unmarshals its last arg as pretty JSON
  45. func debugLogAsJSON(frmt string, args ...interface{}) {
  46. if Debug {
  47. var dfrmt string
  48. _, file, pos, _ := runtime.Caller(1)
  49. dargs := make([]interface{}, 0, len(args)+2)
  50. dargs = append(dargs, filepath.Base(file), pos)
  51. if len(args) > 0 {
  52. dfrmt = "%s:%d: " + frmt + "\n%s"
  53. bbb, _ := json.MarshalIndent(args[len(args)-1], "", " ")
  54. dargs = append(dargs, args[0:len(args)-1]...)
  55. dargs = append(dargs, string(bbb))
  56. } else {
  57. dfrmt = "%s:%d: " + frmt
  58. }
  59. generatorLogger.Printf(dfrmt, dargs...)
  60. }
  61. }