Added more comments
This commit is contained in:
parent
0cc9b76d26
commit
91e8800b6b
|
@ -18,26 +18,16 @@ var (
|
||||||
|
|
||||||
// FileRotator writes bytes to a rotated set of files
|
// FileRotator writes bytes to a rotated set of files
|
||||||
type FileRotator struct {
|
type FileRotator struct {
|
||||||
// MaxFiles is the maximum number of rotated files allowed in a path
|
MaxFiles int // MaxFiles is the maximum number of rotated files allowed in a path
|
||||||
MaxFiles int
|
FileSize int64 // FileSize is the size a rotated file is allowed to grow
|
||||||
// FileSize is the size a rotated file is allowed to grow
|
|
||||||
FileSize int64
|
|
||||||
|
|
||||||
// path is the path on the file system where the rotated set of files are
|
path string // path is the path on the file system where the rotated set of files are opened
|
||||||
// opened
|
baseFileName string // baseFileName is the base file name of the rotated files
|
||||||
path string
|
logFileIdx int // logFileIdx is the current index of the rotated files
|
||||||
// baseFileName is the base file name of the rotated files
|
oldestLogFileIdx int // oldestLogFileIdx is the index of the oldest log file in a path
|
||||||
baseFileName string
|
|
||||||
|
|
||||||
// logFileIdx is the current index of the rotated files
|
currentFile *os.File // currentFile is the file that is currently getting written
|
||||||
logFileIdx int
|
currentWr int64 // currentWr is the number of bytes written to the current file
|
||||||
// oldestLogFileIdx is the index of the oldest log file in a path
|
|
||||||
oldestLogFileIdx int
|
|
||||||
|
|
||||||
// currentFile is the file that is currently getting written
|
|
||||||
currentFile *os.File
|
|
||||||
// currentWr is the number of bytes written to the current file
|
|
||||||
currentWr int64
|
|
||||||
|
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
purgeCh chan struct{}
|
purgeCh chan struct{}
|
||||||
|
@ -70,7 +60,7 @@ func (f *FileRotator) Write(p []byte) (n int, err error) {
|
||||||
var nw int
|
var nw int
|
||||||
|
|
||||||
for n < len(p) {
|
for n < len(p) {
|
||||||
// check if we still have space in the current file, otherwise close and
|
// Check if we still have space in the current file, otherwise close and
|
||||||
// open the next file
|
// open the next file
|
||||||
if f.currentWr >= f.FileSize {
|
if f.currentWr >= f.FileSize {
|
||||||
f.currentFile.Close()
|
f.currentFile.Close()
|
||||||
|
@ -78,23 +68,25 @@ func (f *FileRotator) Write(p []byte) (n int, err error) {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// calculate the remaining size on this file
|
// Calculate the remaining size on this file
|
||||||
remainingSize := f.FileSize - f.currentWr
|
remainingSize := f.FileSize - f.currentWr
|
||||||
// check if the number of bytes that we have to write is less than the
|
|
||||||
|
// Check if the number of bytes that we have to write is less than the
|
||||||
// remaining size of the file
|
// remaining size of the file
|
||||||
if remainingSize < int64(len(p[n:])) {
|
if remainingSize < int64(len(p[n:])) {
|
||||||
// write the number of bytes that we can write on the current file
|
// Write the number of bytes that we can write on the current file
|
||||||
li := int64(n) + remainingSize
|
li := int64(n) + remainingSize
|
||||||
nw, err = f.currentFile.Write(p[n:li])
|
nw, err = f.currentFile.Write(p[n:li])
|
||||||
} else {
|
} else {
|
||||||
// write all the bytes in the current file
|
// Write all the bytes in the current file
|
||||||
nw, err = f.currentFile.Write(p[n:])
|
nw, err = f.currentFile.Write(p[n:])
|
||||||
}
|
}
|
||||||
|
|
||||||
// increment the number of bytes written to the current file in this
|
// Increment the number of bytes written so far in this method
|
||||||
// session
|
// invocation
|
||||||
n += nw
|
n += nw
|
||||||
// increment the total number of bytes in the file
|
|
||||||
|
// Increment the total number of bytes in the file
|
||||||
f.currentWr += int64(n)
|
f.currentWr += int64(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -111,11 +103,7 @@ func (f *FileRotator) nextFile() error {
|
||||||
nextFileIdx += 1
|
nextFileIdx += 1
|
||||||
logFileName := filepath.Join(f.path, fmt.Sprintf("%s.%d", f.baseFileName, nextFileIdx))
|
logFileName := filepath.Join(f.path, fmt.Sprintf("%s.%d", f.baseFileName, nextFileIdx))
|
||||||
if fi, err := os.Stat(logFileName); err == nil {
|
if fi, err := os.Stat(logFileName); err == nil {
|
||||||
if fi.IsDir() {
|
if fi.IsDir() || fi.Size() >= f.FileSize {
|
||||||
nextFileIdx += 1
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if fi.Size() >= f.FileSize {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,7 +123,7 @@ func (f *FileRotator) nextFile() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// lastFile finds out the roated file with the largest index in a path.
|
// lastFile finds out the rotated file with the largest index in a path.
|
||||||
func (f *FileRotator) lastFile() error {
|
func (f *FileRotator) lastFile() error {
|
||||||
finfos, err := ioutil.ReadDir(f.path)
|
finfos, err := ioutil.ReadDir(f.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -37,7 +37,6 @@ func (s *SyslogServer) Start() {
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
connection, err := s.listener.Accept()
|
connection, err := s.listener.Accept()
|
||||||
s.logger.Printf("DIPTANU ACCEPTED CON")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Printf("[ERROR] logcollector.server: error in accepting connection: %v", err)
|
s.logger.Printf("[ERROR] logcollector.server: error in accepting connection: %v", err)
|
||||||
continue
|
continue
|
||||||
|
@ -52,20 +51,18 @@ func (s *SyslogServer) read(connection net.Conn) {
|
||||||
defer connection.Close()
|
defer connection.Close()
|
||||||
scanner := bufio.NewScanner(bufio.NewReader(connection))
|
scanner := bufio.NewScanner(bufio.NewReader(connection))
|
||||||
|
|
||||||
LOOP:
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-s.doneCh:
|
case <-s.doneCh:
|
||||||
break LOOP
|
return
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
if scanner.Scan() {
|
if scanner.Scan() {
|
||||||
b := scanner.Bytes()
|
b := scanner.Bytes()
|
||||||
s.logger.Printf("DIPTANU READ BYTES %v", b)
|
|
||||||
msg := s.parser.Parse(b)
|
msg := s.parser.Parse(b)
|
||||||
s.messages <- msg
|
s.messages <- msg
|
||||||
} else {
|
} else {
|
||||||
break LOOP
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -112,10 +112,10 @@ func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogColl
|
||||||
// otherwise all messages go to stdout
|
// otherwise all messages go to stdout
|
||||||
if logParts.Severity == syslog.LOG_ERR {
|
if logParts.Severity == syslog.LOG_ERR {
|
||||||
s.lre.Write(logParts.Message)
|
s.lre.Write(logParts.Message)
|
||||||
s.lre.Write([]byte("\n"))
|
s.lre.Write([]byte{'\n'})
|
||||||
} else {
|
} else {
|
||||||
s.lro.Write(logParts.Message)
|
s.lro.Write(logParts.Message)
|
||||||
s.lro.Write([]byte("\n"))
|
s.lro.Write([]byte{'\n'})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}(channel)
|
}(channel)
|
||||||
|
|
Loading…
Reference in New Issue