示例#1
0
// Gets all received lines from the tracker.
func (l *LogBuffer) Lines() []string {
	l.mutex.Lock()
	defer l.mutex.Unlock()

	buffer := bytes.NewBuffer(nil)
	// Ignore err, there is nothing we can do with it anyway.
	output, _ := logray.NewIOWriterOutput(buffer, formatStr, "auto")
	for _, ld := range l.buffer {
		// Ignore errors.. there is nothing we can do about it anyway.
		output.Write(ld)
	}
	return strings.Split(string(buffer.Bytes()), "\n")
}
示例#2
0
// Dumps all the lines in the buffer to stdout.
func (l *LogBuffer) DumpToStdout() {
	l.mutex.Lock()
	defer l.mutex.Unlock()

	output, err := logray.NewIOWriterOutput(os.Stdout, formatStr, "auto")
	if err != nil {
		fmt.Printf("Error from logray.NewIOWriterOutput: %s\n", err)
		return
	}
	for _, ld := range l.buffer {
		if err := output.Write(ld); err != nil {
			fmt.Printf("Error in Write(): %s\n", err)
		}
	}
}
示例#3
0
func (l *LogBuffer) NewLines() []string {
	l.mutex.Lock()
	defer l.mutex.Unlock()

	buffer := bytes.NewBuffer(nil)
	// Ignore err, there is nothing we can do with it anyway.
	output, _ := logray.NewIOWriterOutput(buffer, formatStr, "auto")
	for i, ld := range l.buffer {
		if i < l.lastRead {
			continue
		}
		// Ignore errors.. there is nothing we can do about it anyway.
		output.Write(ld)
	}
	l.lastRead = len(l.buffer)
	lines := strings.Split(string(buffer.Bytes()), "\n")
	// Remove empty last line.
	if len(lines[len(lines)-1]) == 0 {
		lines = lines[:len(lines)-1]
	}
	return lines
}
示例#4
0
func (l *LogBuffer) DumpToFile(path string) {
	l.mutex.Lock()
	defer l.mutex.Unlock()
	var err error
	var w io.WriteCloser

	if w, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644); err != nil {
		fmt.Fprintf(os.Stderr, "Error creating log file %q, outputting on STDERR", path)
		w = os.Stderr
	}

	var output logray.Output
	if output, err = logray.NewIOWriterOutput(w, formatStr, "auto"); err != nil {
		fmt.Printf("Error from logray.NewIOWriterOutput: %s\n", err)
		return
	}

	for _, ld := range l.buffer {
		if err := output.Write(ld); err != nil {
			fmt.Fprintf(os.Stderr, "Error in Write(): %s\n", err)
		}
	}
}