Skip to content

Commit

Permalink
Merge pull request #2137 from alixander/slog
Browse files Browse the repository at this point in the history
replace external slog
  • Loading branch information
alixander authored Oct 4, 2024
2 parents 3615052 + 6035ec7 commit fba0a3e
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 255 deletions.
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Improvements 🧹

- Lib: removes a dependency on external slog that was causing troubles with installation [#2137](https://github.com/terrastruct/d2/pull/2137)

#### Bugfixes ⛑️

- Render: fixes edge case of a 3d shape with outside label being cut off [#2132](https://github.com/terrastruct/d2/pull/2132)
2 changes: 1 addition & 1 deletion d2chaos/d2chaos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func test(t *testing.T, textPath, text string) {
}
}()

ctx := log.WithTB(context.Background(), t, nil)
ctx := log.WithTB(context.Background(), t)

ruler, err := textmeasure.NewRuler()
assert.Nil(t, err)
Expand Down
19 changes: 5 additions & 14 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"os"
"os/exec"
"os/user"
Expand Down Expand Up @@ -36,7 +36,7 @@ import (
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/background"
"oss.terrastruct.com/d2/lib/imgbundler"
ctxlog "oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/log"
"oss.terrastruct.com/d2/lib/pdf"
"oss.terrastruct.com/d2/lib/png"
"oss.terrastruct.com/d2/lib/pptx"
Expand All @@ -45,15 +45,10 @@ import (
timelib "oss.terrastruct.com/d2/lib/time"
"oss.terrastruct.com/d2/lib/version"
"oss.terrastruct.com/d2/lib/xgif"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
)

func Run(ctx context.Context, ms *xmain.State) (err error) {
// :(
ctx = DiscardSlog(ctx)

ctx = log.WithDefault(ctx)
// These should be kept up-to-date with the d2 man page
watchFlag, err := ms.Opts.Bool("D2_WATCH", "watch", "w", false, "watch for changes to input and live reload. Use $HOST and $PORT to specify the listening address.\n(default localhost:0, which is will open on a randomly available local port).")
if err != nil {
Expand Down Expand Up @@ -169,6 +164,7 @@ func Run(ctx context.Context, ms *xmain.State) (err error) {
}

if *debugFlag {
ctx = log.Leveled(ctx, slog.LevelDebug)
ms.Env.Setenv("DEBUG", "1")
}
if *imgCacheFlag {
Expand Down Expand Up @@ -1181,11 +1177,6 @@ func getFileName(path string) string {
return strings.TrimSuffix(filepath.Base(path), ext)
}

// TODO: remove after removing slog
func DiscardSlog(ctx context.Context) context.Context {
return ctxlog.With(ctx, slog.Make(sloghuman.Sink(io.Discard)))
}

func populateLayoutOpts(ctx context.Context, ms *xmain.State, ps []d2plugin.Plugin) error {
pluginFlags, err := d2plugin.ListPluginFlags(ctx, ps)
if err != nil {
Expand Down Expand Up @@ -1379,5 +1370,5 @@ func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, er
}

func init() {
ctxlog.Init()
log.Init()
}
7 changes: 3 additions & 4 deletions d2exporter/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package d2exporter_test

import (
"context"
"log/slog"
"path/filepath"
"strings"
"testing"

"cdr.dev/slog"

tassert "github.com/stretchr/testify/assert"

"oss.terrastruct.com/util-go/assert"
Expand Down Expand Up @@ -218,7 +217,7 @@ func runa(t *testing.T, tcs []testCase) {

func run(t *testing.T, tc testCase) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
ctx = log.WithTB(ctx, t)
ctx = log.Leveled(ctx, slog.LevelDebug)

g, config, err := d2compiler.Compile("", strings.NewReader(tc.dsl), &d2compiler.CompileOptions{
Expand Down Expand Up @@ -277,7 +276,7 @@ func run(t *testing.T, tc testCase) {
// TestHashID tests that 2 diagrams with different theme configs do not equal each other
func TestHashID(t *testing.T) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
ctx = log.WithTB(ctx, t)
ctx = log.Leveled(ctx, slog.LevelDebug)

aString := `
Expand Down
11 changes: 6 additions & 5 deletions d2layouts/d2dagrelayout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"sort"
"strings"

"cdr.dev/slog"
"log/slog"

"github.com/dop251/goja"

"oss.terrastruct.com/util-go/xdefer"
Expand Down Expand Up @@ -179,7 +180,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
}

if debugJS {
log.Debug(ctx, "script", slog.F("all", setupJS+configJS+loadScript))
log.Debug(ctx, "script", slog.Any("all", setupJS+configJS+loadScript))
}

if _, err := vm.RunString(loadScript); err != nil {
Expand All @@ -188,7 +189,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err

if _, err := vm.RunString(`dagre.layout(g)`); err != nil {
if debugJS {
log.Warn(ctx, "layout error", slog.F("err", err))
log.Warn(ctx, "layout error", slog.Any("err", err))
}
return err
}
Expand All @@ -203,7 +204,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
return err
}
if debugJS {
log.Debug(ctx, "graph", slog.F("json", dn))
log.Debug(ctx, "graph", slog.Any("json", dn))
}

obj := mapper.ToObj(dn.ID)
Expand All @@ -224,7 +225,7 @@ func Layout(ctx context.Context, g *d2graph.Graph, opts *ConfigurableOpts) (err
return err
}
if debugJS {
log.Debug(ctx, "graph", slog.F("json", de))
log.Debug(ctx, "graph", slog.Any("json", de))
}

points := make([]*geo.Point, len(de.Points))
Expand Down
13 changes: 6 additions & 7 deletions d2layouts/d2layouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package d2layouts
import (
"context"
"fmt"
"log/slog"
"math"
"sort"
"strings"

"cdr.dev/slog"

"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2grid"
"oss.terrastruct.com/d2/d2layouts/d2near"
Expand Down Expand Up @@ -203,7 +202,7 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
extractedEdges = append(extractedEdges, externalEdges...)
extractedEdgeIDs = append(extractedEdgeIDs, externalEdgeIDs...)

log.Info(ctx, "layout nested", slog.F("level", curr.Level()), slog.F("child", curr.AbsID()), slog.F("gi", gi))
log.Debug(ctx, "layout nested", slog.Any("level", curr.Level()), slog.Any("child", curr.AbsID()), slog.Any("gi", gi))
nestedInfo := gi
nearKey := curr.NearKey
if gi.IsConstantNear {
Expand Down Expand Up @@ -250,19 +249,19 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
if len(g.Objects) > 0 {
switch graphInfo.DiagramType {
case GridDiagram:
log.Debug(ctx, "layout grid", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
log.Debug(ctx, "layout grid", slog.Any("rootlevel", g.RootLevel), slog.Any("shapes", g.PrintString()))
if err = d2grid.Layout(ctx, g); err != nil {
return err
}

case SequenceDiagram:
log.Debug(ctx, "layout sequence", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
log.Debug(ctx, "layout sequence", slog.Any("rootlevel", g.RootLevel), slog.Any("shapes", g.PrintString()))
err = d2sequence.Layout(ctx, g, coreLayout)
if err != nil {
return err
}
default:
log.Debug(ctx, "default layout", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
log.Debug(ctx, "default layout", slog.Any("rootlevel", g.RootLevel), slog.Any("shapes", g.PrintString()))
err := coreLayout(ctx, g)
if err != nil {
return err
Expand Down Expand Up @@ -337,7 +336,7 @@ func LayoutNested(ctx context.Context, g *d2graph.Graph, graphInfo GraphInfo, co
}
}

log.Debug(ctx, "done", slog.F("rootlevel", g.RootLevel), slog.F("shapes", g.PrintString()))
log.Debug(ctx, "done", slog.Any("rootlevel", g.RootLevel), slog.Any("shapes", g.PrintString()))
return err
}

Expand Down
10 changes: 5 additions & 5 deletions d2layouts/d2sequence/layout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ n2 -> n1

nEdges := len(g.Edges)

ctx := log.WithTB(context.Background(), t, nil)
ctx := log.WithTB(context.Background(), t)
d2sequence.Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error {
// just set some position as if it had been properly placed
for _, obj := range g.Objects {
Expand Down Expand Up @@ -176,7 +176,7 @@ b.t1 -> a.t1
a.t2 -> b
b -> a.t2`

ctx := log.WithTB(context.Background(), t, nil)
ctx := log.WithTB(context.Background(), t)
g, _, err := d2compiler.Compile("", strings.NewReader(input), nil)
assert.Nil(t, err)

Expand Down Expand Up @@ -297,7 +297,7 @@ func TestNestedSequenceDiagrams(t *testing.T) {
c
container -> c: edge 1
`
ctx := log.WithTB(context.Background(), t, nil)
ctx := log.WithTB(context.Background(), t)
g, _, err := d2compiler.Compile("", strings.NewReader(input), nil)
assert.Nil(t, err)

Expand Down Expand Up @@ -393,7 +393,7 @@ func TestSelfEdges(t *testing.T) {
},
}

ctx := log.WithTB(context.Background(), t, nil)
ctx := log.WithTB(context.Background(), t)
d2sequence.Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error {
return nil
})
Expand Down Expand Up @@ -435,7 +435,7 @@ func TestSequenceToDescendant(t *testing.T) {
},
}

ctx := log.WithTB(context.Background(), t, nil)
ctx := log.WithTB(context.Background(), t)
d2sequence.Layout(ctx, g, func(ctx context.Context, g *d2graph.Graph) error {
return nil
})
Expand Down
5 changes: 2 additions & 3 deletions d2renderers/d2sketch/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package d2sketch_test
import (
"context"
"encoding/xml"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"

"cdr.dev/slog"

tassert "github.com/stretchr/testify/assert"

"oss.terrastruct.com/util-go/assert"
Expand Down Expand Up @@ -1372,7 +1371,7 @@ func runa(t *testing.T, tcs []testCase) {

func run(t *testing.T, tc testCase) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
ctx = log.WithTB(ctx, t)
ctx = log.Leveled(ctx, slog.LevelDebug)

ruler, err := textmeasure.NewRuler()
Expand Down
5 changes: 2 additions & 3 deletions d2renderers/d2svg/appendix/appendix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"encoding/xml"
"io/ioutil"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"

"cdr.dev/slog"

tassert "github.com/stretchr/testify/assert"

"oss.terrastruct.com/util-go/assert"
Expand Down Expand Up @@ -145,7 +144,7 @@ func runa(t *testing.T, tcs []testCase) {

func run(t *testing.T, tc testCase) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
ctx = log.WithTB(ctx, t)
ctx = log.Leveled(ctx, slog.LevelDebug)

ruler, err := textmeasure.NewRuler()
Expand Down
5 changes: 2 additions & 3 deletions d2renderers/d2svg/dark_theme/dark_theme_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"context"
"encoding/xml"
"io/ioutil"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"

"cdr.dev/slog"

tassert "github.com/stretchr/testify/assert"

"oss.terrastruct.com/util-go/assert"
Expand Down Expand Up @@ -419,7 +418,7 @@ func runa(t *testing.T, tcs []testCase) {

func run(t *testing.T, tc testCase) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
ctx = log.WithTB(ctx, t)
ctx = log.Leveled(ctx, slog.LevelDebug)

ruler, err := textmeasure.NewRuler()
Expand Down
4 changes: 2 additions & 2 deletions e2etests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"encoding/xml"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"testing"

"cdr.dev/slog"
"golang.org/x/tools/txtar"

trequire "github.com/stretchr/testify/require"
Expand Down Expand Up @@ -140,7 +140,7 @@ func serde(t *testing.T, tc testCase, ruler *textmeasure.Ruler) {

func run(t *testing.T, tc testCase) {
ctx := context.Background()
ctx = log.WithTB(ctx, t, nil)
ctx = log.WithTB(ctx, t)
ctx = log.Leveled(ctx, slog.LevelDebug)

var ruler *textmeasure.Ruler
Expand Down
2 changes: 1 addition & 1 deletion e2etests/report/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
}

if !*skipTests {
ctx := log.Stderr(context.Background())
ctx := context.Background()

ctx, cancel := timelib.WithTimeout(ctx, 2*time.Minute)
defer cancel()
Expand Down
9 changes: 1 addition & 8 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fba0a3e

Please sign in to comment.