Example #1
0
func (grok *Grok) Match(str string) (map[string]string, error) {

	var gm C.grok_match_t
	//log.Print("Attemping to match '", str, "'")

	grok.lock.Lock()
	ret := C.grok_exec(grok.grok, C.CString(str), &gm)
	grok.lock.Unlock()
	if ret != C.GROK_OK {
		return nil, GrokError{ret}
	}

	var pdata, pname *C.char
	var pname_len, pdata_len C.int

	result := make(map[string]string)

	grok.lock.Lock()
	C.grok_match_walk_init(&gm)
	for C.grok_match_walk_next(&gm, &pname, &pname_len, &pdata, &pdata_len) == 0 {
		name := C.GoStringN(pname, pname_len)
		data := C.GoStringN(pdata, pdata_len)

		result[name] = data
	}
	C.grok_match_walk_end(&gm)
	grok.lock.Unlock()
	return result, nil

	//grok_match_t
}
Example #2
0
func (match *Match) Captures() map[string][]string {
	captures := make(map[string][]string)

	var name, substring *C.char
	var namelen, sublen C.int

	C.grok_match_walk_init(&match.gm)

	for C.grok_match_walk_next(&match.gm, &name, &namelen, &substring, &sublen) == GROK_OK {
		var substrings []string

		gname := C.GoStringN(name, namelen)
		gsubstring := C.GoStringN(substring, sublen)

		if val := captures[gname]; val == nil {
			substrings = make([]string, 0)
		} else {
			substrings = val
		}

		captures[gname] = append(substrings, gsubstring)
	}
	C.grok_match_walk_end(&match.gm)

	return captures
}