Skip to content

Commit

Permalink
Merge pull request cihub#113 from mlallaouret/feature/add_gzip_compre…
Browse files Browse the repository at this point in the history
…ssion_format

Add gzip compression format
  • Loading branch information
goodsign committed Apr 20, 2016
2 parents 6b24396 + ec7e252 commit a98235b
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 67 deletions.
7 changes: 3 additions & 4 deletions cfg_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,10 +1039,9 @@ func createRollingFileWriter(node *xmlNode, formatFromParent *formatter, formats
rArchivePath = rollingArchiveDefaultExplodedName

} else {
rArchivePath, ok = rollingArchiveTypesDefaultNames[rArchiveType]
if !ok {
return nil, fmt.Errorf("cannot get default filename for archive type = %v",
rArchiveType)
rArchivePath, err = rollingArchiveTypeDefaultName(rArchiveType, false)
if err != nil {
return nil, err
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion cfg_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,23 @@ func getParserTests() []parserTest {
testExpected.RootDispatcher = testHeadSplitter
parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})

testName = "Rolling file writer archive gzip"
testLogFileName = getTestFileName(testName, "")
testConfig = `
<seelog type="sync">
<outputs>
<rollingfile type="size" filename="` + testLogFileName + `" maxsize="100" maxrolls="5" archivetype="gzip"/>
</outputs>
</seelog>`
testExpected = new(configForParsing)
testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)
testExpected.Exceptions = nil
testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveGzip, "log.tar.gz", 100, 5, rollingNameModePostfix, false)
testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})
testExpected.LogType = syncloggerTypeFromString
testExpected.RootDispatcher = testHeadSplitter
parserTests = append(parserTests, parserTest{testName, testConfig, testExpected, false, nil})

testName = "Rolling file writer archive zip"
testLogFileName = getTestFileName(testName, "")
testConfig = `
Expand Down Expand Up @@ -320,7 +337,7 @@ func getParserTests() []parserTest {
testExpected = new(configForParsing)
testExpected.Constraints, _ = NewMinMaxConstraints(TraceLvl, CriticalLvl)
testExpected.Exceptions = nil
testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "old", 100, 5, rollingNameModePostfix , true)
testrollingFileWriter, _ = NewRollingFileWriterSize(testLogFileName, rollingArchiveZip, "old", 100, 5, rollingNameModePostfix, true)
testHeadSplitter, _ = NewSplitDispatcher(DefaultFormatter, []interface{}{testrollingFileWriter})
testExpected.LogType = syncloggerTypeFromString
testExpected.RootDispatcher = testHeadSplitter
Expand Down
115 changes: 115 additions & 0 deletions internals_fsutils.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package seelog

import (
"archive/tar"
"archive/zip"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -408,3 +410,116 @@ func createZip(archiveName string, files map[string][]byte) error {

return nil
}

func createTar(files map[string][]byte) ([]byte, error) {

// Create a buffer to write our archive to.
tarBuffer := new(bytes.Buffer)
tarWriter := tar.NewWriter(tarBuffer)

for fpath, fcont := range files {

header := &tar.Header{
Name: fpath,
Size: int64(len(fcont)),
Mode: defaultFilePermissions,
ModTime: time.Now(),
}

err := tarWriter.WriteHeader(header)

if err != nil {
return nil, err
}

_, err = tarWriter.Write(fcont)
if err != nil {
return nil, err
}
}
tarWriter.Close()

return tarBuffer.Bytes(), nil
}

func unTar(data []byte) (map[string][]byte, error) {
tarReader := tar.NewReader(bytes.NewReader(data))
files := make(map[string][]byte)

for {
header, err := tarReader.Next()
if err == io.EOF {
break
} else if err != nil {
return nil, err
}

info := header.FileInfo()
if info.IsDir() {
continue
}
buffer := new(bytes.Buffer)
_, err = io.Copy(buffer, tarReader)
files[header.Name] = buffer.Bytes()
if err != nil {
return nil, err
}
}

return files, nil

}

func createGzip(archiveName string, content []byte) error {

// Create a buffer to write our archive to.
// Make sure to check the error on Close.
gzipBuffer := new(bytes.Buffer)
gzipWriter := gzip.NewWriter(gzipBuffer)

_, err := gzipWriter.Write(content)
if err != nil {
return err
}
err = gzipWriter.Close()
if err != nil {
return err
}

return ioutil.WriteFile(archiveName, gzipBuffer.Bytes(), defaultFilePermissions)

}

func unGzip(filename string) ([]byte, error) {

file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

reader, err := gzip.NewReader(file)
if err != nil {
return nil, err
}

content := new(bytes.Buffer)
byteBuffer := make([]byte, 1000)
byteRead := 0
for {
byteRead, err = reader.Read(byteBuffer)
if err == io.EOF {
break
}
content.Write(byteBuffer[0:byteRead])

}
reader.Close()
return content.Bytes(), nil

}

func isTar(data []byte) bool {
tarMagicNumbers := []byte{'\x75', '\x73', '\x74', '\x61', '\x72'}
return bytes.Equal(data[257:262], tarMagicNumbers)
}
59 changes: 59 additions & 0 deletions internals_fsutils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package seelog

import (
"reflect"
"testing"
)

func TestGzip(t *testing.T) {
defer cleanupWriterTest(t)

files := make(map[string][]byte)
files["file1"] = []byte("I am a log")
err := createGzip("./gzip.gz", files["file1"])
if err != nil {
t.Fatal(err)
}

decompressedFile, err := unGzip("./gzip.gz")
if err != nil {
t.Fatal(err)
}

equal := reflect.DeepEqual(files["file1"], decompressedFile)
if !equal {
t.Fatal("gzip(ungzip(file)) should be equal to file")
}
}

func TestTar(t *testing.T) {
defer cleanupWriterTest(t)
files := make(map[string][]byte)
files["file1"] = []byte("I am a log")
files["file2"] = []byte("I am another log")
tar, err := createTar(files)
if err != nil {
t.Fatal(err)
}

resultFiles, err := unTar(tar)
if err != nil {
t.Fatal(err)
}
equal := reflect.DeepEqual(files, resultFiles)
if !equal {
t.Fatal("untar(tar(files)) should be equal to files")
}
}

func TestIsTar(t *testing.T) {
defer cleanupWriterTest(t)
files := make(map[string][]byte)
files["file1"] = []byte("I am a log")
files["file2"] = []byte("I am another log")
tar, _ := createTar(files)

if !isTar(tar) {
t.Fatal("tar(files) should be recognized as a tar file")
}
}
2 changes: 1 addition & 1 deletion writers_filewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewFileWriterTester(
}

func isWriterTestFile(fn string) bool {
return strings.Contains(fn, ".testlog") || strings.Contains(fn, ".zip")
return strings.Contains(fn, ".testlog") || strings.Contains(fn, ".zip") || strings.Contains(fn, ".gz")
}

func cleanupWriterTest(t *testing.T) {
Expand Down
Loading

0 comments on commit a98235b

Please sign in to comment.