logging_generate.go

 1//go:build clientlogmode
 2// +build clientlogmode
 3
 4package main
 5
 6import (
 7	"fmt"
 8	"log"
 9	"os"
10	"strings"
11	"text/template"
12)
13
14var config = struct {
15	ModeBits []string
16}{
17	// Items should be appended only to keep bit-flag positions stable
18	ModeBits: []string{
19		"Signing",
20		"Retries",
21		"Request",
22		"RequestWithBody",
23		"Response",
24		"ResponseWithBody",
25		"DeprecatedUsage",
26		"RequestEventMessage",
27		"ResponseEventMessage",
28	},
29}
30
31func bitName(name string) string {
32	return strings.ToUpper(name[:1]) + name[1:]
33}
34
35var tmpl = template.Must(template.New("ClientLogMode").Funcs(map[string]interface{}{
36	"symbolName": func(name string) string {
37		return "Log" + bitName(name)
38	},
39	"bitName": bitName,
40}).Parse(`// Code generated by aws/logging_generate.go DO NOT EDIT.
41
42package aws
43
44// ClientLogMode represents the logging mode of SDK clients. The client logging mode is a bit-field where
45// each bit is a flag that describes the logging behavior for one or more client components.
46// The entire 64-bit group is reserved for later expansion by the SDK.
47//
48// Example: Setting ClientLogMode to enable logging of retries and requests
49//  clientLogMode := aws.LogRetries | aws.LogRequest
50//
51// Example: Adding an additional log mode to an existing ClientLogMode value
52//  clientLogMode |= aws.LogResponse
53type ClientLogMode uint64
54
55// Supported ClientLogMode bits that can be configured to toggle logging of specific SDK events.
56const (
57{{- range $index, $field := .ModeBits }}
58	{{ (symbolName $field) }}{{- if (eq 0 $index) }} ClientLogMode = 1 << (64 - 1 - iota){{- end }}
59{{- end }}
60)
61{{ range $_, $field := .ModeBits }}
62// Is{{- bitName $field }} returns whether the {{ bitName $field }} logging mode bit is set
63func (m ClientLogMode) Is{{- bitName $field }}() bool {
64	return m&{{- (symbolName $field) }} != 0
65}
66{{ end }}
67{{- range $_, $field := .ModeBits }}
68// Clear{{- bitName $field }} clears the {{ bitName $field }} logging mode bit
69func (m *ClientLogMode) Clear{{- bitName $field }}() {
70	*m &^= {{ (symbolName $field) }}
71}
72{{ end -}}
73`))
74
75func main() {
76	uniqueBitFields := make(map[string]struct{})
77
78	for _, bitName := range config.ModeBits {
79		if _, ok := uniqueBitFields[strings.ToLower(bitName)]; ok {
80			panic(fmt.Sprintf("duplicate bit field: %s", bitName))
81		}
82		uniqueBitFields[bitName] = struct{}{}
83	}
84
85	file, err := os.Create("logging.go")
86	if err != nil {
87		log.Fatal(err)
88	}
89	defer file.Close()
90
91	err = tmpl.Execute(file, config)
92	if err != nil {
93		log.Fatal(err)
94	}
95}