github.com/dubbogo/gost@v1.14.0/log/color.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  // package gxlog is based on log4go.
    19  // color.go provides colorful terminal log output functions.
    20  package gxlog
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  	"path/filepath"
    26  	"runtime"
    27  	"time"
    28  )
    29  
    30  import (
    31  	"github.com/mattn/go-isatty"
    32  )
    33  
    34  var (
    35  	// Normal colors
    36  	// NORMAL   = []byte{'\033', '0', 'm'}
    37  	NORMAL   = []byte{'\033', '0'}
    38  	NBlack   = []byte{'\033', '[', '3', '0', 'm'}
    39  	NRed     = []byte{'\033', '[', '3', '1', 'm'}
    40  	NGreen   = []byte{'\033', '[', '3', '2', 'm'}
    41  	NYellow  = []byte{'\033', '[', '3', '3', 'm'}
    42  	NBlue    = []byte{'\033', '[', '3', '4', 'm'}
    43  	NMagenta = []byte{'\033', '[', '3', '5', 'm'}
    44  	NCyan    = []byte{'\033', '[', '3', '6', 'm'}
    45  	NWhite   = []byte{'\033', '[', '3', '7', 'm'}
    46  	// Bright colors
    47  	BBlack                    = []byte{'\033', '[', '3', '0', ';', '1', 'm'}
    48  	BRed                      = []byte{'\033', '[', '3', '1', ';', '1', 'm'}
    49  	BGreen                    = []byte{'\033', '[', '3', '2', ';', '1', 'm'}
    50  	BYellow                   = []byte{'\033', '[', '3', '3', ';', '1', 'm'}
    51  	BBlue                     = []byte{'\033', '[', '3', '4', ';', '1', 'm'}
    52  	BMagenta                  = []byte{'\033', '[', '3', '5', ';', '1', 'm'}
    53  	BCyan                     = []byte{'\033', '[', '3', '6', ';', '1', 'm'}
    54  	BWhite                    = []byte{'\033', '[', '3', '7', ';', '1', 'm'}
    55  	UnderlineTwinkleHighLight = []byte{'\033', '[', '1', ';', '6', ';', '4', '0', 'm'}
    56  
    57  	reset = []byte{'\033', '[', '0', 'm'}
    58  )
    59  
    60  func funcFileLine() string {
    61  	tm := time.Unix(time.Now().Unix(), 0)
    62  	funcName, file, line, _ := runtime.Caller(3)
    63  	return "[" + tm.Format("2006-01-02/15:04:05 ") +
    64  		runtime.FuncForPC(funcName).Name() +
    65  		": " + filepath.Base(file) +
    66  		": " + fmt.Sprintf("%d", line) +
    67  		"] "
    68  }
    69  
    70  func CPrintf(color []byte, format string, args ...interface{}) {
    71  	logStr := fmt.Sprintf(format, args...)
    72  	if isatty.IsTerminal(os.Stdout.Fd()) {
    73  		fmt.Fprintf(os.Stdout, string(color)+funcFileLine()+"%s"+string(reset), logStr)
    74  	} else {
    75  		fmt.Fprintf(os.Stdout, "%s", logStr)
    76  	}
    77  }
    78  
    79  func CPrintfln(color []byte, format string, args ...interface{}) {
    80  	logStr := fmt.Sprintf(format, args...)
    81  	if isatty.IsTerminal(os.Stdout.Fd()) {
    82  		fmt.Fprintf(os.Stdout, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
    83  	} else {
    84  		fmt.Fprintf(os.Stdout, "%s\n", logStr)
    85  	}
    86  }
    87  
    88  func CEPrintf(color []byte, format string, args ...interface{}) {
    89  	logStr := fmt.Sprintf(format, args...)
    90  	if isatty.IsTerminal(os.Stdout.Fd()) {
    91  		fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset), logStr)
    92  	} else {
    93  		fmt.Fprintf(os.Stderr, "%s", logStr)
    94  	}
    95  }
    96  
    97  func CEPrintfln(color []byte, format string, args ...interface{}) {
    98  	logStr := fmt.Sprintf(format, args...)
    99  	if isatty.IsTerminal(os.Stdout.Fd()) {
   100  		fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
   101  	} else {
   102  		fmt.Fprintf(os.Stderr, "%s\n", logStr)
   103  	}
   104  }
   105  
   106  func CDebug(format string, args ...interface{}) {
   107  	CPrintfln(NORMAL, format, args...)
   108  }
   109  
   110  func CInfo(format string, args ...interface{}) {
   111  	CPrintfln(NGreen, format, args...)
   112  }
   113  
   114  func CWarn(format string, args ...interface{}) {
   115  	CEPrintfln(BMagenta, format, args...)
   116  }
   117  
   118  func CError(format string, args ...interface{}) {
   119  	CEPrintfln(NRed, format, args...)
   120  }
   121  
   122  func CFatal(format string, args ...interface{}) {
   123  	CEPrintfln(BRed, format, args...)
   124  }