Skip to content

Commit

Permalink
chore: Consolidate log-related funcs into log package
Browse files Browse the repository at this point in the history
This fixes Ebiten being included in all generate packages
  • Loading branch information
gabe565 committed Oct 3, 2024
1 parent dfb71f9 commit 84f8e73
Show file tree
Hide file tree
Showing 22 changed files with 79 additions and 69 deletions.
6 changes: 3 additions & 3 deletions cmd/gones/icons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"os"
"testing"

"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/log"
"github.com/stretchr/testify/assert"
)

//nolint:paralleltest
func Test_getWindowIcons(t *testing.T) {
var buf bytes.Buffer
config.InitLog(&buf)
log.Init(&buf)
t.Cleanup(func() {
config.InitLog(os.Stderr)
log.Init(os.Stderr)
})

icons := getWindowIcons()
Expand Down
4 changes: 2 additions & 2 deletions cmd/gonesutil/ls/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"sync"

"github.com/gabe565/gones/internal/cartridge"
"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/log"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func New() *cobra.Command {

cmd.Flags().BoolP("reverse", "r", false, "Reverse the output")

config.InitLog(os.Stderr)
log.Init(os.Stderr)
return cmd
}

Expand Down
4 changes: 2 additions & 2 deletions internal/apu/apu.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/interrupt"
"github.com/gabe565/gones/internal/log"
"github.com/gabe565/gones/internal/memory"
"github.com/gabe565/gones/internal/util"
)

type CPU interface {
Expand Down Expand Up @@ -119,7 +119,7 @@ func (a *APU) WriteMem(addr uint16, data byte) {
a.stepLength()
}
default:
slog.Error("Invalid APU write", "addr", util.HexAddr(addr))
slog.Error("Invalid APU write", "addr", log.HexAddr(addr))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/gabe565/gones/internal/cartridge"
"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/controller"
"github.com/gabe565/gones/internal/log"
"github.com/gabe565/gones/internal/ppu"
"github.com/gabe565/gones/internal/util"
)

func New(conf *config.Config, mapper cartridge.Mapper, ppu *ppu.PPU, apu *apu.APU) *Bus {
Expand Down Expand Up @@ -55,7 +55,7 @@ func (b *Bus) ReadMem(addr uint16) byte {
case 0x4020 <= addr:
b.OpenBus = b.mapper.ReadMem(addr)
default:
slog.Error("Invalid Bus read", "addr", util.HexAddr(addr))
slog.Error("Invalid Bus read", "addr", log.HexAddr(addr))
return 0
}
return b.OpenBus
Expand Down Expand Up @@ -96,7 +96,7 @@ func (b *Bus) WriteMem(addr uint16, data byte) {
case 0x4020 <= addr:
b.mapper.WriteMem(addr, data)
default:
slog.Error("Invalid Bus write", "addr", util.HexAddr(addr))
slog.Error("Invalid Bus write", "addr", log.HexAddr(addr))
}
b.OpenBus = data
}
Expand Down
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_1.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"

"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/util"
"github.com/gabe565/gones/internal/log"
)

func NewMapper1(cartridge *Cartridge) *Mapper1 {
Expand Down Expand Up @@ -48,7 +48,7 @@ func (m *Mapper1) ReadMem(addr uint16) byte {
offset := int(addr % consts.PRGChunkSize)
return m.cartridge.prg[m.PRGOffsets[bank]+offset]
default:
slog.Error("Invalid mapper 1 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 1 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func (m *Mapper1) WriteMem(addr uint16, data byte) {
}
}
default:
slog.Error("Invalid mapper 1 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 1 write", "addr", log.HexAddr(addr))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"

"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/util"
"github.com/gabe565/gones/internal/log"
)

func NewMapper2(cartridge *Cartridge) *Mapper2 {
Expand Down Expand Up @@ -46,7 +46,7 @@ func (m *Mapper2) ReadMem(addr uint16) byte {
addr += m.PRGBank2 * consts.PRGChunkSize
return m.cartridge.prg[addr]
default:
slog.Error("Invalid mapper 2 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 2 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand All @@ -63,6 +63,6 @@ func (m *Mapper2) WriteMem(addr uint16, data byte) {
data %= m.PRGBanks
m.PRGBank1 = data
default:
slog.Error("Invalid mapper 2 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 2 write", "addr", log.HexAddr(addr))
}
}
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_3.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"

"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/util"
"github.com/gabe565/gones/internal/log"
)

func NewMapper3(cartridge *Cartridge) *Mapper3 {
Expand Down Expand Up @@ -47,7 +47,7 @@ func (m *Mapper3) ReadMem(addr uint16) byte {
addr += m.PRGBank2 * consts.PRGChunkSize
return m.cartridge.prg[addr]
default:
slog.Error("Invalid mapper 3 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 3 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand All @@ -64,6 +64,6 @@ func (m *Mapper3) WriteMem(addr uint16, data byte) {
case 0x8000 <= addr:
m.CHRBank = uint(data & 3)
default:
slog.Error("Invalid mapper 3 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 3 write", "addr", log.HexAddr(addr))
}
}
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_4.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package cartridge
import (
"log/slog"

"github.com/gabe565/gones/internal/log"
"github.com/gabe565/gones/internal/ppu/registers"
"github.com/gabe565/gones/internal/util"
)

func NewMapper4(cartridge *Cartridge) *Mapper4 {
Expand Down Expand Up @@ -78,7 +78,7 @@ func (m *Mapper4) ReadMem(addr uint16) byte {
offset := int(addr % 0x2000)
return m.cartridge.prg[m.PRGOffsets[bank]+offset]
default:
slog.Error("Invalid mapper 4 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 4 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (m *Mapper4) WriteMem(addr uint16, data byte) {
m.IRQPending = false
}
default:
slog.Error("Invalid mapper 4 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 4 write", "addr", log.HexAddr(addr))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_69.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cartridge
import (
"log/slog"

"github.com/gabe565/gones/internal/util"
"github.com/gabe565/gones/internal/log"
)

func NewMapper69(cartridge *Cartridge) *Mapper69 {
Expand Down Expand Up @@ -74,7 +74,7 @@ func (m *Mapper69) ReadMem(addr uint16) byte {
addr := m.PRGBanks[bank]*0x2000 + offset
return m.cartridge.prg[addr%len(m.cartridge.prg)]
default:
slog.Error("Invalid mapper 69 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 69 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand All @@ -93,7 +93,7 @@ func (m *Mapper69) WriteMem(addr uint16, data byte) {
// Parameter register
m.runCommand(data)
default:
slog.Error("Invalid mapper 69 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 69 write", "addr", log.HexAddr(addr))
}
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_7.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"

"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/util"
"github.com/gabe565/gones/internal/log"
)

func NewMapper7(cartridge *Cartridge) *Mapper7 {
Expand Down Expand Up @@ -36,7 +36,7 @@ func (m *Mapper7) ReadMem(addr uint16) byte {
addr %= uint(len(m.cartridge.prg))
return m.cartridge.prg[addr]
default:
slog.Error("Invalid mapper 7 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 7 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand All @@ -58,6 +58,6 @@ func (m *Mapper7) WriteMem(addr uint16, data byte) {
}
m.PRGBank = uint(data & 7)
default:
slog.Error("Invalid mapper 7 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 7 write", "addr", log.HexAddr(addr))
}
}
6 changes: 3 additions & 3 deletions internal/cartridge/mapper_71.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"

"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/util"
"github.com/gabe565/gones/internal/log"
)

func NewMapper71(cartridge *Cartridge) *Mapper71 {
Expand Down Expand Up @@ -43,7 +43,7 @@ func (m *Mapper71) ReadMem(addr uint16) byte {
addr += m.PRGLast * consts.PRGChunkSize
return m.cartridge.prg[addr]
default:
slog.Error("Invalid mapper 71 read", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 71 read", "addr", log.HexAddr(addr))
return 0
}
}
Expand All @@ -62,6 +62,6 @@ func (m *Mapper71) WriteMem(addr uint16, data byte) {
data %= m.PRGCount
m.PRGActive = data
default:
slog.Error("Invalid mapper 71 write", "addr", util.HexAddr(addr))
slog.Error("Invalid mapper 71 write", "addr", log.HexAddr(addr))
}
}
3 changes: 2 additions & 1 deletion internal/config/generate_example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"os"

"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/log"
"github.com/pelletier/go-toml/v2"
)

func main() {
config.InitLog(os.Stderr)
log.Init(os.Stderr)

f, err := os.Create("config_example.toml")
if err != nil {
Expand Down
21 changes: 2 additions & 19 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@ package config
import (
"bytes"
"errors"
"io"
"log/slog"
"os"
"path/filepath"
"time"

"github.com/gabe565/gones/internal/consts"
"github.com/gabe565/gones/internal/log"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/providers/structs"
"github.com/knadh/koanf/v2"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
)

func Load(cmd *cobra.Command) (*Config, error) {
InitLog(cmd.ErrOrStderr())
log.Init(cmd.ErrOrStderr())

k := koanf.New(".")
conf := NewDefault()
Expand Down Expand Up @@ -192,18 +190,3 @@ func fixConfig(k *koanf.Koanf) error {

return nil
}

func InitLog(out io.Writer) {
var color bool
if f, ok := out.(*os.File); ok {
color = isatty.IsTerminal(f.Fd()) || isatty.IsCygwinTerminal(f.Fd())
}

slog.SetDefault(slog.New(
tint.NewHandler(out, &tint.Options{
Level: slog.LevelInfo,
TimeFormat: time.Kitchen,
NoColor: !color,
}),
))
}
4 changes: 2 additions & 2 deletions internal/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"log/slog"

"github.com/gabe565/gones/internal/interrupt"
"github.com/gabe565/gones/internal/log"
"github.com/gabe565/gones/internal/memory"
"github.com/gabe565/gones/internal/util"
)

func New(b memory.ReadSafeWrite) *CPU {
Expand Down Expand Up @@ -114,7 +114,7 @@ func (c *CPU) Step() uint {
op := opcodes[code]
if op == nil {
c.StepErr = fmt.Errorf("%w: $%02X", ErrUnsupportedOpcode, code)
slog.Error("Failed to step CPU", "error", ErrUnsupportedOpcode, "code", util.HexVal(code))
slog.Error("Failed to step CPU", "error", ErrUnsupportedOpcode, "code", log.HexVal(code))
return 1
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cpu/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"runtime"
"strings"

"github.com/gabe565/gones/internal/log"
"github.com/gabe565/gones/internal/memory"
"github.com/gabe565/gones/internal/util"
)

func (c *CPU) Trace() string {
Expand Down Expand Up @@ -64,7 +64,7 @@ func (c *CPU) Trace() string {
default:
slog.Error("Invalid addressing mode has len 2",
"mode", op.Mode,
"code", util.HexVal(code),
"code", log.HexVal(code),
)
}
case 3:
Expand All @@ -89,7 +89,7 @@ func (c *CPU) Trace() string {
default:
slog.Error("Invalid addressing mode has len 3",
"mode", op.Mode,
"code", util.HexVal(code),
"code", log.HexVal(code),
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/database/generate/download/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"os"
"time"

"github.com/gabe565/gones/internal/config"
"github.com/gabe565/gones/internal/log"
)

func main() {
config.InitLog(os.Stderr)
log.Init(os.Stderr)

action, err := NewDownloader("Nintendo - Nintendo Entertainment System")
if err != nil {
Expand Down
Loading

0 comments on commit 84f8e73

Please sign in to comment.