Golang将自定义os.File添加到os.Stdout

时间:2019-01-20 11:58:08

标签: file go io stdout

每当我在控制台上打印内容时,我都试图将输出写入文件。似乎没有使用连续流的漂亮示例,而是读取单个值,因此我想出了以下代码:

package main

import (
    "fmt"
    "io"
    "os"
)

type ahhh struct {
    *os.File
    __writer io.Writer
}

func (me *ahhh) Write(b []byte) (n int, err error) {
    return me.__writer.Write(b)
}

func write_print_to_file(file_name string) {
    file, _ := os.OpenFile(file_name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)

    new_stdout := &ahhh{file, io.MultiWriter(file, os.Stdout)}

    os.Stdout = new_stdout
}

func main() {
    write_print_to_file("output.log")
    fmt.Println("Hello world!")
}

由于某些原因,os.Stdout = new_stdout不想工作。我相当确定这是一个允许的分配,因为我在FUSE文件系统中使用了类似的结构,因此我很好奇os.Stdout为什么不想接受继承的os.File。 / p>

或者,如果真的不可能对将连续数据流传输到文件有什么好的建议?

1 个答案:

答案 0 :(得分:1)

$ go doc os.stdout
var (
  Stdin  = NewFile(uintptr(syscall.Stdin), "/dev/stdin")
  Stdout = NewFile(uintptr(syscall.Stdout), "/dev/stdout")
  Stderr = NewFile(uintptr(syscall.Stderr), "/dev/stderr")
)
    Stdin, Stdout, and Stderr are open Files pointing to the standard input,
    standard output, and standard error file descriptors.

    Note that the Go runtime writes to standard error for panics and crashes;
    closing Stderr may cause those messages to go elsewhere, perhaps to a file
    opened later.

$ go doc os.newfile
func NewFile(fd uintptr, name string) *File
    NewFile returns a new File with the given file descriptor and name. The
    returned value will be nil if fd is not a valid file descriptor. On Unix
    systems, if the file descriptor is in non-blocking mode, NewFile will
    attempt to return a pollable File (one for which the SetDeadline methods
    work).

$ go doc -u os.file
type File struct {
  *file // os specific
}
    File represents an open file descriptor.

func Create(name string) (*File, error)
func NewFile(fd uintptr, name string) *File
func Open(name string) (*File, error)
func OpenFile(name string, flag int, perm FileMode) (*File, error)
func newFile(fd uintptr, name string, kind newFileKind) *File
func openFdAt(fd int, path string) (*File, error)
func openFileNolog(name string, flag int, perm FileMode) (*File, error)
func (f *File) Chdir() error
func (f *File) Chmod(mode FileMode) error
func (f *File) Chown(uid, gid int) error
func (f *File) Close() error
func (f *File) Fd() uintptr
func (f *File) Name() string
func (f *File) Read(b []byte) (n int, err error)
func (f *File) ReadAt(b []byte, off int64) (n int, err error)
func (f *File) Readdir(n int) ([]FileInfo, error)
func (f *File) Readdirnames(n int) (names []string, err error)
func (f *File) Seek(offset int64, whence int) (ret int64, err error)
func (f *File) SetDeadline(t time.Time) error
func (f *File) SetReadDeadline(t time.Time) error
func (f *File) SetWriteDeadline(t time.Time) error
func (f *File) Stat() (FileInfo, error)
func (f *File) Sync() error
func (f *File) SyscallConn() (syscall.RawConn, error)
func (f *File) Truncate(size int64) error
func (f *File) Write(b []byte) (n int, err error)
func (f *File) WriteAt(b []byte, off int64) (n int, err error)
func (f *File) WriteString(s string) (n int, err error)
func (f *File) checkValid(op string) error
func (f *File) chmod(mode FileMode) error
func (file File) close() error
func (f *File) pread(b []byte, off int64) (n int, err error)
func (f *File) pwrite(b []byte, off int64) (n int, err error)
func (f *File) read(b []byte) (n int, err error)
func (f *File) readdir(n int) (fi []FileInfo, err error)
func (f *File) readdirnames(n int) (names []string, err error)
func (f *File) seek(offset int64, whence int) (ret int64, err error)
func (f *File) setDeadline(t time.Time) error
func (f *File) setReadDeadline(t time.Time) error
func (f *File) setWriteDeadline(t time.Time) error
func (f *File) wrapErr(op string, err error) error
func (f *File) write(b []byte) (n int, err error)
type file struct {
  pfd         poll.FD
  name        string
  dirinfo     *dirInfo // nil unless directory being read
  nonblock    bool     // whether we set nonblocking mode
  stdoutOrErr bool     // whether this is stdout or stderr
}
    file is the real representation of *File. The extra level of indirection
    ensures that no clients of os can overwrite this data, which could cause the
    finalizer to close the wrong file descriptor.

func (file *file) close() error

  

我相当确定这应该是允许的作业。

new_stdout := &ahhh{file, io.MultiWriter(file, os.Stdout)}
os.Stdout = new_stdout

*os.File吗?否。为什么要允许它?