Skip to content

Commit

Permalink
Take steps to clarify API
Browse files Browse the repository at this point in the history
  • Loading branch information
mrb committed Jul 4, 2012
1 parent b273476 commit 70dfcf4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 50 deletions.
2 changes: 1 addition & 1 deletion err.go
@@ -1,6 +1,6 @@
package windex

import(
import (
"errors"
)

Expand Down
13 changes: 6 additions & 7 deletions indexer.go
@@ -1,18 +1,17 @@
package windex

type Indexer interface {
Parse() error
Flush() error
Parse() error
Flush() error
}

type StdoutIndexer struct {

}

func (i *Indexer) Parse() (err error) {

func (i *StdoutIndexer) Parse() (err error) {
return
}

func (i *Indexer) Flush() (err error) {

func (i *StdoutIndexer) Flush() (err error) {
return
}
28 changes: 20 additions & 8 deletions log_file.go
@@ -1,10 +1,14 @@
package windex

import (
"os"
)

type LogFile struct {
FileName string
File *os.File
FileSize int64
Cursor *LogFileCursor
FileName string
File *os.File
FileSize int64
Cursor *LogFileCursor
}

type LogFileCursor struct {
Expand All @@ -13,14 +17,21 @@ type LogFileCursor struct {
Delta int64
}

func NewLogFile(filename string, ){
&LogFile{
func NewLogFile(filename string) (log_file *LogFile, err error) {
file, err := os.Open(filename)

if err != nil {
return nil, err
}

defer file.Close()

return &LogFile{
File: file,
FileName: filename,
FileSize: 0,
Pair: &LogFileCursor{0, 0, 0},
}

}

func (m *LogFileCursor) setDelta() (err error) {
Expand All @@ -34,7 +45,7 @@ func (m *LogFileCursor) setDelta() (err error) {
return nil
}

func (log *Log) moveAndFlush() {
func (log *LogFile) moveAndFlush() {
if ok := log.movePair(); ok {
log.flush()
}
Expand Down Expand Up @@ -68,6 +79,7 @@ func (log *LogFile) updateFileSize() (err error) {
return nil
}

//
func (log *LogFile) flush() {
delta := log.Pair.Delta
file := log.File
Expand Down
44 changes: 32 additions & 12 deletions watcher.go
@@ -1,11 +1,28 @@
package windex

import(
import (
"github.com/howeyc/fsnotify"
)

type Watcher struct {
watcher *fsnotify.Watcher
}

func NewWatcher() (watcher *Watcher, err error) {
return &Watcher{
watcher: &fsnotify.Watcher,
}
}

func (log *LogFile) Watch() (err error) {
err = log.watchable()
if err != nil {
return
}

log.Watcher.Watch(log.FileName)

return
}

func (log *LogFile) watchable() (err error) {
Expand All @@ -24,17 +41,7 @@ func (log *LogFile) watchable() (err error) {
return
}

func (log *LogFile) Watch() (err error) {
err = log.watchable()
if err != nil {
return
}

log.Watcher.Watch(log.FileName)

return
}

/*
go func() {
for {
select {
Expand All @@ -48,5 +55,18 @@ go func() {
}
}
}()
*/
/*
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
logfile := &LogFile{}
if err = log.updateFileSize(); err != nil {
return nil, err
}
*/
44 changes: 22 additions & 22 deletions windex.go
@@ -1,51 +1,51 @@
package windex

import (
"os"
)

type Windex struct {
watcher *Watcher
logfile *LogFile
indexer *Indexer
logchan chan []byte
logfile *LogFile
watcher *Watcher
indexer *Indexer
log_to_index chan []byte
exit chan bool
}

/*
watched_index, err = windex.New("logfile01.log")
watched_index.Watch()
watched_index.Index()
windex, err = windex.New("logfile01.log")
err = windex.Watch()
err = windex.Index()
// or .Index(StdoutIndex) where StdoutIndex implements
// Index interface
exit <- windex.exit
Windex methods orchestrate between logfile and indexer,
getting signals from watcher to know when to act
[]byte channel between logfile and indexer
bool channel between windex and the outside world
*/
func New(filename string) (windex *Windex, err error) {
Watcher *fsnotify.Watcher

if err != nil {
logfile, err := NewLogFile(filename)
if err != nil {
return nil, err
}

defer file.Close()

watcher, err := fsnotify.NewWatcher()
watcher, err := NewWatcher()
if err != nil {
return nil, err
}

logfile := &LogFile{}
return &Windex{
logfile: logfile,
watcher: watcher,
}, nil
}

if err = log.updateFileSize(); err != nil {
return nil, err
}
func (windex *Windex) Watch() (err error) {

return log, nil
}

func (windex *Windex) Index() (err error) {

}

0 comments on commit 70dfcf4

Please sign in to comment.