github.com/oarkflow/log@v1.0.78/console_unix.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package log
     5  
     6  import (
     7  	"os"
     8  	"syscall"
     9  	"unsafe"
    10  )
    11  
    12  func isTerminal(fd uintptr, os, arch string) bool {
    13  	var trap uintptr // SYS_IOCTL
    14  	switch os {
    15  	case "plan9", "js", "nacl":
    16  		return false
    17  	case "linux", "android":
    18  		switch arch {
    19  		case "amd64":
    20  			trap = 16
    21  		case "arm64":
    22  			trap = 29
    23  		case "mips", "mipsle":
    24  			trap = 4054
    25  		case "mips64", "mips64le":
    26  			trap = 5015
    27  		default:
    28  			trap = 54
    29  		}
    30  	default:
    31  		trap = 54
    32  	}
    33  
    34  	var req uintptr // TIOCGETA
    35  	switch os {
    36  	case "linux", "android":
    37  		switch arch {
    38  		case "ppc64", "ppc64le":
    39  			req = 0x402c7413
    40  		case "mips", "mipsle", "mips64", "mips64le":
    41  			req = 0x540d
    42  		default:
    43  			req = 0x5401
    44  		}
    45  	case "darwin":
    46  		switch arch {
    47  		case "amd64", "arm64":
    48  			req = 0x40487413
    49  		default:
    50  			req = 0x402c7413
    51  		}
    52  	default:
    53  		req = 0x402c7413
    54  	}
    55  
    56  	var termios [256]byte
    57  	_, _, err := syscall.Syscall6(trap, fd, req, uintptr(unsafe.Pointer(&termios[0])), 0, 0, 0)
    58  	// println(os, arch, err)
    59  	return err == 0
    60  }
    61  
    62  // WriteEntry implements Writer.
    63  func (w *ConsoleWriter) WriteEntry(e *Entry) (int, error) {
    64  	out := w.Writer
    65  	if out == nil {
    66  		out = os.Stderr
    67  	}
    68  	return w.write(out, e.buf)
    69  }