|
| 1 | +// Copyright 2025 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 hdebug |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "strings" |
| 19 | + "time" |
| 20 | + |
| 21 | + "github.com/gohugoio/hugo/common/types" |
| 22 | + "github.com/gohugoio/hugo/htesting" |
| 23 | +) |
| 24 | + |
| 25 | +// Printf is a debug print function that should be removed before committing code to the repository. |
| 26 | +func Printf(format string, args ...any) { |
| 27 | + panicIfRealCI() |
| 28 | + if len(args) == 1 && !strings.Contains(format, "%") { |
| 29 | + format = format + ": %v" |
| 30 | + } |
| 31 | + if !strings.HasSuffix(format, "\n") { |
| 32 | + format = format + "\n" |
| 33 | + } |
| 34 | + fmt.Printf(format, args...) |
| 35 | + time.Sleep(10 * time.Millisecond) // Give the output a chance to be printed before the program exits. |
| 36 | +} |
| 37 | + |
| 38 | +func AssertNotNil(a ...any) { |
| 39 | + panicIfRealCI() |
| 40 | + for _, v := range a { |
| 41 | + if types.IsNil(v) { |
| 42 | + panic("hdebug.AssertNotNil: value is nil") |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func Panicf(format string, args ...any) { |
| 48 | + panicIfRealCI() |
| 49 | + // fmt.Println(stack()) |
| 50 | + if len(args) == 1 && !strings.Contains(format, "%") { |
| 51 | + format = format + ": %v" |
| 52 | + } |
| 53 | + if !strings.HasSuffix(format, "\n") { |
| 54 | + format = format + "\n" |
| 55 | + } |
| 56 | + panic(fmt.Sprintf(format, args...)) |
| 57 | +} |
| 58 | + |
| 59 | +func panicIfRealCI() { |
| 60 | + if htesting.IsRealCI() { |
| 61 | + panic("This debug statement should be removed before committing code!") |
| 62 | + } |
| 63 | +} |
0 commit comments