@@ -29,13 +29,13 @@ import (
2929)
3030
3131type PatternAction struct {
32- filter string
33- cmd string
32+ Filter string
33+ Cmd string
3434}
3535
3636type RegexpAction struct {
37- filter * regexp.Regexp
38- cmd string
37+ Filter * regexp.Regexp
38+ Cmd string
3939}
4040
4141// valheim-logfilter is a string processor for log lines emitted by Valheim dedicated server.
@@ -112,7 +112,7 @@ func main() {
112112 containsFilters = append (containsFilters , PatternAction {varValue , cmd })
113113 } else if strings .HasPrefix (envVar , * envRegexp ) {
114114 if foundCmdInEnv {
115- glog .V (2 ).Infof ("On log lines matching regexp '%s' running '%s" , varValue , cmd )
115+ glog .V (2 ).Infof ("On log lines matching regexp '%s' running '%s' " , varValue , cmd )
116116 } else {
117117 glog .V (2 ).Infof ("Removing log lines matching regexp '%s'" , varValue )
118118 }
@@ -157,51 +157,51 @@ Input:
157157 logLine = string (v )
158158 }
159159 for _ , action := range matchFilters {
160- if logLine == action .filter {
160+ if logLine == action .Filter {
161161 if glog .V (5 ) {
162- glog .Infof ("Line matched '%s'" , action .filter )
162+ glog .Infof ("Line matched '%s'" , action .Filter )
163163 }
164- if removeLogLine (action .cmd , logLine ) {
164+ if removeLogLine (action .Cmd , logLine ) {
165165 continue Input
166166 }
167167 }
168168 }
169169 for _ , action := range prefixFilters {
170- if strings .HasPrefix (logLine , action .filter ) {
170+ if strings .HasPrefix (logLine , action .Filter ) {
171171 if glog .V (5 ) {
172- glog .Infof ("Line matched prefix filter '%s'" , action .filter )
172+ glog .Infof ("Line matched prefix filter '%s'" , action .Filter )
173173 }
174- if removeLogLine (action .cmd , logLine ) {
174+ if removeLogLine (action .Cmd , logLine ) {
175175 continue Input
176176 }
177177 }
178178 }
179179 for _ , action := range suffixFilters {
180- if strings .HasSuffix (logLine , action .filter ) {
180+ if strings .HasSuffix (logLine , action .Filter ) {
181181 if glog .V (5 ) {
182- glog .Infof ("Line matched suffix filter '%s'" , action .filter )
182+ glog .Infof ("Line matched suffix filter '%s'" , action .Filter )
183183 }
184- if removeLogLine (action .cmd , logLine ) {
184+ if removeLogLine (action .Cmd , logLine ) {
185185 continue Input
186186 }
187187 }
188188 }
189189 for _ , action := range containsFilters {
190- if strings .Contains (logLine , action .filter ) {
190+ if strings .Contains (logLine , action .Filter ) {
191191 if glog .V (5 ) {
192- glog .Infof ("Line contains filter '%s'" , action .filter )
192+ glog .Infof ("Line contains filter '%s'" , action .Filter )
193193 }
194- if removeLogLine (action .cmd , logLine ) {
194+ if removeLogLine (action .Cmd , logLine ) {
195195 continue Input
196196 }
197197 }
198198 }
199199 for _ , action := range regexpFilters {
200- if action .filter .MatchString (logLine ) {
200+ if action .Filter .MatchString (logLine ) {
201201 if glog .V (5 ) {
202- glog .Infof ("Line matched regexp filter '%s'" , action .filter )
202+ glog .Infof ("Line matched regexp filter '%s'" , action .Filter )
203203 }
204- if removeLogLine (action .cmd , logLine ) {
204+ if removeLogLine (action .Cmd , logLine ) {
205205 continue Input
206206 }
207207 }
@@ -243,17 +243,25 @@ func runHook(cmd string, logLine string) {
243243 stdin , err := subProcess .StdinPipe ()
244244 if err != nil {
245245 glog .Error (err )
246+ return
246247 }
247248
248249 subProcess .Stdout = os .Stdout
249250 subProcess .Stderr = os .Stderr
250251
251252 if err = subProcess .Start (); err != nil {
252253 glog .Error (err )
254+ return
253255 }
254256 glog .Flush ()
255257
256- io .WriteString (stdin , logLine + "\n " )
257- stdin .Close ()
258- subProcess .Wait ()
258+ if _ , err = io .WriteString (stdin , logLine + "\n " ); err != nil {
259+ glog .Errorf ("Failed to write to stdin: %v" , err )
260+ }
261+ if err = stdin .Close (); err != nil {
262+ glog .Errorf ("Failed to close stdin: %v" , err )
263+ }
264+ if err = subProcess .Wait (); err != nil {
265+ glog .Errorf ("Command failed: %v" , err )
266+ }
259267}
0 commit comments