inet.af/netstack@v0.0.0-20220214151720-7585b01ddccf/log/glog.go (about)

     1  // Copyright 2018 The gVisor Authors.
     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 log
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"runtime"
    21  	"strings"
    22  	"time"
    23  )
    24  
    25  // GoogleEmitter is a wrapper that emits logs in a format compatible with
    26  // package github.com/golang/glog.
    27  type GoogleEmitter struct {
    28  	*Writer
    29  }
    30  
    31  // pid is used for the threadid component of the header.
    32  var pid = os.Getpid()
    33  
    34  // Emit emits the message, google-style.
    35  //
    36  // Log lines have this form:
    37  //   Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
    38  //
    39  // where the fields are defined as follows:
    40  //   L                A single character, representing the log level (eg 'I' for INFO)
    41  //   mm               The month (zero padded; ie May is '05')
    42  //   dd               The day (zero padded)
    43  //   hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
    44  //   threadid         The space-padded thread ID as returned by GetTID()
    45  //   file             The file name
    46  //   line             The line number
    47  //   msg              The user-supplied message
    48  //
    49  func (g GoogleEmitter) Emit(depth int, level Level, timestamp time.Time, format string, args ...interface{}) {
    50  	// Log level.
    51  	prefix := byte('?')
    52  	switch level {
    53  	case Debug:
    54  		prefix = byte('D')
    55  	case Info:
    56  		prefix = byte('I')
    57  	case Warning:
    58  		prefix = byte('W')
    59  	}
    60  
    61  	// Timestamp.
    62  	_, month, day := timestamp.Date()
    63  	hour, minute, second := timestamp.Clock()
    64  	microsecond := int(timestamp.Nanosecond() / 1000)
    65  
    66  	// 0 = this frame.
    67  	_, file, line, ok := runtime.Caller(depth + 1)
    68  	if ok {
    69  		// Trim any directory path from the file.
    70  		slash := strings.LastIndexByte(file, byte('/'))
    71  		if slash >= 0 {
    72  			file = file[slash+1:]
    73  		}
    74  	} else {
    75  		// We don't have a filename.
    76  		file = "???"
    77  		line = 0
    78  	}
    79  
    80  	// Generate the message.
    81  	message := fmt.Sprintf(format, args...)
    82  
    83  	// Emit the formatted result.
    84  	fmt.Fprintf(g.Writer, "%c%02d%02d %02d:%02d:%02d.%06d % 7d %s:%d] %s\n", prefix, int(month), day, hour, minute, second, microsecond, pid, file, line, message)
    85  }