Skip to content

Commit bee733c

Browse files
committed
fix command log
1 parent 3233a9b commit bee733c

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

engine.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func (e *engine) Shutdown(ctx context.Context) error {
179179
}
180180

181181
func (e *engine) doCmd(execSessID int64, cmd0 Command) error {
182-
logDo(execSessID, cmd0, e.l)
182+
logDo(execSessID, cmd0, false, e.l)
183183

184184
switch cmd := cmd0.(type) {
185185
case *TransitCommand:
@@ -273,19 +273,19 @@ func (e *engine) doCmd(execSessID int64, cmd0 Command) error {
273273
return fmt.Errorf("no commands to commit")
274274
}
275275

276-
for _, c := range cmd.Commands {
277-
logDo(execSessID, cmd0, e.l)
276+
for _, subCmd := range cmd.Commands {
277+
logDo(execSessID, subCmd, true, e.l)
278278

279-
if _, ok := c.(*CommitCommand); ok {
279+
if _, ok := subCmd.(*CommitCommand); ok {
280280
return fmt.Errorf("commit command not allowed inside another commit")
281281
}
282-
if _, ok := c.(*ExecuteCommand); ok {
282+
if _, ok := subCmd.(*ExecuteCommand); ok {
283283
return fmt.Errorf("execute command not allowed inside commit")
284284
}
285-
if _, ok := c.(*GetDelayedStatesCommand); ok {
285+
if _, ok := subCmd.(*GetDelayedStatesCommand); ok {
286286
return fmt.Errorf("get delayed states command not allowed inside commit")
287287
}
288-
if _, ok := c.(*GetStatesCommand); ok {
288+
if _, ok := subCmd.(*GetStatesCommand); ok {
289289
return fmt.Errorf("get states command not allowed inside commit")
290290
}
291291
}

log.go

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func logExecute(stateCtx *StateCtx, l *slog.Logger) {
2929
l.Info("engine: execute", args...)
3030
}
3131

32-
func logDo(execSessID int64, cmd0 Command, l *slog.Logger) {
32+
func logDo(execSessID int64, cmd0 Command, subCmd bool, l *slog.Logger) {
3333
var args []any
3434

3535
if execSessID > 0 {
@@ -38,22 +38,25 @@ func logDo(execSessID int64, cmd0 Command, l *slog.Logger) {
3838
args = []any{"sess", cmd0.SessID()}
3939
}
4040

41+
cmdArg := "cmd"
42+
if subCmd {
43+
cmdArg = "sub_cmd"
44+
}
45+
4146
switch cmd := cmd0.(type) {
4247
case *CommitCommand:
43-
args = append(args, "cmd", "commit", "len", len(cmd.Commands))
48+
args = append(args, cmdArg, "commit", "len", len(cmd.Commands))
4449
case *CommitStateCtxCommand:
45-
args = append(args, "cmd", "commit_state_ctx", "id", cmd.StateCtx.Current.ID, "rev", cmd.StateCtx.Current.Rev)
50+
args = append(args, cmdArg, "commit_state_ctx", "id", cmd.StateCtx.Current.ID, "rev", cmd.StateCtx.Current.Rev)
4651
case *TransitCommand:
47-
args = append(args,
48-
"cmd", "transit",
49-
"id", cmd.StateCtx.Current.ID,
50-
"rev", cmd.StateCtx.Current.Rev,
51-
"to", cmd.To,
52-
"labels", cmd.StateCtx.Current.Labels,
53-
)
52+
args = append(args, cmdArg, "transit", "id", cmd.StateCtx.Current.ID, "rev", cmd.StateCtx.Current.Rev, "to", cmd.To)
53+
54+
if len(cmd.StateCtx.Current.Labels) > 0 {
55+
args = append(args, "labels", cmd.StateCtx.Current.Labels)
56+
}
5457
case *PauseCommand:
5558
args = append(args,
56-
"cmd", "pause",
59+
cmdArg, "pause",
5760
"id", cmd.StateCtx.Current.ID,
5861
"rev", cmd.StateCtx.Current.Rev,
5962
)
@@ -63,43 +66,53 @@ func logDo(execSessID int64, cmd0 Command, l *slog.Logger) {
6366
args = append(args, "labels", cmd.StateCtx.Current.Labels)
6467
case *ResumeCommand:
6568
args = append(args,
66-
"cmd", "resume",
69+
cmdArg, "resume",
6770
"id", cmd.StateCtx.Current.ID,
6871
"rev", cmd.StateCtx.Current.Rev,
69-
"labels", cmd.StateCtx.Current.Labels,
7072
)
73+
if len(cmd.StateCtx.Current.Labels) > 0 {
74+
args = append(args, "labels", cmd.StateCtx.Current.Labels)
75+
}
7176
case *EndCommand:
7277
args = append(args,
73-
"cmd", "end",
78+
cmdArg, "end",
7479
"id", cmd.StateCtx.Current.ID,
7580
"rev", cmd.StateCtx.Current.Rev,
76-
"labels", cmd.StateCtx.Current.Labels,
7781
)
82+
if len(cmd.StateCtx.Current.Labels) > 0 {
83+
args = append(args, "labels", cmd.StateCtx.Current.Labels)
84+
}
7885
case *DelayCommand:
7986
args = append(args,
80-
"cmd", "delay",
87+
cmdArg, "delay",
8188
"id", cmd.StateCtx.Current.ID,
8289
"rev", cmd.StateCtx.Current.Rev,
8390
"execute_at", cmd.ExecuteAt,
84-
"labels", cmd.StateCtx.Current.Labels,
8591
)
92+
if len(cmd.StateCtx.Current.Labels) > 0 {
93+
args = append(args, "labels", cmd.StateCtx.Current.Labels)
94+
}
8695
case *ExecuteCommand:
8796
args = append(args,
88-
"cmd", "execute",
97+
cmdArg, "execute",
8998
"id", cmd.StateCtx.Current.ID,
9099
"rev", cmd.StateCtx.Current.Rev,
91-
"labels", cmd.StateCtx.Current.Labels,
92100
)
101+
if len(cmd.StateCtx.Current.Labels) > 0 {
102+
args = append(args, "labels", cmd.StateCtx.Current.Labels)
103+
}
93104
case *NoopCommand:
94105
args = append(args,
95-
"cmd", "noop",
106+
cmdArg, "noop",
96107
"id", cmd.StateCtx.Current.ID,
97108
"rev", cmd.StateCtx.Current.Rev,
98-
"labels", cmd.StateCtx.Current.Labels,
99109
)
110+
if len(cmd.StateCtx.Current.Labels) > 0 {
111+
args = append(args, "labels", cmd.StateCtx.Current.Labels)
112+
}
100113
case *AttachDataCommand:
101114
args = append(args,
102-
"cmd", "attach_data",
115+
cmdArg, "attach_data",
103116
"id", cmd.StateCtx.Current.ID,
104117
"rev", cmd.StateCtx.Current.Rev,
105118
"data_id", cmd.Data.ID,
@@ -108,28 +121,28 @@ func logDo(execSessID int64, cmd0 Command, l *slog.Logger) {
108121
)
109122
case *GetDataCommand:
110123
args = append(args,
111-
"cmd", "get_data",
124+
cmdArg, "get_data",
112125
"id", cmd.StateCtx.Current.ID,
113126
"rev", cmd.StateCtx.Current.Rev,
114127
"data_id", cmd.Data.ID,
115128
"data_rev", cmd.Data.Rev,
116129
"alias", cmd.Alias,
117130
)
118131
case *UnstackCommand:
119-
args = append(args, "cmd", "unstack")
132+
args = append(args, cmdArg, "unstack")
120133
case *StackCommand:
121-
args = append(args, "cmd", "stack")
134+
args = append(args, cmdArg, "stack")
122135
case *GetStateByIDCommand:
123-
args = append(args, "cmd", "get_state_by_id",
136+
args = append(args, cmdArg, "get_state_by_id",
124137
"id", cmd.ID,
125138
"rev", cmd.Rev,
126139
)
127140
case *GetStateByLabelsCommand:
128-
args = append(args, "cmd", "get_state_by_labels",
141+
args = append(args, cmdArg, "get_state_by_labels",
129142
"labels", cmd.Labels,
130143
)
131144
case *GetStatesCommand:
132-
args = append(args, "cmd", "get_states")
145+
args = append(args, cmdArg, "get_states")
133146
if cmd.SinceRev > 0 {
134147
args = append(args, "since_rev", cmd.SinceRev)
135148
}
@@ -146,13 +159,13 @@ func logDo(execSessID int64, cmd0 Command, l *slog.Logger) {
146159
args = append(args, "limit", cmd.Limit)
147160
}
148161
case *GetDelayedStatesCommand:
149-
args = append(args, "cmd", "get_delayed_states",
162+
args = append(args, cmdArg, "get_delayed_states",
150163
"since", cmd.Since,
151164
"until", cmd.Until,
152165
"offset", cmd.Offset,
153166
)
154167
default:
155-
args = append(args, "cmd", fmt.Sprintf("%T", cmd))
168+
args = append(args, cmdArg, fmt.Sprintf("%T", cmd))
156169
}
157170

158171
l.Info("engine: do", args...)

0 commit comments

Comments
 (0)