postailer

package module
v0.0.0-...-daaa1ba Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2018 License: MIT Imports: 7 Imported by: 8

README

postailer

Build Status MIT License GoDoc

Description

Utility for reading files to start reading from the position read last time and also care file rotation.

Author

Songmu

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Postailer

type Postailer struct {
	// contains filtered or unexported fields
}

Postailer is main struct of postailer package

func Open

func Open(filepath, posfile string) (*Postailer, error)

Open the file with posfile

func (*Postailer) Close

func (pt *Postailer) Close() error

Close for io.Closer interface

func (*Postailer) Read

func (pt *Postailer) Read(p []byte) (int, error)

Read for io.Reader interface

Example
package main

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/Songmu/postailer"
)

func main() {
	appendFile := func(fname, content string) error {
		f, _ := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
		f.WriteString(content)
		return f.Close()
	}

	tmpd, _ := ioutil.TempDir("", "example")
	if tmpd != "" {
		defer os.RemoveAll(tmpd)
	}
	logfile := filepath.Join(tmpd, "log.log")
	posfile := logfile + ".json"

	appendFile(logfile, "1\n22\n")
	{
		pt, _ := postailer.Open(logfile, posfile)
		s := bufio.NewScanner(pt)
		for s.Scan() {
			fmt.Println(s.Text())
		}
		pt.Close()
	}

	appendFile(logfile, "333\n")
	{
		pt, _ := postailer.Open(logfile, posfile)
		s := bufio.NewScanner(pt)
		for s.Scan() {
			fmt.Println(s.Text())
		}
		pt.Close()
	}

	appendFile(logfile, "4444\n")
	os.Rename(logfile, logfile+".bak")
	appendFile(logfile, "newfile:1\nnewfile:22")
	{
		pt, _ := postailer.Open(logfile, posfile)
		s := bufio.NewScanner(pt)
		for s.Scan() {
			fmt.Println(s.Text())
		}
		pt.Close()
	}
}
Output:

1
22
333
4444
newfile:1
newfile:22

func (*Postailer) Seek

func (pt *Postailer) Seek(offset int64, whence int) (int64, error)

Seek for io.Seeker interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL