blob: d5723b6ff0923b51ba93478c6f02ebfd76b72ddc [file] [log] [blame]
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build fuchsia
package fdio
import (
"syscall/zx"
"syscall/zx/io"
)
type Logger struct {
log zx.Log
logBufferArray [1024]byte
logBufferN int
}
func NewLogger(log zx.Log) *Logger {
return &Logger{log: log}
}
// Clone makes a clone of the object.
func (f *Logger) Clone() (FDIO, error) {
logHandle := zx.Handle(f.log)
h, err := logHandle.Duplicate(zx.RightSameRights)
if err != nil {
return nil, err
}
return NewLogger(zx.Log(h)), nil
}
// Handle returns the underlying log handle as an untyped handle.
func (f *Logger) Handles() []zx.Handle {
return []zx.Handle{zx.Handle(f.log)}
}
// Close closes the object.
func (f *Logger) Close() error {
return f.log.Close()
}
// Sync implements FDIO for Logger.
func (f *Logger) Sync() error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// GetAttr implements FDIO for Logger.
func (f *Logger) GetAttr() (io.NodeAttributes, error) {
return io.NodeAttributes{}, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// SetAttr implements FDIO for Logger.
func (f *Logger) SetAttr(flags uint32, attr io.NodeAttributes) error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Ioctl implements FDIO for Logger.
func (f *Logger) Ioctl(op uint32, max uint64, in []byte, handles []zx.Handle) ([]byte, []zx.Handle, error) {
return nil, nil, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Read implements FDIO for Logger.
func (f *Logger) Read(data []byte) (int, error) {
return 0, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// ReadAt implements FDIO for Logger.
func (f *Logger) ReadAt(data []byte, off int64) (int, error) {
return 0, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Write implements FDIO for Logger.
func (f *Logger) Write(data []byte) (int, error) {
// Sys_log_write adds a newline to output buffers. To prevent fragmented logs, we buffer
// output until a newline character is encountered.
f.logBufferN += copy(f.logBufferArray[f.logBufferN:], data)
if f.logBufferArray[f.logBufferN-1] == '\n' {
n, err := f.log.Write(f.logBufferArray[:f.logBufferN])
// Flush buffer, regardless of status returned from write
f.logBufferN = 0
if err != nil {
return len(data), nil
}
return n, err
}
return len(data), nil
}
// WriteAt implements FDIO for Logger.
func (f *Logger) WriteAt(data []byte, off int64) (int, error) {
return 0, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Seek implements FDIO for Logger.
func (f *Logger) Seek(offset int64, whence int) (int64, error) {
return 0, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Truncate implements FDIO for Logger.
func (f *Logger) Truncate(length uint64) error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Open implements FDIO for Logger.
func (f *Logger) Open(path string, flags uint32, mode uint32) (FDIO, error) {
return nil, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Link implements FDIO for Logger.
func (f *Logger) Link(oldpath, newpath string) error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Rename implements FDIO for Logger.
func (f *Logger) Rename(oldpath, newpath string) error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Unlink implements FDIO for Logger.
func (f *Logger) Unlink(path string) error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// ReadDirents implements FDIO for Logger.
func (f *Logger) ReadDirents(max uint64) ([]byte, error) {
return nil, zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}
// Rewind implements FDIO for Logger.
func (f *Logger) Rewind() error {
return zx.Error{Status: zx.ErrNotSupported, Text: "fdio.logger"}
}