Skip to content

Commit

Permalink
Avoid needing to expose Logger on HostContext
Browse files Browse the repository at this point in the history
It still is, but for now I don't think it'll be needed except to make it easier to use stuff like Fatalf in testing
  • Loading branch information
maxfierke committed Jan 18, 2024
1 parent e7eb6ad commit 6e38ef3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
17 changes: 16 additions & 1 deletion devices/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

type HostContext interface {
Logger() *log.Logger
Log(msg string, args ...any)
LogErr(msg string, args ...any)
LogWarn(msg string, args ...any)
SerialCable() SerialCable
}

Expand All @@ -25,6 +28,18 @@ func (h *Host) Logger() *log.Logger {
return h.logger
}

func (h *Host) Log(msg string, args ...any) {
h.logger.Printf(msg+"\n", args...)
}

func (h *Host) LogErr(msg string, args ...any) {
h.Log("ERROR: "+msg, args...)
}

func (h *Host) LogWarn(msg string, args ...any) {
h.Log("WARN: "+msg, args...)
}

func (h *Host) SetLogger(logger *log.Logger) {
h.logger = logger
}
Expand All @@ -33,6 +48,6 @@ func (h *Host) SerialCable() SerialCable {
return h.serialCable
}

func (h *Host) SetSerialCable(serialCable SerialCable) {
func (h *Host) AttachSerialCable(serialCable SerialCable) {
h.serialCable = serialCable
}
2 changes: 1 addition & 1 deletion hardware/dmg.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (dmg *DMG) Step() bool {

cycles, err := dmg.cpu.Step(dmg.mmu)
if err != nil {
dmg.host.Logger().Printf("Unexpected error while executing instruction: %v\n", err)
dmg.host.LogErr("Unexpected error while executing instruction", err)
return false
}

Expand Down
30 changes: 15 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
} else {
logFile, err := os.Create(options.logPath)
if err != nil {
log.Fatalf("Unable to open log file '%s' for writing: %v\n", options.logPath, err)
log.Fatalf("ERROR: Unable to open log file '%s' for writing: %v\n", options.logPath, err)
}
defer logFile.Close()

Expand Down Expand Up @@ -66,7 +66,7 @@ func debugPrint(options *CLIOptions) {
case "opcodes":
debugPrintOpcodes(options)
default:
options.logger.Fatalf("unrecognized \"debug-print\" option: %v\n", options.debugPrint)
options.logger.Fatalf("ERROR: unrecognized \"debug-print\" option: %v\n", options.debugPrint)
}
}

Expand All @@ -75,15 +75,15 @@ func debugPrintCartHeader(options *CLIOptions) {

cartFile, err := os.Open(options.cartPath)
if options.cartPath == "" || err != nil {
logger.Fatalf("Unable to load cartridge. Please ensure it's inserted correctly (exists): %v\n", err)
logger.Fatalf("ERROR: Unable to load cartridge. Please ensure it's inserted correctly (exists): %v\n", err)
}
defer cartFile.Close()

cartReader, err := cart.NewReader(cartFile)
if err == cart.ErrHeader {
logger.Printf("Warning: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
logger.Printf("WARN: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
} else if err != nil {
logger.Fatalf("Unable to load cartridge. Please ensure it's inserted correctly or trying blowing on it: %v\n", err)
logger.Fatalf("ERROR: Unable to load cartridge. Please ensure it's inserted correctly or trying blowing on it: %v\n", err)
}

cartReader.Header.DebugPrint(logger)
Expand All @@ -94,7 +94,7 @@ func debugPrintOpcodes(options *CLIOptions) {

opcodes, err := isa.LoadOpcodes()
if err != nil {
logger.Fatalf("Unable to load opcodes: %v\n", err)
logger.Fatalf("ERROR: Unable to load opcodes: %v\n", err)
}

opcodes.DebugPrint(logger)
Expand All @@ -116,24 +116,24 @@ func initDMG(options *CLIOptions) *hardware.DMG {
} else {
serialPort, err := os.Create(options.serialPort)
if err != nil {
logger.Fatalf("Unable to open file '%s' as serial port: %v\n", options.serialPort, err)
logger.Fatalf("ERROR: Unable to open file '%s' as serial port: %v\n", options.serialPort, err)
}

serialCable.SetReader(serialPort)
serialCable.SetWriter(serialPort)
}

host.SetSerialCable(serialCable)
host.AttachSerialCable(serialCable)
}

debugger, err := debug.NewDebugger(options.debugger)
if err != nil {
logger.Fatalf("Unable to initialize Debugger: %v\n", err)
logger.Fatalf("ERROR: Unable to initialize Debugger: %v\n", err)
}

dmg, err := hardware.NewDMGDebug(host, debugger)
if err != nil {
logger.Fatalf("Unable to initialize DMG: %v\n", err)
logger.Fatalf("ERROR: Unable to initialize DMG: %v\n", err)
}

return dmg
Expand All @@ -148,22 +148,22 @@ func loadCart(dmg *hardware.DMG, options *CLIOptions) {

cartFile, err := os.Open(options.cartPath)
if options.cartPath == "" || err != nil {
logger.Fatalf("Unable to load cartridge. Please ensure it's inserted correctly (e.g. file exists): %v\n", err)
logger.Fatalf("ERROR: Unable to load cartridge. Please ensure it's inserted correctly (e.g. file exists): %v\n", err)
}
defer cartFile.Close()

cartReader, err := cart.NewReader(cartFile)
if err == cart.ErrHeader {
logger.Printf("Warning: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
logger.Printf("WARN: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
} else if err != nil {
logger.Fatalf("Unable to load cartridge. Please ensure it's inserted correctly (e.g. file exists): %v\n", err)
logger.Fatalf("ERROR: Unable to load cartridge. Please ensure it's inserted correctly (e.g. file exists): %v\n", err)
}

err = dmg.LoadCartridge(cartReader)
if err == cart.ErrHeader {
logger.Printf("Warning: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
logger.Printf("WARN: Cartridge header does not match expected checksum. Continuing, but subsequent operations may fail")
} else if err != nil {
logger.Fatalf("Unable to load cartridge: %v\n", err)
logger.Fatalf("ERROR: Unable to load cartridge: %v\n", err)
}
}

Expand Down

0 comments on commit 6e38ef3

Please sign in to comment.