mode.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package xerrors
  2. import (
  3. "fmt"
  4. "github.com/ydb-platform/ydb/library/go/core/xerrors/internal/modes"
  5. "github.com/ydb-platform/ydb/library/go/x/xruntime"
  6. )
  7. func DefaultStackTraceMode() {
  8. modes.DefaultStackTraceMode()
  9. }
  10. func EnableFrames() {
  11. modes.SetStackTraceMode(modes.StackTraceModeFrames)
  12. }
  13. func EnableStacks() {
  14. modes.SetStackTraceMode(modes.StackTraceModeStacks)
  15. }
  16. func EnableStackThenFrames() {
  17. modes.SetStackTraceMode(modes.StackTraceModeStackThenFrames)
  18. }
  19. func EnableStackThenNothing() {
  20. modes.SetStackTraceMode(modes.StackTraceModeStackThenNothing)
  21. }
  22. func DisableStackTraces() {
  23. modes.SetStackTraceMode(modes.StackTraceModeNothing)
  24. }
  25. // newStackTrace returns stacktrace based on current mode and frames count
  26. func newStackTrace(skip int, err error) *xruntime.StackTrace {
  27. skip++
  28. m := modes.GetStackTraceMode()
  29. switch m {
  30. case modes.StackTraceModeFrames:
  31. return xruntime.NewFrame(skip)
  32. case modes.StackTraceModeStackThenFrames:
  33. if err != nil && StackTraceOfEffect(err) != nil {
  34. return xruntime.NewFrame(skip)
  35. }
  36. return _newStackTrace(skip)
  37. case modes.StackTraceModeStackThenNothing:
  38. if err != nil && StackTraceOfEffect(err) != nil {
  39. return nil
  40. }
  41. return _newStackTrace(skip)
  42. case modes.StackTraceModeStacks:
  43. return _newStackTrace(skip)
  44. case modes.StackTraceModeNothing:
  45. return nil
  46. }
  47. panic(fmt.Sprintf("unknown stack trace mode %d", m))
  48. }
  49. func MaxStackFrames16() {
  50. modes.SetStackFramesCountMax(modes.StackFramesCount16)
  51. }
  52. func MaxStackFrames32() {
  53. modes.SetStackFramesCountMax(modes.StackFramesCount32)
  54. }
  55. func MaxStackFrames64() {
  56. modes.SetStackFramesCountMax(modes.StackFramesCount64)
  57. }
  58. func MaxStackFrames128() {
  59. modes.SetStackFramesCountMax(modes.StackFramesCount128)
  60. }
  61. func _newStackTrace(skip int) *xruntime.StackTrace {
  62. skip++
  63. count := modes.GetStackFramesCountMax()
  64. switch count {
  65. case 16:
  66. return xruntime.NewStackTrace16(skip)
  67. case 32:
  68. return xruntime.NewStackTrace32(skip)
  69. case 64:
  70. return xruntime.NewStackTrace64(skip)
  71. case 128:
  72. return xruntime.NewStackTrace128(skip)
  73. }
  74. panic(fmt.Sprintf("unknown stack frames count %d", count))
  75. }