kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/util/log/log.go (about)

     1  /*
     2   * Copyright 2023 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package log provides semantic log functions.
    18  package log
    19  
    20  import (
    21  	"context"
    22  	"log"
    23  	"reflect"
    24  )
    25  
    26  // Infof logs to the informational log.
    27  func Infof(msg string, args ...any) { log.Printf(msg, args...) }
    28  
    29  // Info logs to the informational log.
    30  func Info(args ...any) { log.Println(args...) }
    31  
    32  // Warningf logs to the warning log.
    33  func Warningf(msg string, args ...any) { log.Printf("WARNING: "+msg, args...) }
    34  
    35  // Warning logs to the warning log.
    36  func Warning(args ...any) { log.Print(append([]any{"WARNING:"}, args...)) }
    37  
    38  // Errorf logs to the error log.
    39  func Errorf(msg string, args ...any) { log.Printf("ERROR: "+msg, args...) }
    40  
    41  // Error logs to the warning log.
    42  func Error(args ...any) { log.Print(append([]any{"ERROR:"}, args...)) }
    43  
    44  // Fatalf logs to the error log and panics.
    45  func Fatalf(msg string, args ...any) { log.Fatalf(msg, args...) }
    46  
    47  // Fatal logs to the error log and panics.
    48  func Fatal(args ...any) { log.Fatal(args...) }
    49  
    50  // Fatalln logs to the error log and panics.
    51  func Fatalln(args ...any) { log.Fatalln(args...) }
    52  
    53  // Exit logs to the error log and exits.
    54  func Exit(args ...any) { log.Fatal(args...) }
    55  
    56  // Exitf logs to the error log and panics.
    57  func Exitf(msg string, args ...any) { log.Fatalf(msg, args...) }
    58  
    59  // InfoContext logs to the informational log with a Context.
    60  func InfoContext(ctx context.Context, args ...any) {
    61  	InfoContextf(ctx, defaultFormat(args), args...)
    62  }
    63  
    64  // WarningContext logs to the informational log with a Context.
    65  func WarningContext(ctx context.Context, args ...any) {
    66  	WarningContextf(ctx, defaultFormat(args), args...)
    67  }
    68  
    69  // ErrorContext logs to the informational log with a Context.
    70  func ErrorContext(ctx context.Context, args ...any) {
    71  	ErrorContextf(ctx, defaultFormat(args), args...)
    72  }
    73  
    74  // InfoContextf logs to the informational log with a Context.
    75  func InfoContextf(ctx context.Context, msg string, args ...any) { Infof(msg, args...) }
    76  
    77  // ErrorContextf logs to the error log with a Context.
    78  func ErrorContextf(ctx context.Context, msg string, args ...any) { Errorf(msg, args...) }
    79  
    80  // WarningContextf logs to the warning log with a Context.
    81  func WarningContextf(ctx context.Context, msg string, args ...any) { Warningf(msg, args...) }
    82  
    83  // defaultFormat returns a fmt.Printf format specifier that formats its
    84  // arguments as if they were passed to fmt.Print.
    85  func defaultFormat(args []any) string {
    86  	n := len(args)
    87  	switch n {
    88  	case 0:
    89  		return ""
    90  	case 1:
    91  		return "%v"
    92  	}
    93  
    94  	b := make([]byte, 0, n*3-1)
    95  	wasString := true // Suppress leading space.
    96  	for _, arg := range args {
    97  		isString := arg != nil && reflect.TypeOf(arg).Kind() == reflect.String
    98  		if wasString || isString {
    99  			b = append(b, "%v"...)
   100  		} else {
   101  			b = append(b, " %v"...)
   102  		}
   103  		wasString = isString
   104  	}
   105  	return string(b)
   106  }