Golang добавить пользовательский os.File в os.Stdout - PullRequest
0 голосов
/ 20 января 2019

Я пытаюсь записать вывод в файл всякий раз, когда что-то печатаю на консоль. Похоже, не было хороших примеров использования непрерывного потока, а, скорее, чтения одного значения, поэтому я придумал следующий код:

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.

В качестве альтернативы, если это действительно невозможно, есть ли хорошие предложения для создания непрерывного потока данных в файл?

1 Ответ

0 голосов
/ 20 января 2019
$ 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? Нет. Почему это должно быть разрешено?

...