Skip to content

Commit 01d3b46

Browse files
committed
logcalls: configurable selector names/prefixes
1 parent 5ab2c5c commit 01d3b46

15 files changed

Lines changed: 604 additions & 45 deletions

ARCHITECTURE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,11 @@ Goal:
169169
- Pack arguments greedily within the column limit.
170170
- Split long format strings safely with a configurable minimum “tail” length
171171
to avoid ugly 1–2 character remnants.
172+
- Selection can be restricted to an allowlist of selector receiver prefixes
173+
via `--logcalls-selector-prefixes` (useful when a repo has many `Infof`-like
174+
methods that should not be rewritten).
175+
- The set of recognized `*f` selector names can be overridden via
176+
`--logcalls-selector-names`.
172177

173178
Implementation:
174179

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,17 @@ Helpful flags:
9191
call formatting
9292
- `--logcalls-min-tail-len N`: avoid leaving tiny tails when splitting long
9393
format strings (0 means default)
94+
- `--logcalls-selector-names n1,n2`: override the set of `*f` selector names to
95+
treat as printf/log calls for compact formatting (default includes `Infof`,
96+
`Errorf`, `Sprintf`, etc.)
97+
- `--logcalls-selector-prefixes p1,p2`: restrict compact log/printf call
98+
formatting to specific selector receiver expression prefixes (e.g.
99+
`rpcSLog,zap.L().Sugar()`)
94100
- `--fixpoint-iters N`: run the full pipeline repeatedly until stable (default
95101
is `3` in the CLI)
96102
- `--print-plan`: print resolved stage plan and exit
103+
- `--print-logcalls-patterns`: print the currently recognized log/printf call
104+
patterns and exit
97105

98106
## Tests
99107

cmd/llformat/main.go

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"sort"
99
"strings"
1010

11+
"github.com/lightninglabs/llformat/dsl"
1112
"github.com/lightninglabs/llformat/formatter"
1213
)
1314

@@ -22,7 +23,10 @@ type cliFlags struct {
2223
moveInline bool
2324
multilineExclude string
2425
logCallsMinTailLen int
26+
logCallsNames string
27+
logCallsPrefixes string
2528
printPlan bool
29+
printLogCalls bool
2630
fixpointIters int
2731
traceDSL bool
2832
traceDSLReasons bool
@@ -64,10 +68,26 @@ func run(args []string, stdout, stderr io.Writer) int {
6468
"tail length when splitting printf/logcall strings "+
6569
"in next profile (0 => default)",
6670
)
71+
fs.StringVar(
72+
&f.logCallsNames, "logcalls-selector-names", "", "comma-separ"+
73+
"ated list of selector/ident names to treat as "+
74+
"printf-style calls for suffix-only matching (empty "+
75+
"=> built-in default)",
76+
)
77+
fs.StringVar(
78+
&f.logCallsPrefixes, "logcalls-selector-prefixes", "", "comma"+
79+
"-separated list of selector receiver expression "+
80+
"prefixes to target for log/printf call formatting "+
81+
"(empty => match any selector prefix in next profile)",
82+
)
6783
fs.BoolVar(
6884
&f.printPlan, "print-plan", false,
6985
"print resolved pipeline plan and exit",
7086
)
87+
fs.BoolVar(
88+
&f.printLogCalls, "print-logcalls-patterns", false,
89+
"print log/printf call matching patterns and exit",
90+
)
7191
fs.BoolVar(
7292
&f.traceDSL, "trace-dsl", false,
7393
"trace applied DSL edits to stderr",
@@ -98,6 +118,9 @@ func run(args []string, stdout, stderr io.Writer) int {
98118
if f.printPlan {
99119
return runPrintPlan(stdout, cfg)
100120
}
121+
if f.printLogCalls {
122+
return runPrintLogCallsPatterns(stdout, cfg)
123+
}
101124

102125
if fs.NArg() != 1 {
103126
fs.Usage()
@@ -124,11 +147,18 @@ func run(args []string, stdout, stderr io.Writer) int {
124147

125148
func printUsage(w io.Writer) {
126149
fmt.Fprintln(
127-
w, "usage: llformat [-w] [--wrap-inline-comments] [--col N] "+
150+
w, "usage:",
151+
)
152+
fmt.Fprintln(
153+
w, " llformat [-w] [--wrap-inline-comments] [--col N] "+
128154
"[--tab N] [--multiline-exclude FUNCS] "+
129-
"[--logcalls-min-tail-len N] [--fixpoint-iters N] "+
130-
"[--print-plan] <path>",
155+
"[--logcalls-min-tail-len N] "+
156+
"[--logcalls-selector-names NAMES] "+
157+
"[--logcalls-selector-prefixes PREFIXES] "+
158+
"[--fixpoint-iters N] <path>",
131159
)
160+
fmt.Fprintln(w, " llformat --print-plan")
161+
fmt.Fprintln(w, " llformat --print-logcalls-patterns")
132162
fmt.Fprintln(w)
133163
fmt.Fprintln(w, "flags:")
134164
fmt.Fprintln(
@@ -155,6 +185,16 @@ func printUsage(w io.Writer) {
155185
w, " --logcalls-min-tail-len N minimum tail length when "+
156186
"splitting printf/logcall strings (0 => default)",
157187
)
188+
fmt.Fprintln(
189+
w, " --logcalls-selector-names NAMES comma-separated "+
190+
"selector/ident names for printf-style matching "+
191+
"(example: \"Infof,Errorf\")",
192+
)
193+
fmt.Fprintln(
194+
w, " --logcalls-selector-prefixes PREFIXES comma-separated "+
195+
"receiver expression prefixes to target (example: "+
196+
"\"rpcSLog,zap.L().Sugar()\")",
197+
)
158198
fmt.Fprintln(
159199
w, " --fixpoint-iters N repeat full pipeline until "+
160200
"stable (0=auto; default 3)",
@@ -163,6 +203,10 @@ func printUsage(w io.Writer) {
163203
w, " --print-plan print resolved pipeline "+
164204
"plan and exit",
165205
)
206+
fmt.Fprintln(
207+
w, " --print-logcalls-patterns print log/printf matching "+
208+
"patterns and exit",
209+
)
166210
fmt.Fprintln(
167211
w, " --trace-dsl trace applied DSL edits to "+
168212
"stderr",
@@ -175,6 +219,8 @@ func printUsage(w io.Writer) {
175219

176220
func buildPipelineConfig(f cliFlags) (formatter.PipelineConfig, error) {
177221
excludes := parseCommaList(f.multilineExclude)
222+
logCallsNames := parseCommaList(f.logCallsNames)
223+
logCallsPrefixes := parseCommaList(f.logCallsPrefixes)
178224

179225
fixpointIters := f.fixpointIters
180226
if fixpointIters < 0 {
@@ -187,14 +233,16 @@ func buildPipelineConfig(f cliFlags) (formatter.PipelineConfig, error) {
187233
}
188234

189235
cfg := formatter.PipelineConfig{
190-
ColumnLimit: f.colLimit,
191-
TabStop: f.tabStop,
192-
MoveInlineAbove: f.moveInline,
193-
Excludes: excludes,
194-
LogCallsMinTailLen: f.logCallsMinTailLen,
195-
MaxPipelineIterations: fixpointIters,
196-
TraceDSL: f.traceDSL,
197-
TraceDSLReasons: f.traceDSLReasons,
236+
ColumnLimit: f.colLimit,
237+
TabStop: f.tabStop,
238+
MoveInlineAbove: f.moveInline,
239+
Excludes: excludes,
240+
LogCallsMinTailLen: f.logCallsMinTailLen,
241+
LogCallsSelectorNames: logCallsNames,
242+
LogCallsSelectorPrefixes: logCallsPrefixes,
243+
MaxPipelineIterations: fixpointIters,
244+
TraceDSL: f.traceDSL,
245+
TraceDSLReasons: f.traceDSLReasons,
198246
}
199247

200248
if err := formatter.ValidatePipelineConfig(cfg); err != nil {
@@ -204,6 +252,47 @@ func buildPipelineConfig(f cliFlags) (formatter.PipelineConfig, error) {
204252
return cfg, nil
205253
}
206254

255+
func runPrintLogCallsPatterns(w io.Writer, cfg formatter.PipelineConfig) int {
256+
257+
fmt.Fprintln(w, "log/printf call matching patterns:")
258+
fmt.Fprintln(w)
259+
260+
selectorNames := cfg.LogCallsSelectorNames
261+
if len(selectorNames) == 0 {
262+
selectorNames = dsl.LogPrintfSelectorNames()
263+
}
264+
fmt.Fprintf(
265+
w, "- printf-style selector names (effective): %s\n", strings.Join(
266+
selectorNames, ", ",
267+
),
268+
)
269+
fmt.Fprintf(
270+
w, "- canonical exact patterns: %s\n",
271+
strings.Join(
272+
dsl.LogPrintfCanonicalPatterns(), ", ",
273+
),
274+
)
275+
fmt.Fprintf(
276+
w, "- non-f string-call names (next): %s\n",
277+
strings.Join(
278+
dsl.NonFStringLogNames(), ", ",
279+
),
280+
)
281+
if len(cfg.LogCallsSelectorPrefixes) == 0 {
282+
fmt.Fprintln(
283+
w, "- selector prefix allowlist: (none; next "+
284+
"matches any prefix)",
285+
)
286+
} else {
287+
fmt.Fprintf(
288+
w, "- selector prefix allowlist: %s\n",
289+
strings.Join(cfg.LogCallsSelectorPrefixes, ", "),
290+
)
291+
}
292+
293+
return 0
294+
}
295+
207296
func parseCommaList(s string) []string {
208297
if strings.TrimSpace(s) == "" {
209298
return nil

0 commit comments

Comments
 (0)