@@ -8,11 +8,6 @@ import (
88 "reflect"
99 "sync"
1010
11- "io/ioutil"
12- "path/filepath"
13- "regexp"
14- "strings"
15-
1611 "github.com/go-stack/stack"
1712)
1813
@@ -75,111 +70,6 @@ func FileHandler(path string, fmtr Format) (Handler, error) {
7570 return closingHandler {f , StreamHandler (f , fmtr )}, nil
7671}
7772
78- // countingWriter wraps a WriteCloser object in order to count the written bytes.
79- type countingWriter struct {
80- w io.WriteCloser // the wrapped object
81- count uint // number of bytes written
82- }
83-
84- // Write increments the byte counter by the number of bytes written.
85- // Implements the WriteCloser interface.
86- func (w * countingWriter ) Write (p []byte ) (n int , err error ) {
87- n , err = w .w .Write (p )
88- w .count += uint (n )
89- return n , err
90- }
91-
92- // Close implements the WriteCloser interface.
93- func (w * countingWriter ) Close () error {
94- return w .w .Close ()
95- }
96-
97- // prepFile opens the log file at the given path, and cuts off the invalid part
98- // from the end, because the previous execution could have been finished by interruption.
99- // Assumes that every line ended by '\n' contains a valid log record.
100- func prepFile (path string ) (* countingWriter , error ) {
101- f , err := os .OpenFile (path , os .O_RDWR | os .O_APPEND , 0600 )
102- if err != nil {
103- return nil , err
104- }
105- _ , err = f .Seek (- 1 , io .SeekEnd )
106- if err != nil {
107- return nil , err
108- }
109- buf := make ([]byte , 1 )
110- var cut int64
111- for {
112- if _ , err := f .Read (buf ); err != nil {
113- return nil , err
114- }
115- if buf [0 ] == '\n' {
116- break
117- }
118- if _ , err = f .Seek (- 2 , io .SeekCurrent ); err != nil {
119- return nil , err
120- }
121- cut ++
122- }
123- fi , err := f .Stat ()
124- if err != nil {
125- return nil , err
126- }
127- ns := fi .Size () - cut
128- if err = f .Truncate (ns ); err != nil {
129- return nil , err
130- }
131- return & countingWriter {w : f , count : uint (ns )}, nil
132- }
133-
134- // RotatingFileHandler returns a handler which writes log records to file chunks
135- // at the given path. When a file's size reaches the limit, the handler creates
136- // a new file named after the timestamp of the first log record it will contain.
137- func RotatingFileHandler (path string , limit uint , formatter Format ) (Handler , error ) {
138- if err := os .MkdirAll (path , 0700 ); err != nil {
139- return nil , err
140- }
141- files , err := ioutil .ReadDir (path )
142- if err != nil {
143- return nil , err
144- }
145- re := regexp .MustCompile (`\.log$` )
146- last := len (files ) - 1
147- for last >= 0 && (! files [last ].Mode ().IsRegular () || ! re .MatchString (files [last ].Name ())) {
148- last --
149- }
150- var counter * countingWriter
151- if last >= 0 && files [last ].Size () < int64 (limit ) {
152- // Open the last file, and continue to write into it until it's size reaches the limit.
153- if counter , err = prepFile (filepath .Join (path , files [last ].Name ())); err != nil {
154- return nil , err
155- }
156- }
157- if counter == nil {
158- counter = new (countingWriter )
159- }
160- h := StreamHandler (counter , formatter )
161-
162- return FuncHandler (func (r * Record ) error {
163- if counter .count > limit {
164- counter .Close ()
165- counter .w = nil
166- }
167- if counter .w == nil {
168- f , err := os .OpenFile (
169- filepath .Join (path , fmt .Sprintf ("%s.log" , strings .Replace (r .Time .Format ("060102150405.00" ), "." , "" , 1 ))),
170- os .O_CREATE | os .O_APPEND | os .O_WRONLY ,
171- 0600 ,
172- )
173- if err != nil {
174- return err
175- }
176- counter .w = f
177- counter .count = 0
178- }
179- return h .Log (r )
180- }), nil
181- }
182-
18373// NetHandler opens a socket to the given address and writes records
18474// over the connection.
18575func NetHandler (network , addr string , fmtr Format ) (Handler , error ) {
0 commit comments