Skip to content

Commit

Permalink
Merge pull request #3 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Release 5
  • Loading branch information
andyone committed Nov 26, 2015
2 parents 3f9e008 + 78db5e0 commit 45398b7
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 11 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
### Changelog

#### r5

* [log] Now buffered I/O must be enabled manually
* [log] Auto flushing for bufio

#### r4

* [system] Added json tags for User, Group and SessionInfo structs
Expand Down
53 changes: 45 additions & 8 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ type Logger struct {
PrefixError bool // Prefix for error messages
PrefixCrit bool // Prefix for critical error messages

file string
perms os.FileMode
fd *os.File
w *bufio.Writer
level int
file string
perms os.FileMode
fd *os.File
w *bufio.Writer
useBufIO bool
level int
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand Down Expand Up @@ -112,6 +113,11 @@ func Set(file string, perms os.FileMode) error {
return Global.Set(file, perms)
}

// EnableBufIO enable buffered I/O
func EnableBufIO(interval time.Duration) {
Global.EnableBufIO(interval)
}

// Flush write buffered data to file
func Flush() error {
return Global.Flush()
Expand Down Expand Up @@ -165,7 +171,10 @@ func (l *Logger) Reopen() error {
return errors.New("Output file is not set")
}

l.w.Flush()
if l.w != nil {
l.w.Flush()
}

l.fd.Close()

return l.Set(l.file, l.perms)
Expand All @@ -187,6 +196,17 @@ func (l *Logger) MinLevel(level int) {
l.level = level
}

// EnableBufIO enable buffered I/O support
func (l *Logger) EnableBufIO(interval time.Duration) {
l.useBufIO = true

if l.fd != nil {
l.w = bufio.NewWriter(l.fd)
}

go l.flushDaemon(interval)
}

// Set change logger output target
func (l *Logger) Set(file string, perms os.FileMode) error {
fd, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, perms)
Expand All @@ -198,14 +218,19 @@ func (l *Logger) Set(file string, perms os.FileMode) error {
// Flush data if writter exist
if l.w != nil {
l.w.Flush()
l.w = nil
}

if l.fd != nil {
l.fd.Close()
l.fd = nil
}

l.fd, l.file, l.perms = fd, file, perms
l.w = bufio.NewWriter(l.fd)

if l.useBufIO {
l.w = bufio.NewWriter(l.fd)
}

return nil
}
Expand All @@ -230,7 +255,11 @@ func (l *Logger) Print(level int, f string, a ...interface{}) (int, error) {
w = os.Stdout
}
} else {
w = l.w
if l.w != nil {
w = l.w
} else {
w = l.fd
}
}

var showPrefixes bool
Expand Down Expand Up @@ -324,6 +353,14 @@ func (l *Logger) Aux(f string, a ...interface{}) (int, error) {

// ////////////////////////////////////////////////////////////////////////////////// //

func (l *Logger) flushDaemon(interval time.Duration) {
for _ = range time.NewTicker(interval).C {
l.Flush()
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

func getTime() string {
return "[ " + time.Now().Format(TimeFormat) + " ]"
}
122 changes: 119 additions & 3 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"strings"
"testing"
"time"

"github.com/essentialkaos/ek/fsutil"
)
Expand All @@ -37,6 +38,16 @@ func (ls *LogSuite) SetUpSuite(c *C) {
ls.TempDir = c.MkDir()
}

func (ls *LogSuite) SetUpTest(c *C) {
Global = &Logger{
PrefixWarn: true,
PrefixError: true,
PrefixCrit: true,

level: INFO,
}
}

// ////////////////////////////////////////////////////////////////////////////////// //

func (ls *LogSuite) TestErrors(c *C) {
Expand Down Expand Up @@ -182,8 +193,6 @@ func (ls *LogSuite) TestWithoutPrefixes(c *C) {
l.Error("Test error %d\n", ERROR)
l.Crit("Test crit %d\n", CRIT)

l.Flush()

data, err := ioutil.ReadFile(logfile)

c.Assert(len(data), Not(Equals), 0)
Expand Down Expand Up @@ -250,7 +259,82 @@ func (ls *LogSuite) TestWithPrefixes(c *C) {
Crit("Test crit %d", CRIT)
Aux("Test aux %d", AUX)

Flush()
data, err := ioutil.ReadFile(logfile)

c.Assert(len(data), Not(Equals), 0)
c.Assert(err, IsNil)

dataSlice := strings.Split(string(data[:]), "\n")

c.Assert(len(dataSlice), Equals, 19)

c.Assert(dataSlice[0][28:], Equals, "[DEBUG] Test debug 0")
c.Assert(dataSlice[1][28:], Equals, "[INFO] Test info 1")
c.Assert(dataSlice[2][28:], Equals, "[WARNING] Test warn 2")
c.Assert(dataSlice[3][28:], Equals, "[ERROR] Test error 3")
c.Assert(dataSlice[4][28:], Equals, "[CRITICAL] Test crit 4")
c.Assert(dataSlice[5][28:], Equals, "Test aux 99")

c.Assert(dataSlice[6][28:], Equals, "[DEBUG] Test debug")
c.Assert(dataSlice[7][28:], Equals, "[INFO] Test info")
c.Assert(dataSlice[8][28:], Equals, "[WARNING] Test warn")
c.Assert(dataSlice[9][28:], Equals, "[ERROR] Test error")
c.Assert(dataSlice[10][28:], Equals, "[CRITICAL] Test crit")
c.Assert(dataSlice[11][28:], Equals, "Test aux")

c.Assert(dataSlice[12][28:], Equals, "[DEBUG] Test debug 0")
c.Assert(dataSlice[13][28:], Equals, "[INFO] Test info 1")
c.Assert(dataSlice[14][28:], Equals, "[WARNING] Test warn 2")
c.Assert(dataSlice[15][28:], Equals, "[ERROR] Test error 3")
c.Assert(dataSlice[16][28:], Equals, "[CRITICAL] Test crit 4")
c.Assert(dataSlice[17][28:], Equals, "Test aux 99")
}

func (ls *LogSuite) TestBufIODaemon(c *C) {
logfile := ls.TempDir + "/file3.log"
err := Set(logfile, 0644)

MinLevel(DEBUG)

c.Assert(Global, Not(IsNil))
c.Assert(err, IsNil)

Global.PrefixDebug = true
Global.PrefixInfo = true
Global.PrefixWarn = true
Global.PrefixError = true
Global.PrefixCrit = true

c.Assert(fsutil.GetPerm(logfile), Equals, os.FileMode(0644))

EnableBufIO(250 * time.Millisecond)

Print(DEBUG, "Test debug %d", DEBUG)
Print(INFO, "Test info %d", INFO)
Print(WARN, "Test warn %d", WARN)
Print(ERROR, "Test error %d", ERROR)
Print(CRIT, "Test crit %d", CRIT)
Print(AUX, "Test aux %d", AUX)

Print(DEBUG, "Test debug")
Print(INFO, "Test info")
Print(WARN, "Test warn")
Print(ERROR, "Test error")
Print(CRIT, "Test crit")
Print(AUX, "Test aux")

Debug("Test debug %d", DEBUG)
Info("Test info %d", INFO)
Warn("Test warn %d", WARN)
Error("Test error %d", ERROR)
Crit("Test crit %d", CRIT)
Aux("Test aux %d", AUX)

c.Assert(fsutil.GetSize(logfile), Equals, int64(0))

time.Sleep(500 * time.Millisecond)

c.Assert(fsutil.GetSize(logfile), Not(Equals), int64(0))

data, err := ioutil.ReadFile(logfile)

Expand Down Expand Up @@ -282,3 +366,35 @@ func (ls *LogSuite) TestWithPrefixes(c *C) {
c.Assert(dataSlice[16][28:], Equals, "[CRITICAL] Test crit 4")
c.Assert(dataSlice[17][28:], Equals, "Test aux 99")
}

func (ls *LogSuite) TestBufIO(c *C) {
logfile := ls.TempDir + "/file4.log"
err := Set(logfile, 0644)

c.Assert(Global, Not(IsNil))
c.Assert(err, IsNil)

c.Assert(fsutil.GetPerm(logfile), Equals, os.FileMode(0644))

EnableBufIO(time.Minute)

Aux("Test aux %d", AUX)

fileSize := fsutil.GetSize(logfile)

c.Assert(fileSize, Equals, int64(0))

Reopen()

fileSize = fsutil.GetSize(logfile)

c.Assert(fileSize, Not(Equals), int64(0))

Aux("Test aux %d", AUX)

c.Assert(fsutil.GetSize(logfile), Equals, fileSize)

Flush()

c.Assert(fsutil.GetSize(logfile), Not(Equals), fileSize)
}

0 comments on commit 45398b7

Please sign in to comment.