-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
156 lines (132 loc) · 3.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
)
func main() {
maxSize := flag.Int64("s", 0, "Maximum size of the log file in bytes (use either -s or -l)")
maxLines := flag.Int("l", 0, "Maximum number of lines in the log file (use either -s or -l)")
logFile := flag.String("f", "", "Log file path")
verbose := flag.Bool("v", false, "Enable verbose output")
flag.Parse()
if *verbose {
fmt.Fprintf(os.Stderr, "Verbose mode enabled\n")
fmt.Fprintf(os.Stderr, "Log file: %s\n", *logFile)
if *maxSize > 0 {
fmt.Fprintf(os.Stderr, "Max size: %d bytes\n", *maxSize)
}
if *maxLines > 0 {
fmt.Fprintf(os.Stderr, "Max lines: %d\n", *maxLines)
}
}
if *logFile == "" || (*maxSize == 0 && *maxLines == 0) {
fmt.Println("Usage: ringlog -s <max_size_bytes> or -l <max_lines> -f <log_file>\n\nPipe-friendly utility to manage log files by capping size or line count.")
os.Exit(1)
}
file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening log file: %v\n", err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
if *verbose {
fmt.Fprintf(os.Stderr, "Writing line to log: %s\n", line)
}
if _, err := file.WriteString(line + "\n"); err != nil {
fmt.Fprintf(os.Stderr, "Error writing to log file: %v\n", err)
os.Exit(1)
}
// Check size or lines and truncate if needed
if *maxSize > 0 {
if *verbose {
fmt.Fprintf(os.Stderr, "Checking if truncation by size is needed\n")
}
checkAndTruncateBySize(*logFile, *maxSize)
} else if *maxLines > 0 {
if *verbose {
fmt.Fprintf(os.Stderr, "Checking if truncation by lines is needed\n")
}
checkAndTruncateByLines(*logFile, *maxLines)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading stdin: %v\n", err)
}
}
func checkAndTruncateBySize(filePath string, maxSize int64) {
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening log file for truncation: %v\n", err)
return
}
defer file.Close()
info, err := file.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "Error stating file: %v\n", err)
return
}
if info.Size() <= maxSize {
return
}
// Seek to the point where we want to start reading
offset := info.Size() - maxSize
_, err = file.Seek(offset, io.SeekStart)
if err != nil {
fmt.Fprintf(os.Stderr, "Error seeking file for truncation: %v\n", err)
return
}
// Read the remaining content
remainingContent := make([]byte, maxSize)
bytesRead, err := file.Read(remainingContent)
if err != nil && err != io.EOF {
fmt.Fprintf(os.Stderr, "Error reading file for truncation: %v\n", err)
return
}
// Truncate and rewrite the file
err = file.Truncate(0)
if err != nil {
fmt.Fprintf(os.Stderr, "Error truncating file: %v\n", err)
return
}
file.Seek(0, 0)
_, err = file.Write(remainingContent[:bytesRead])
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing truncated content: %v\n", err)
}
}
func checkAndTruncateByLines(filePath string, maxLines int) {
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening log file for truncation: %v\n", err)
return
}
defer file.Close()
file.Seek(0, 0)
scanner := bufio.NewScanner(file)
lines := []string{}
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if len(lines) <= maxLines {
return
}
lines = lines[len(lines)-maxLines:]
err = file.Truncate(0)
if err != nil {
fmt.Fprintf(os.Stderr, "Error truncating file: %v\n", err)
return
}
file.Seek(0, 0)
for _, line := range lines {
if _, err := file.WriteString(line + "\n"); err != nil {
fmt.Fprintf(os.Stderr, "Error writing to log file: %v\n", err)
return
}
}
}