Commit 38335b90 authored by Geoff Simmons's avatar Geoff Simmons

Tweaks in the data types to improve inlining and garbage collection.

parent bb33548c
......@@ -47,8 +47,6 @@ nonprintable(enum VSL_tag_e tag)
*/
import "C"
import "fmt"
// TxType is a classifier for a log transaction, indicating if it is
// the log of a client or backend request/response, a Varnish session,
// or a raw transaction.
......@@ -89,7 +87,7 @@ func (txtype TxType) String() string {
case TxRaw:
return "Record"
default:
panic("invalid transaction type")
return "invalid transaction type"
}
}
......@@ -154,7 +152,7 @@ func (reason Reason) String() string {
case Pipe:
return "pipe"
default:
panic("invalid reason")
return "invalid reason"
}
}
......@@ -225,54 +223,77 @@ const (
// String returns "b", "c" or "-".
func (rt RecordType) String() string {
return string(rt)
switch rt {
case Client:
return "c"
case Backend:
return "b"
case None:
return "-"
default:
return "invalid RecordType"
}
}
func initTags() []string {
var tags []string
// TagData contains metadata about log tags, particularly their string
// form, since a tag appears as a number in a Record. The type of the
// Tags variable is TagData[]; for a tag t, use TagData[t] to get its
// metadata.
//
// String is 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 Tags[t].Legal is false, then there is no legal tag for t. No
// such tag is generated from log reads for a live instance of
// Varnish. You may encounter such a tag when reading from a binary
// log that was generated by a version of Varnish that is different
// from the version with which the Go client is currently linked.
//
// NonPrintable is true if log payloads of the type indicated by the
// tag may contain non-printable characters. If NonPrintable is false
// (which is the case for most tags), you can assume that the log
// payload contains only printable ASCII characters.
type TagData struct {
String string
Legal bool
NonPrintable bool
}
func initTags() []TagData {
var tags []TagData
if uint8(C.slt_max()) > ^uint8(0) {
panic("SLT__MAX > max uint8")
}
max := int(C.slt_max())
for i := 0; i < max; i++ {
if C.VSL_tags[i] == nil {
tags = append(tags, fmt.Sprintf("%d", i))
continue
tagdata := TagData{}
if C.VSL_tags[i] != nil {
tagdata.String = C.GoString(C.VSL_tags[i])
tagdata.Legal = true
tagdata.NonPrintable = C.nonprintable(uint32(i)) != 0
}
tags = append(tags, C.GoString(C.VSL_tags[i]))
tags = append(tags, tagdata)
}
return tags
}
var tags = initTags()
// Tags has the type []TagData -- for a tag t in a Record, Tags[t]
// contains its metadata.
var Tags = initTags()
// 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
// output. For a Tag t, Tags[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() returns the numeric value of t as a string, then
// there is no legal tag for t. No such tag is generated from log
// reads for a live instance of Varnish. You may encounter such a tag
// when reading from a binary log that was generated by a version of
// Varnish that is different from the version with which the Go client
// is currently linked.
// String returns the string form for a tag (the value in
// Tags[tag].String). This makes it easy to get the string form with
// the %s or %v verbs for a formatter in package fmt.
func (tag Tag) String() string {
return tags[tag]
}
// Nonprintable returns true if log payloads of the type indicated by
// the tag may contain non-printable characters. If Nonprintable()
// returns false (which is the case for most tags), you can assume
// that the log payload contains only printable ASCII characters.
func (tag Tag) Nonprintable() bool {
return C.nonprintable(uint32(tag)) != 0
return Tags[tag].String
}
// Payload is the type of the message contained in a Record -- the
......@@ -413,7 +434,7 @@ func (status Status) String() string {
case More:
return "More log transactions are pending"
default:
panic("invalid status")
return "invalid status"
}
}
......
......@@ -30,6 +30,20 @@ package log
import "testing"
func TestTags(t *testing.T) {
for i, tag := range Tags {
if Tag(i).String() != tag.String {
t.Errorf("Tag %d string mismatch .String()=%v "+
"Tags[%d].String=%v", i, Tag(i).String(),
i, Tags[i].String)
}
if !tag.Legal && tag.String != "" {
t.Errorf("Non-empty String for illegal Tag %d: %s", i,
tag.String)
}
}
}
var expTxTypeStr = map[TxType]string{
TxUnknown: "Unknown",
Sess: "Session",
......
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