| 1 |
// Copyright 2023 Google LLC |
| 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 |
|
| 15 |
package logg |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"log" |
| 20 |
"strings" |
| 21 |
) |
| 22 |
|
| 23 |
var ( |
| 24 |
// Quiet is the global variable toggled by -q flags. |
| 25 |
Quiet bool |
| 26 |
logBuffer []string |
| 27 |
) |
| 28 |
|
| 29 |
// Printf is a potentially quiet log.Printf. |
| 30 |
func Printf(format string, values ...interface{}) { |
| 31 |
if Quiet { |
| 32 |
logBuffer = append(logBuffer, fmt.Sprintf(format, values...)) |
| 33 |
return |
| 34 |
} |
| 35 |
log.Printf(format, values...) |
| 36 |
} |
| 37 |
|
| 38 |
// Fatal is a potentially really loud log.Fatal. |
| 39 |
// It dumps the log buffer if run in quiet mode. |
| 40 |
func Fatal(err error) { |
| 41 |
if Quiet && len(logBuffer) > 0 { |
| 42 |
log.Print(strings.Join(logBuffer, "\n")) |
| 43 |
} |
| 44 |
log.Fatal(err) |
| 45 |
} |
| 46 |
|
| 47 |
// Fatalf is a potentially really loud log.Fatalf. |
| 48 |
// It dumps the log buffer if run in quiet mode. |
| 49 |
func Fatalf(format string, values ...interface{}) { |
| 50 |
if Quiet && len(logBuffer) > 0 { |
| 51 |
log.Print(strings.Join(logBuffer, "\n")) |
| 52 |
} |
| 53 |
log.Fatalf(format, values...) |
| 54 |
} |