Skip to content

Commit 0bb56a8

Browse files
committed
watch: Fix duplicate logs and remove timestamps
Instead we log the duration each layer's full compilation took.
1 parent 92ee929 commit 0bb56a8

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

ci/release/changelogs/next.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#### Features 🚀
22

33
- Many non-Latin languages (e.g. Chinese, Japanese, Korean) are usable now that multi-byte characters are measured correctly. [#817](https://github.com/terrastruct/d2/pull/817)
4+
- Fix duplicate success logs in watch mode. [830](https://github.com/terrastruct/d2/pull/830)
45

56
#### Improvements 🧹
67

8+
- Cleaner watch mode logs without timestamps. [830](https://github.com/terrastruct/d2/pull/830)
9+
710
#### Bugfixes ⛑️
811

912
- Fixes edge case where layouts with dagre show a connection from the bottom side of shapes being slightly disconnected from the shape. [#820](https://github.com/terrastruct/d2/pull/820)

main.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
198198
if inputPath == "-" {
199199
return xmain.UsageErrorf("-w[atch] cannot be combined with reading input from stdin")
200200
}
201-
ms.Log.SetTS(true)
202201
w, err := newWatcher(ctx, ms, watcherOpts{
203202
layoutPlugin: plugin,
204203
sketch: *sketchFlag,
@@ -232,6 +231,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) {
232231
}
233232

234233
func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad, themeID int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page) (_ []byte, written bool, _ error) {
234+
start := time.Now()
235235
input, err := ms.ReadPath(inputPath)
236236
if err != nil {
237237
return nil, false, err
@@ -256,38 +256,41 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketc
256256
return nil, false, err
257257
}
258258

259-
svg, err := render(ctx, ms, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, diagram)
259+
compileDir := time.Since(start)
260+
svg, err := render(ctx, ms, compileDir, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, diagram)
260261
if err != nil {
261262
return svg, false, err
262263
}
263264
return svg, true, nil
264265
}
265266

266-
func render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, sketch bool, pad int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
267+
func render(ctx context.Context, ms *xmain.State, compileDur time.Duration, plugin d2plugin.Plugin, sketch bool, pad int64, inputPath, outputPath string, bundle, forceAppendix bool, page playwright.Page, ruler *textmeasure.Ruler, diagram *d2target.Diagram) ([]byte, error) {
267268
outputPath = layerOutputPath(outputPath, diagram)
268269
for _, dl := range diagram.Layers {
269-
_, err := render(ctx, ms, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
270+
_, err := render(ctx, ms, compileDur, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
270271
if err != nil {
271272
return nil, err
272273
}
273274
}
274275
for _, dl := range diagram.Scenarios {
275-
_, err := render(ctx, ms, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
276+
_, err := render(ctx, ms, compileDur, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
276277
if err != nil {
277278
return nil, err
278279
}
279280
}
280281
for _, dl := range diagram.Steps {
281-
_, err := render(ctx, ms, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
282+
_, err := render(ctx, ms, compileDur, plugin, sketch, pad, inputPath, outputPath, bundle, forceAppendix, page, ruler, dl)
282283
if err != nil {
283284
return nil, err
284285
}
285286
}
287+
start := time.Now()
286288
svg, err := _render(ctx, ms, plugin, sketch, pad, outputPath, bundle, forceAppendix, page, ruler, diagram)
287289
if err != nil {
288290
return svg, err
289291
}
290-
ms.Log.Success.Printf("successfully compiled %v to %v", inputPath, outputPath)
292+
dur := compileDur + time.Since(start)
293+
ms.Log.Success.Printf("successfully compiled %s to %s in %s", inputPath, outputPath, dur)
291294
return svg, nil
292295
}
293296

watch.go

-2
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,6 @@ func (w *watcher) compileLoop(ctx context.Context) error {
367367
}
368368
errs = err.Error()
369369
w.ms.Log.Error.Print(errs)
370-
} else {
371-
w.ms.Log.Success.Printf("successfully %scompiled %v to %v", recompiledPrefix, w.inputPath, w.outputPath)
372370
}
373371
w.broadcast(&compileResult{
374372
SVG: string(svg),

0 commit comments

Comments
 (0)