| 1 |
// Copyright 2017 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 fmt provides template functions for formatting strings. |
| 15 |
package fmt |
| 16 |
|
| 17 |
import ( |
| 18 |
_fmt "fmt" |
| 19 |
"sort" |
| 20 |
|
| 21 |
"github.com/bep/logg" |
| 22 |
"github.com/gohugoio/hugo/common/loggers" |
| 23 |
"github.com/gohugoio/hugo/deps" |
| 24 |
"github.com/spf13/cast" |
| 25 |
) |
| 26 |
|
| 27 |
// New returns a new instance of the fmt-namespaced template functions. |
| 28 |
func New(d *deps.Deps) *Namespace { |
| 29 |
ns := &Namespace{ |
| 30 |
logger: d.Log, |
| 31 |
} |
| 32 |
|
| 33 |
d.BuildStartListeners.Add(func(...any) bool { |
| 34 |
ns.logger.Reset() |
| 35 |
return false |
| 36 |
}) |
| 37 |
|
| 38 |
return ns |
| 39 |
} |
| 40 |
|
| 41 |
// Namespace provides template functions for the "fmt" namespace. |
| 42 |
type Namespace struct { |
| 43 |
logger loggers.Logger |
| 44 |
} |
| 45 |
|
| 46 |
// Print returns a string representation of args. |
| 47 |
func (ns *Namespace) Print(args ...any) string { |
| 48 |
return _fmt.Sprint(args...) |
| 49 |
} |
| 50 |
|
| 51 |
// Printf returns string representation of args formatted with the layout in format. |
| 52 |
func (ns *Namespace) Printf(format string, args ...any) string { |
| 53 |
return _fmt.Sprintf(format, args...) |
| 54 |
} |
| 55 |
|
| 56 |
// Println returns string representation of args ending with a newline. |
| 57 |
func (ns *Namespace) Println(args ...any) string { |
| 58 |
return _fmt.Sprintln(args...) |
| 59 |
} |
| 60 |
|
| 61 |
// Errorf formats args according to a format specifier and logs an ERROR. |
| 62 |
// It returns an empty string. |
| 63 |
func (ns *Namespace) Errorf(format string, args ...any) string { |
| 64 |
ns.logger.Errorf(format, args...) |
| 65 |
return "" |
| 66 |
} |
| 67 |
|
| 68 |
// Erroridf formats args according to a format specifier and logs an ERROR and |
| 69 |
// an information text that the error with the given id can be suppressed in config. |
| 70 |
// It returns an empty string. |
| 71 |
func (ns *Namespace) Erroridf(id, format string, args ...any) string { |
| 72 |
ns.logger.Erroridf(id, format, args...) |
| 73 |
return "" |
| 74 |
} |
| 75 |
|
| 76 |
// Warnf formats args according to a format specifier and logs a WARNING. |
| 77 |
// It returns an empty string. |
| 78 |
func (ns *Namespace) Warnf(format string, args ...any) string { |
| 79 |
ns.logger.Warnf(format, args...) |
| 80 |
return "" |
| 81 |
} |
| 82 |
|
| 83 |
// Warnidf formats args according to a format specifier and logs an WARNING and |
| 84 |
// an information text that the warning with the given id can be suppressed in config. |
| 85 |
// It returns an empty string. |
| 86 |
func (ns *Namespace) Warnidf(id, format string, args ...any) string { |
| 87 |
ns.logger.Warnidf(id, format, args...) |
| 88 |
return "" |
| 89 |
} |
| 90 |
|
| 91 |
// Warnmf is experimental and subject to change at any time. |
| 92 |
func (ns *Namespace) Warnmf(m any, format string, args ...any) string { |
| 93 |
return ns.logmf(ns.logger.Warn(), m, format, args...) |
| 94 |
} |
| 95 |
|
| 96 |
// Errormf is experimental and subject to change at any time. |
| 97 |
func (ns *Namespace) Errormf(m any, format string, args ...any) string { |
| 98 |
return ns.logmf(ns.logger.Error(), m, format, args...) |
| 99 |
} |
| 100 |
|
| 101 |
func (ns *Namespace) logmf(l logg.LevelLogger, m any, format string, args ...any) string { |
| 102 |
mm := cast.ToStringMap(m) |
| 103 |
fields := make(logg.Fields, len(mm)) |
| 104 |
i := 0 |
| 105 |
for k, v := range mm { |
| 106 |
fields[i] = logg.Field{Name: k, Value: v} |
| 107 |
i++ |
| 108 |
} |
| 109 |
// Sort the fields to make the output deterministic. |
| 110 |
sort.Slice(fields, func(i, j int) bool { |
| 111 |
return fields[i].Name < fields[j].Name |
| 112 |
}) |
| 113 |
|
| 114 |
l.WithFields(fields).Logf(format, args...) |
| 115 |
|
| 116 |
return "" |
| 117 |
} |