func parseKLogMessageFragment()

in pkg/parser/k8s/klog.go [76:157]


func parseKLogMessageFragment(klogMessageFragment string) map[string]string {
	result := map[string]string{}
	inQuotes := false
	inGoBrace := false
	parsingKey := true
	escaping := false
	currentKey := ""
	currentGroup := ""
	// For the log format not starting from the double quote
	// Example:
	// Error foo" fieldWithQuotes="foo" fieldWithEscape="foo \"bar\"" fieldWithoutQuotes=qux1234
	if strings.Count(klogMessageFragment, "\"")%2 == 1 {
		klogMessageFragment = `"` + klogMessageFragment
	}
	for i := 0; i < len(klogMessageFragment); i++ {
		// For log body beginning with `"`, it should be regarded as the msg field.
		if i == 0 && klogMessageFragment[i] == '"' {
			inQuotes = true
			// `msg` is reserved for the main message
			currentKey = "msg"
			parsingKey = false
			continue
		}
		if !escaping {

			if klogMessageFragment[i] == '\\' {
				escaping = true
				continue
			}

			if klogMessageFragment[i] == '{' && !inQuotes {
				inGoBrace = true
				currentGroup += string(klogMessageFragment[i])
				continue
			}

			if klogMessageFragment[i] == '}' && !inQuotes && inGoBrace {
				inGoBrace = false
				currentGroup += string(klogMessageFragment[i])
				continue
			}

			if klogMessageFragment[i] == '"' && !inGoBrace {
				if !parsingKey && inQuotes {
					result[currentKey] = currentGroup
					parsingKey = true
					currentGroup = ""
				}
				inQuotes = !inQuotes
				continue
			}

			if klogMessageFragment[i] == '=' && !inQuotes && !inGoBrace {
				if parsingKey {
					currentKey = currentGroup
					currentGroup = ""
					parsingKey = false
					continue
				}
			}
		}

		if klogMessageFragment[i] == ' ' && !inQuotes && !inGoBrace {
			if !parsingKey {
				result[currentKey] = currentGroup
				parsingKey = true
				currentGroup = ""
			}
			continue
		}

		if escaping {
			escaping = false
		}

		currentGroup += string(klogMessageFragment[i])
	}
	if !parsingKey {
		result[currentKey] = currentGroup
	}
	return result
}