diff pkg/imports/sr.go @ 2963:27ffd94afcb5

SR import: Limit number of warnings in parsing XYZ files to 100.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 08 Apr 2019 16:51:50 +0200
parents b1707f60f241
children 4acbee65275d
line wrap: on
line diff
--- a/pkg/imports/sr.go	Mon Apr 08 16:38:57 2019 +0200
+++ b/pkg/imports/sr.go	Mon Apr 08 16:51:50 2019 +0200
@@ -431,18 +431,32 @@
 
 	var hasNegZ bool
 
+	const maxWarnings = 100
+	var warnings int
+
+	warn := func(format string, args ...interface{}) {
+		if warnings++; warnings <= maxWarnings {
+			feedback.Warn(format, args...)
+		}
+	}
+	defer func() {
+		if warnings > maxWarnings {
+			feedback.Warn("Too many warnings. %d ignored.", warnings-maxWarnings)
+		}
+	}()
+
 	for line := 1; s.Scan(); line++ {
 		text := s.Text()
 		var p octree.Vertex
 		// fmt.Sscanf(text, "%f,%f,%f") is 4 times slower.
 		idx := strings.IndexByte(text, ',')
 		if idx == -1 {
-			feedback.Warn("format error in line %d", line)
+			warn("format error in line %d", line)
 			continue
 		}
 		var err error
 		if p.X, err = strconv.ParseFloat(text[:idx], 64); err != nil {
-			feedback.Warn("format error in line %d: %v", line, err)
+			warn("format error in line %d: %v", line, err)
 			continue
 		}
 		text = text[idx+1:]
@@ -451,19 +465,19 @@
 			continue
 		}
 		if p.Y, err = strconv.ParseFloat(text[:idx], 64); err != nil {
-			feedback.Warn("format error in line %d: %v", line, err)
+			warn("format error in line %d: %v", line, err)
 			continue
 		}
 		text = text[idx+1:]
 		if p.Z, err = strconv.ParseFloat(text, 64); err != nil {
-			feedback.Warn("format error in line %d: %v", line, err)
+			warn("format error in line %d: %v", line, err)
 			continue
 		}
 		if p.Z < 0 {
 			p.Z = -p.Z
 			if !hasNegZ {
 				hasNegZ = true
-				feedback.Warn("Negative Z value found: Using -Z")
+				warn("Negative Z value found: Using -Z")
 			}
 		}
 		mpz = append(mpz, p)