Skip to content

Commit 3fb59b0

Browse files
authored
Merge pull request #2612 from alixander/ascii-remove
d2ascii: remove debug logs
2 parents c717f3a + f2d2896 commit 3fb59b0

16 files changed

Lines changed: 192 additions & 221 deletions

File tree

d2cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
912912
Charset: charsetType,
913913
}
914914
asciiArtist := d2ascii.NewASCIIartist()
915-
ascii, err := asciiArtist.Render(diagram, renderOpts)
915+
ascii, err := asciiArtist.Render(ctx, diagram, renderOpts)
916916
if err != nil {
917917
return ascii, err
918918
}

d2js/d2wasm/functions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ func Render(args []js.Value) (interface{}, error) {
416416
return nil, &WASMError{Message: "ASCII rendering does not support multi-board targets", Code: 400}
417417
}
418418

419+
ctx := log.WithDefault(context.Background())
419420
artist := d2ascii.NewASCIIartist()
420421
asciiOpts := &d2ascii.RenderOpts{}
421422
if input.Opts.Scale != nil {
@@ -435,7 +436,7 @@ func Render(args []js.Value) (interface{}, error) {
435436
}
436437
asciiOpts.Charset = charsetType
437438

438-
out, err := artist.Render(diagram, asciiOpts)
439+
out, err := artist.Render(ctx, diagram, asciiOpts)
439440
if err != nil {
440441
return nil, &WASMError{Message: fmt.Sprintf("ASCII render failed: %s", err.Error()), Code: 500}
441442
}

d2js/js/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

d2js/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@terrastruct/d2",
33
"author": "Terrastruct, Inc.",
44
"description": "D2.js is a wrapper around the WASM build of D2, the modern text-to-diagram language.",
5-
"version": "0.1.32",
5+
"version": "0.1.33",
66
"repository": {
77
"type": "git",
88
"url": "git+https://github.com/terrastruct/d2.git",

d2renderers/d2ascii/asciiroute/asciiroute.go

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package asciiroute
22

33
import (
4-
"fmt"
4+
"context"
5+
"log/slog"
56
"math"
67
"strings"
78

89
"oss.terrastruct.com/d2/d2renderers/d2ascii/asciicanvas"
910
"oss.terrastruct.com/d2/d2renderers/d2ascii/charset"
1011
"oss.terrastruct.com/d2/d2target"
12+
"oss.terrastruct.com/d2/lib/log"
1113
)
1214

1315
const (
@@ -45,52 +47,47 @@ type RouteDrawer interface {
4547
GetScale() float64
4648
GetBoundaryForShape(s d2target.Shape) (Point, Point)
4749
CalibrateXY(x, y float64) (float64, float64)
50+
GetContext() context.Context
4851
}
4952

5053
func DrawRoute(rd RouteDrawer, conn d2target.Connection) {
5154
routes := conn.Route
5255
label := conn.Label
56+
ctx := rd.GetContext()
5357

54-
fmt.Printf("[D2ASCII] Starting edge route for connection %s -> %s\n", conn.Src, conn.Dst)
55-
fmt.Printf("[D2ASCII] Initial route points (%d points):\n", len(routes))
58+
log.Debug(ctx, "starting edge route", slog.String("src", conn.Src), slog.String("dst", conn.Dst))
59+
log.Debug(ctx, "initial route points", slog.Int("count", len(routes)))
5660
for i, pt := range routes {
57-
fmt.Printf("[D2ASCII] Point %d: (%.2f, %.2f)\n", i, pt.X, pt.Y)
61+
log.Debug(ctx, "route point", slog.Int("index", i), slog.Float64("x", pt.X), slog.Float64("y", pt.Y))
5862
}
5963

6064
frmShapeBoundary, toShapeBoundary := getConnectionBoundaries(rd, conn.Src, conn.Dst)
61-
fmt.Printf("[D2ASCII] Source boundary: TL(%d,%d) BR(%d,%d)\n",
62-
frmShapeBoundary.TL.X, frmShapeBoundary.TL.Y,
63-
frmShapeBoundary.BR.X, frmShapeBoundary.BR.Y)
64-
fmt.Printf("[D2ASCII] Dest boundary: TL(%d,%d) BR(%d,%d)\n",
65-
toShapeBoundary.TL.X, toShapeBoundary.TL.Y,
66-
toShapeBoundary.BR.X, toShapeBoundary.BR.Y)
65+
log.Debug(ctx, "boundaries", slog.Int("srcTLX", frmShapeBoundary.TL.X), slog.Int("srcTLY", frmShapeBoundary.TL.Y), slog.Int("srcBRX", frmShapeBoundary.BR.X), slog.Int("srcBRY", frmShapeBoundary.BR.Y), slog.Int("dstTLX", toShapeBoundary.TL.X), slog.Int("dstTLY", toShapeBoundary.TL.Y), slog.Int("dstBRX", toShapeBoundary.BR.X), slog.Int("dstBRY", toShapeBoundary.BR.Y))
6766

68-
routes = processRoute(rd, routes, frmShapeBoundary, toShapeBoundary)
67+
routes = processRoute(ctx, rd, routes, frmShapeBoundary, toShapeBoundary)
6968

7069
turnDir := calculateTurnDirections(routes)
71-
fmt.Printf("[D2ASCII] Turn directions calculated: %d turns\n", len(turnDir))
70+
log.Debug(ctx, "turn directions calculated", slog.Int("count", len(turnDir)))
7271
for key, dir := range turnDir {
73-
fmt.Printf("[D2ASCII] Turn at %s: direction %s\n", key, dir)
72+
log.Debug(ctx, "turn direction", slog.String("key", key), slog.String("dir", dir))
7473
}
7574

7675
var labelPos *RouteLabelPosition
7776
if strings.TrimSpace(label) != "" {
7877
labelPos = calculateBestLabelPosition(rd, routes, label)
7978
if labelPos != nil {
80-
fmt.Printf("[D2ASCII] Label position calculated: segment %d, pos (%d, %d), maxDiff %.2f\n",
81-
labelPos.I, labelPos.X, labelPos.Y, labelPos.MaxDiff)
79+
log.Debug(ctx, "label position calculated", slog.Int("segmentIndex", labelPos.I), slog.Int("x", labelPos.X), slog.Int("y", labelPos.Y), slog.Float64("maxDiff", labelPos.MaxDiff))
8280
}
8381
}
8482

8583
corners, arrows := getCharacterMaps(rd)
8684

87-
fmt.Printf("[D2ASCII] Drawing %d segments\n", len(routes)-1)
85+
log.Debug(ctx, "drawing segments", slog.Int("count", len(routes)-1))
8886
for i := 1; i < len(routes); i++ {
89-
fmt.Printf("[D2ASCII] Drawing segment %d: (%.2f,%.2f) -> (%.2f,%.2f)\n",
90-
i-1, routes[i-1].X, routes[i-1].Y, routes[i].X, routes[i].Y)
91-
drawSegmentBetweenPoints(rd, routes[i-1], routes[i], i, conn, corners, arrows, turnDir, frmShapeBoundary, toShapeBoundary, labelPos, label)
87+
log.Debug(ctx, "drawing segment", slog.Int("index", i-1), slog.Float64("x1", routes[i-1].X), slog.Float64("y1", routes[i-1].Y), slog.Float64("x2", routes[i].X), slog.Float64("y2", routes[i].Y))
88+
drawSegmentBetweenPoints(ctx, rd, routes[i-1], routes[i], i, conn, corners, arrows, turnDir, frmShapeBoundary, toShapeBoundary, labelPos, label)
9289
}
93-
fmt.Printf("[D2ASCII] Edge route completed for %s -> %s\n", conn.Src, conn.Dst)
90+
log.Debug(ctx, "edge route completed", slog.String("src", conn.Src), slog.String("dst", conn.Dst))
9491
}
9592

9693
func getCharacterMaps(rd RouteDrawer) (corners, arrows map[string]string) {

d2renderers/d2ascii/asciiroute/drawing.go

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
11
package asciiroute
22

33
import (
4+
"context"
45
"fmt"
6+
"log/slog"
57
"math"
68

79
"oss.terrastruct.com/d2/d2target"
810
"oss.terrastruct.com/d2/lib/geo"
11+
"oss.terrastruct.com/d2/lib/log"
912
)
1013

11-
func drawSegmentBetweenPoints(rd RouteDrawer, start, end *geo.Point, segmentIndex int, conn d2target.Connection,
14+
func drawSegmentBetweenPoints(ctx context.Context, rd RouteDrawer, start, end *geo.Point, segmentIndex int, conn d2target.Connection,
1215
corners, arrows, turnDir map[string]string, frmBoundary, toBoundary Boundary, labelPos *RouteLabelPosition, label string) {
1316

1417
ax, ay := start.X, start.Y
1518
cx, cy := end.X, end.Y
1619

17-
fmt.Printf("[D2ASCII] Drawing segment %d: (%.2f,%.2f) -> (%.2f,%.2f)\n",
18-
segmentIndex-1, ax, ay, cx, cy)
20+
log.Debug(ctx, "drawing segment", slog.Int("index", segmentIndex-1), slog.Float64("x1", ax), slog.Float64("y1", ay), slog.Float64("x2", cx), slog.Float64("y2", cy))
1921

2022
sx := cx - ax
2123
sy := cy - ay
2224
step := math.Max(math.Abs(sx), math.Abs(sy))
2325
if step == 0 {
24-
fmt.Printf("[D2ASCII] Zero-length segment, skipping\n")
26+
log.Debug(ctx, "zero-length segment, skipping")
2527
return
2628
}
2729
sx /= step
2830
sy /= step
2931

30-
fmt.Printf("[D2ASCII] Step vector: (%.2f, %.2f), total steps: %.0f\n", sx, sy, step)
32+
log.Debug(ctx, "step vector", slog.Float64("x", sx), slog.Float64("y", sy), slog.Float64("steps", step))
3133

3234
fx, fy := ax, ay
3335
attempt := 0
@@ -38,9 +40,9 @@ func drawSegmentBetweenPoints(rd RouteDrawer, start, end *geo.Point, segmentInde
3840
attempt++
3941
if x == int(math.Round(cx)) && y == int(math.Round(cy)) || attempt == MaxRouteAttempts {
4042
if attempt == MaxRouteAttempts {
41-
fmt.Printf("[D2ASCII] Max route attempts (%d) reached\n", MaxRouteAttempts)
43+
log.Debug(ctx, "max route attempts reached", slog.Int("attempts", MaxRouteAttempts))
4244
} else {
43-
fmt.Printf("[D2ASCII] Reached segment endpoint at (%d, %d)\n", x, y)
45+
log.Debug(ctx, "reached segment endpoint", slog.Int("x", x), slog.Int("y", y))
4446
}
4547
break
4648
}
@@ -49,13 +51,13 @@ func drawSegmentBetweenPoints(rd RouteDrawer, start, end *geo.Point, segmentInde
4951

5052
// Skip if out of bounds or contains alphanumeric character
5153
if !isInBounds(rd, x, y) {
52-
fmt.Printf("[D2ASCII] Position (%d, %d) out of bounds, skipping\n", x, y)
54+
log.Debug(ctx, "position out of bounds, skipping", slog.Int("x", x), slog.Int("y", y))
5355
fx += sx
5456
fy += sy
5557
continue
5658
}
5759
if containsAlphaNumeric(rd, x, y) {
58-
fmt.Printf("[D2ASCII] Position (%d, %d) contains alphanumeric, skipping\n", x, y)
60+
log.Debug(ctx, "position contains alphanumeric, skipping", slog.Int("x", x), slog.Int("y", y))
5961
fx += sx
6062
fy += sy
6163
continue
@@ -85,41 +87,40 @@ func drawRoutePoint(rd RouteDrawer, x, y int, sx, sy float64, segmentIndex, rout
8587

8688
// Check for corners first
8789
if char, ok := corners[turnDir[key]]; ok {
88-
fmt.Printf("[D2ASCII] Drawing corner at (%d, %d): '%s' (direction: %s)\n", x, y, char, turnDir[key])
90+
log.Debug(rd.GetContext(), "drawing corner", slog.Int("x", x), slog.Int("y", y), slog.String("char", char), slog.String("direction", turnDir[key]))
8991
canvas.Set(x, y, char)
9092
return
9193
}
9294

9395
// Check for destination arrow
9496
if segmentIndex == routeLen-1 && x == int(math.Round(cx)) && y == int(math.Round(cy)) && conn.DstArrow != d2target.NoArrowhead {
95-
fmt.Printf("[D2ASCII] Drawing destination arrow at (%d, %d)\n", x, y)
97+
log.Debug(rd.GetContext(), "drawing destination arrow", slog.Int("x", x), slog.Int("y", y))
9698
drawArrowhead(rd, x, y, sx, sy, arrows)
9799
if conn.DstLabel != nil {
98-
fmt.Printf("[D2ASCII] Drawing destination label: %s\n", conn.DstLabel.Label)
100+
log.Debug(rd.GetContext(), "drawing destination label", slog.String("label", conn.DstLabel.Label))
99101
drawDestinationLabel(rd, conn.DstLabel.Label, cx, cy, sx, sy)
100102
}
101103
return
102104
}
103105

104106
// Check for source arrow
105107
if segmentIndex == 1 && x == int(math.Round(ax)) && y == int(math.Round(ay)) && conn.SrcArrow != d2target.NoArrowhead {
106-
fmt.Printf("[D2ASCII] Drawing source arrow at (%d, %d)\n", x, y)
108+
log.Debug(rd.GetContext(), "drawing source arrow", slog.Int("x", x), slog.Int("y", y))
107109
arrowKey := fmt.Sprintf("%d%d", geo.Sign(sx)*-1, geo.Sign(sy)*-1)
108110
canvas.Set(x, y, arrows[arrowKey])
109111
if conn.SrcLabel != nil {
110-
fmt.Printf("[D2ASCII] Drawing source label: %s\n", conn.SrcLabel.Label)
112+
log.Debug(rd.GetContext(), "drawing source label", slog.String("label", conn.SrcLabel.Label))
111113
drawSourceLabel(rd, conn.SrcLabel.Label, ax, cy, cx, sx, sy)
112114
}
113115
return
114116
}
115117

116118
// Default: draw route segment
117-
fmt.Printf("[D2ASCII] Drawing route segment at (%d, %d), existing: '%s'\n",
118-
x, y, existingChar)
119-
drawRouteSegment(rd, x, y, sx, sy, frmBoundary, toBoundary)
119+
log.Debug(rd.GetContext(), "drawing route segment", slog.Int("x", x), slog.Int("y", y), slog.String("existing", string(existingChar)))
120+
drawRouteSegment(rd.GetContext(), rd, x, y, sx, sy, frmBoundary, toBoundary)
120121
}
121122

122-
func drawRouteSegment(rd RouteDrawer, x, y int, sx, sy float64, frmBoundary, toBoundary Boundary) {
123+
func drawRouteSegment(ctx context.Context, rd RouteDrawer, x, y int, sx, sy float64, frmBoundary, toBoundary Boundary) {
123124
if !isInBounds(rd, x, y) {
124125
return
125126
}
@@ -129,56 +130,53 @@ func drawRouteSegment(rd RouteDrawer, x, y int, sx, sy float64, frmBoundary, toB
129130
overWrite := existingChar != " "
130131

131132
if sx == 0 { // Vertical line
132-
fmt.Printf("[D2ASCII] Drawing vertical segment at (%d, %d), overwrite=%t, existing='%s'\n",
133-
x, y, overWrite, existingChar)
134-
drawVerticalSegment(rd, x, y, sy, overWrite, frmBoundary, toBoundary)
133+
log.Debug(ctx, "drawing vertical segment", slog.Int("x", x), slog.Int("y", y), slog.Bool("overwrite", overWrite), slog.String("existing", string(existingChar)))
134+
drawVerticalSegment(ctx, rd, x, y, sy, overWrite, frmBoundary, toBoundary)
135135
} else { // Horizontal line
136-
fmt.Printf("[D2ASCII] Drawing horizontal segment at (%d, %d), overwrite=%t, existing='%s'\n",
137-
x, y, overWrite, existingChar)
138-
drawHorizontalSegment(rd, x, y, sx, overWrite, frmBoundary, toBoundary)
136+
log.Debug(ctx, "drawing horizontal segment", slog.Int("x", x), slog.Int("y", y), slog.Bool("overwrite", overWrite), slog.String("existing", string(existingChar)))
137+
drawHorizontalSegment(ctx, rd, x, y, sx, overWrite, frmBoundary, toBoundary)
139138
}
140139

141140
newChar := canvas.Get(x, y)
142141
if newChar != existingChar {
143-
fmt.Printf("[D2ASCII] Character placed: '%s' -> '%s' at (%d, %d)\n",
144-
existingChar, newChar, x, y)
142+
log.Debug(ctx, "character placed", slog.String("from", string(existingChar)), slog.String("to", string(newChar)), slog.Int("x", x), slog.Int("y", y))
145143
}
146144
}
147145

148-
func drawVerticalSegment(rd RouteDrawer, x, y int, sy float64, overWrite bool, frmBoundary, toBoundary Boundary) {
146+
func drawVerticalSegment(ctx context.Context, rd RouteDrawer, x, y int, sy float64, overWrite bool, frmBoundary, toBoundary Boundary) {
149147
canvas := rd.GetCanvas()
150148
chars := rd.GetChars()
151149

152150
if overWrite && shouldDrawTJunction(rd, x, y, frmBoundary, toBoundary, true) {
153151
if sy > 0 {
154-
fmt.Printf("[D2ASCII] Drawing T-junction down at (%d, %d)\n", x, y)
152+
log.Debug(ctx, "drawing T-junction down", slog.Int("x", x), slog.Int("y", y))
155153
canvas.Set(x, y, chars.TDown())
156154
} else {
157-
fmt.Printf("[D2ASCII] Drawing T-junction up at (%d, %d)\n", x, y)
155+
log.Debug(ctx, "drawing T-junction up", slog.Int("x", x), slog.Int("y", y))
158156
canvas.Set(x, y, chars.TUp())
159157
}
160158
} else if overWrite && shouldSkipOverwrite(rd, x, y, frmBoundary, toBoundary) {
161-
fmt.Printf("[D2ASCII] Skipping overwrite at (%d, %d)\n", x, y)
159+
log.Debug(ctx, "skipping overwrite", slog.Int("x", x), slog.Int("y", y))
162160
} else {
163-
fmt.Printf("[D2ASCII] Drawing vertical line at (%d, %d)\n", x, y)
161+
log.Debug(ctx, "drawing vertical line", slog.Int("x", x), slog.Int("y", y))
164162
canvas.Set(x, y, chars.Vertical())
165163
}
166164
}
167165

168-
func drawHorizontalSegment(rd RouteDrawer, x, y int, sx float64, overWrite bool, frmBoundary, toBoundary Boundary) {
166+
func drawHorizontalSegment(ctx context.Context, rd RouteDrawer, x, y int, sx float64, overWrite bool, frmBoundary, toBoundary Boundary) {
169167
canvas := rd.GetCanvas()
170168
chars := rd.GetChars()
171169

172170
if overWrite && shouldDrawTJunction(rd, x, y, frmBoundary, toBoundary, false) {
173171
if sx > 0 {
174-
fmt.Printf("[D2ASCII] Drawing T-junction right at (%d, %d)\n", x, y)
172+
log.Debug(ctx, "drawing T-junction right", slog.Int("x", x), slog.Int("y", y))
175173
canvas.Set(x, y, chars.TRight())
176174
} else {
177-
fmt.Printf("[D2ASCII] Drawing T-junction left at (%d, %d)\n", x, y)
175+
log.Debug(ctx, "drawing T-junction left", slog.Int("x", x), slog.Int("y", y))
178176
canvas.Set(x, y, chars.TLeft())
179177
}
180178
} else {
181-
fmt.Printf("[D2ASCII] Drawing horizontal line at (%d, %d)\n", x, y)
179+
log.Debug(ctx, "drawing horizontal line", slog.Int("x", x), slog.Int("y", y))
182180
canvas.Set(x, y, chars.Horizontal())
183181
}
184182
}

0 commit comments

Comments
 (0)