blob: bd54aa9eb8971dee22c799367f586ad045481b2f [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"
)
// #define LOGBUF_MAX (ZX_LOG_RECORD_MAX - sizeof(zx_log_record_t))
const logBufMax = 224
type Logger struct {
log zx.Log
logBufferArray [logBufMax]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) {
written := 0
for i := 0; i < len(data); i++ {
c := data[i]
written++
if c == '\n' {
_, err := f.log.Write(f.logBufferArray[:f.logBufferN])
f.logBufferN = 0
if err != nil {
return written, err
}
continue
}
if c < ' ' {
continue
}
f.logBufferArray[f.logBufferN] = c
f.logBufferN++
if f.logBufferN == len(f.logBufferArray) {
_, err := f.log.Write(f.logBufferArray[:f.logBufferN])
f.logBufferN = 0
if err != nil {
return written, err
}
}
}
return written, 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"}
}