Commit 663ffc5c authored by Geoff Simmons's avatar Geoff Simmons

Encapsulate the Tag type, and have it implement the Stringer interface.

parent 90c90809
......@@ -171,7 +171,7 @@ const (
// above.
type RecordType uint8
func tags() []string {
func initTags() []string {
var tags []string
if uint8(C.slt_max()) > ^uint8(0) {
panic("SLT__MAX > max uint8")
......@@ -188,18 +188,28 @@ func tags() []string {
}
var (
// Tags is an array of strings indexed by the Tag field of a
// Record, classifying the Record's contents, as in the second
// column of default varnishlog output. Examples are "ReqURL"
// for a client request URL and "BerespStatus" for the HTTP
// status of a backend response.
//
// If Tags[i] == "", then no tag exists for index i.
Tags = tags()
tags = initTags()
nonprint = regexp.MustCompile("[^[:print:]]")
cbMap = make(map[int]ReadCB)
)
// A Tag is a classifier for the contents of a Record's payload,
// corresponding to the second column of default varnishlog
// output. For a Tag t, t.String() may be "ReqURL" for a client
// request URL, "BerespStatus" for the HTTP status of a backend
// response, and so forth.
type Tag uint8
// String returns the Tag name that appears in the second column of
// default varnishlog output, such as "ReqHeader" for a client request
// header, or "BerespHeader" for a backend response header.
//
// If t.String() == "", then no tag exists for Tag t. No such tag is
// generated from log reads.
func (tag Tag) String() string {
return tags[tag]
}
// A Record in the Varnish log, corresponding to a line of output from
// the varnishlog utility. Instances of this type are contained in the
// Tx objects returned by Read() operations, and form the bulk of log
......@@ -213,12 +223,11 @@ type Record struct {
// backend log, or neither.
Type RecordType
// An index into the Tags array, classifying the contents of
// this record, as in the second column of default varnishlog
// output. For a Tag t, Tags[t] may be a string such as
// "ReqURL" for a client request URL, "BerespStatus" for the
// HTTP status of a backend response, and so forth.
Tag uint8
// A classifier for the contents of this Record's payload. Use
// Tag.String() to get tag names such as "ReqURL" or
// "BerespHeader" that appear in the second column of default
// varnishlog output.
Tag Tag
// The unique ID generated by Varnish for this log
// transaction.
......@@ -383,14 +392,14 @@ func publish(trans unsafe.Pointer, priv unsafe.Pointer) C.int {
break
}
tag := C.tag(vtx.c)
if tag < 0 || int(tag) >= len(Tags) {
if tag < 0 || int(tag) >= len(tags) {
panic("tag index out of range")
}
if Tags[tag] == "" {
if tags[tag] == "" {
panic("unknown tag")
}
rec := new(Record)
rec.Tag = uint8(tag)
rec.Tag = Tag(tag)
rec.Payload = C.GoStringN(C.cdata(vtx.c), C.len(vtx.c))
if C.unsafe(tag) != 0 || C.binary(tag) != 0 {
nonprint.ReplaceAllStringFunc(rec.Payload,
......
......@@ -221,8 +221,8 @@ func TestDefaultRead(t *testing.T) {
t.Errorf("rec vxid expected=%v got=%v",
expRec.vxid, rec.VXID)
}
if Tags[rec.Tag] != expRec.tag {
t.Errorf("rec tag expected=%v got=%v",
if rec.Tag.String() != expRec.tag {
t.Errorf("rec tag expected=%v got=%s",
expRec.tag, rec.Tag)
}
if rune(rec.Type) != expRec.rectype {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment