| 634 | } |
| 635 | |
| 636 | func setTestLogging(logLevel LogLevel, caller string, logKeys ...LogKey) (teardownFn func()) { |
| 637 | if GlobalTestLoggingSet.IsTrue() { |
| 638 | // noop, test log level is already set globally |
| 639 | return func() { |
| 640 | if caller != "" { |
| 641 | InfofCtx(context.Background(), KeyAll, "%v: Reset logging", caller) |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | logger := consoleLogger.Load() |
| 647 | initialLogLevel := LevelInfo |
| 648 | initialLogKey := logKeyMask(KeyHTTP) |
| 649 | |
| 650 | // Check that a previous invocation has not forgotten to call teardownFn |
| 651 | if *logger.LogLevel != initialLogLevel || |
| 652 | *logger.LogKeyMask != *initialLogKey { |
| 653 | panic(fmt.Sprintf("Logging is in an unexpected state! Did a previous test forget to call the teardownFn of SetUpTestLogging?, LogLevel=%s, expected=%s; LogKeyMask=%s, expected=%s", logger.LogLevel, initialLogLevel, logger.LogKeyMask, initialLogKey)) |
| 654 | } |
| 655 | if errorLogger.Load() != nil { |
| 656 | panic("Logging is in an expected state, an earlier test possibly needed to call ResetGlobalTestLogging") |
| 657 | } |
| 658 | logger.LogLevel.Set(logLevel) |
| 659 | logger.LogKeyMask.Set(logKeyMask(logKeys...)) |
| 660 | updateExternalLoggers() |
| 661 | |
| 662 | return func() { |
| 663 | logger := consoleLogger.Load() |
| 664 | // Return logging to a default state |
| 665 | logger.LogLevel.Set(initialLogLevel) |
| 666 | logger.LogKeyMask.Set(initialLogKey) |
| 667 | updateExternalLoggers() |
| 668 | if caller != "" { |
| 669 | InfofCtx(context.Background(), KeyAll, "%v: Reset logging", caller) |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | // Make a deep copy from src into dst. |
| 675 | // Copied from https://github.com/getlantern/deepcopy, commit 7f45deb8130a0acc553242eb0e009e3f6f3d9ce3 (Apache 2 licensed) |