starting to look promising. Squash this commit
[arvados.git] / lib / crunchrun / logging.go
index 050894383d757a1e90e79999ef9fa8f2f08b9f01..a1160cf2e2c3cdd0a7fcc07618a65d911bdf52b1 100644 (file)
@@ -24,6 +24,7 @@ type Timestamper func(t time.Time) string
 
 // Logging plumbing:
 //
+// (optionally) RegexpMatchingWriter ->
 // ThrottledLogger.Logger -> ThrottledLogger.Write ->
 // ThrottledLogger.buf -> ThrottledLogger.flusher ->
 // ArvLogWriter.Write -> CollectionFileWriter.Write | Api.Create
@@ -189,6 +190,35 @@ func NewThrottledLogger(writer io.WriteCloser) *ThrottledLogger {
        return tl
 }
 
+// regexpMatchingWriter compares every line and sends it to the underlying
+// Writer. Function Notify() will be triggered if any regexp matches.
+type regexpMatchingWriter struct {
+       io.WriteCloser
+       Notify    func(regexp string, line string)
+       Regexps   []string
+       setupOnce sync.Once
+       matchers  []*regexp.Regexp
+}
+
+func (w *regexpMatchingWriter) Write(p []byte) (n int, err error) {
+       // set up matchers from Regexps on first Write()
+       w.setupOnce.Do(func() {
+               w.matchers = make([]*regexp.Regexp, len(w.Regexps))
+               for i, rstring := range w.Regexps {
+                       w.matchers[i] = regexp.MustCompile(rstring)
+               }
+       })
+
+       // If any regexp matches then call w.Notify()...
+       for _, matcher := range w.matchers {
+               if matcher.Match(p) {
+                       w.Notify(matcher.String(), string(p))
+               }
+       }
+
+       return w.WriteCloser.Write(p)
+}
+
 // Log throttling rate limiting config parameters
 var crunchLimitLogBytesPerJob int64 = 67108864
 var crunchLogThrottleBytes int64 = 65536