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