Skip to content

Commit

Permalink
log: add leveled log
Browse files Browse the repository at this point in the history
  • Loading branch information
icexin committed Jul 31, 2021
1 parent da213e3 commit a2fac43
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 131 deletions.
4 changes: 2 additions & 2 deletions app/cmd/nes.go
Expand Up @@ -11,9 +11,9 @@ import (
"time"

"github.com/icexin/eggos/app"
"github.com/icexin/eggos/debug"
"github.com/icexin/eggos/drivers/kbd"
"github.com/icexin/eggos/drivers/vbe"
"github.com/icexin/eggos/log"

"github.com/fogleman/nes/nes"
"golang.org/x/image/draw"
Expand Down Expand Up @@ -117,7 +117,7 @@ func runGame(rom io.Reader, scaler draw.Interpolator) error {
sum += used
cnt++
if cnt%30 == 0 {
debug.Logf("used %s", sum/time.Duration(cnt))
log.Infof("used %s", sum/time.Duration(cnt))
cnt = 0
sum = 0
}
Expand Down
38 changes: 0 additions & 38 deletions debug/log.go

This file was deleted.

28 changes: 14 additions & 14 deletions drivers/e1000/e1000.go
Expand Up @@ -6,12 +6,12 @@ import (
"time"
"unsafe"

"github.com/icexin/eggos/debug"
"github.com/icexin/eggos/drivers/pci"
"github.com/icexin/eggos/drivers/pic"
"github.com/icexin/eggos/inet"
"github.com/icexin/eggos/kernel/mm"
"github.com/icexin/eggos/kernel/sys"
"github.com/icexin/eggos/log"

"gvisor.dev/gvisor/pkg/tcpip/buffer"
"gvisor.dev/gvisor/pkg/tcpip/stack"
Expand Down Expand Up @@ -131,7 +131,7 @@ func (d *driver) Init(dev *pci.Device) error {

d.dev = dev

debug.Logf("[e1000] enable bus master")
log.Infof("[e1000] enable bus master")
dev.Addr.EnableBusMaster()

// mmap bar address
Expand All @@ -141,7 +141,7 @@ func (d *driver) Init(dev *pci.Device) error {
}
mm.SysFixedMmap(uintptr(baddr), uintptr(baddr), uintptr(blen))
d.bar = uintptr(baddr)
debug.Logf("[e1000] mmap for bar0 0x%x", d.bar)
log.Infof("[e1000] mmap for bar0 0x%x", d.bar)

// alloc desc
descptr := mm.Alloc()
Expand All @@ -152,21 +152,21 @@ func (d *driver) Init(dev *pci.Device) error {
// disable all intrs
d.writecmd(REG_IMC, 0xffffffff)

debug.Logf("[e1000] begin reset")
log.Infof("[e1000] begin reset")
// Reset the device.
d.writecmd(REG_CTRL, d.readcmd(REG_CTRL)|CTRL_RST)

// Wait until the device gets reset.
// for (d.readcmd(REG_CTRL) & CTRL_RST) != 0 {
// }
time.Sleep(time.Microsecond)
debug.Logf("[e1000] reset done")
log.Infof("[e1000] reset done")
// again disable all intrs after reset
d.writecmd(REG_IMC, 0xffffffff)

// Link up!
d.writecmd(REG_CTRL, d.readcmd(REG_CTRL)|CTRL_SLU|CTRL_ASDE)
debug.Logf("[e1000] link up")
log.Infof("[e1000] link up")

// Fill Multicast Table Array with zeros.
for i := uint16(0); i < 0x80; i++ {
Expand Down Expand Up @@ -213,9 +213,9 @@ func (d *driver) Init(dev *pci.Device) error {
d.readcmd(REG_ICR)
d.writecmd(REG_ICR, ^uint32(0))

debug.Logf("[e1000] begin read mac")
log.Infof("[e1000] begin read mac")
d.readmac()
debug.Logf("[e1000] mac:%x", d.mac)
log.Infof("[e1000] mac:%x", d.mac)
// go d.recvloop()
return nil
}
Expand Down Expand Up @@ -244,15 +244,15 @@ func (d *driver) Transmit(pkt *stack.PacketBuffer) error {

// for atomic.LoadInt32((*int32)(unsafe.Pointer(&desc.status))) == 0 {
// }
// debug.Logf("[e1000] send %d bytes", pktlen)
// log.Infof("[e1000] send %d bytes", pktlen)
return nil
}

func (d *driver) Intr() {
defer pic.EnableIRQ(uint16(d.dev.IRQLine))
defer pic.EOI(uintptr(d.dev.IRQNO))
cause := d.readcmd(REG_ICR)
// debug.Logf("[e1000] cause %x", cause)
// log.Infof("[e1000] cause %x", cause)
// clear ICR register
// e1000e may not clear upon read
d.writecmd(REG_ICR, ^uint32(0))
Expand All @@ -278,9 +278,9 @@ func (d *driver) recvloop() {
func (d *driver) readpkt() bool {
d.rxidx = d.readcmd(REG_RDT)
d.rxidx = (d.rxidx + 1) % NUM_RX_DESCS
// debug.Logf("[e1000] head:%d", d.readcmd(0x02810))
// debug.Logf("[e1000] tail:%d", d.readcmd(0x02818))
// debug.Logf("[e1000] rxidx:%d", d.rxidx)
// log.Infof("[e1000] head:%d", d.readcmd(0x02810))
// log.Infof("[e1000] tail:%d", d.readcmd(0x02818))
// log.Infof("[e1000] rxidx:%d", d.rxidx)
desc := &d.rxdescs[d.rxidx]

// fmt.Printf("status:%x\n", atomic.LoadUint32((*uint32)(unsafe.Pointer(&desc.status))))
Expand All @@ -291,7 +291,7 @@ func (d *driver) readpkt() bool {
}

buf := sys.UnsafeBuffer(uintptr(desc.paddr), int(desc.len))
// debug.Logf("[e1000] read %d bytes", desc.len)
// log.Infof("[e1000] read %d bytes", desc.len)
if d.rxfunc != nil {
d.rxfunc(buf)
}
Expand Down
6 changes: 3 additions & 3 deletions drivers/pci/pci.go
@@ -1,9 +1,9 @@
package pci

import (
"github.com/icexin/eggos/debug"
"github.com/icexin/eggos/drivers/pic"
"github.com/icexin/eggos/kernel/trap"
"github.com/icexin/eggos/log"
)

type Identity struct {
Expand Down Expand Up @@ -74,10 +74,10 @@ func Init() {
for _, driver := range drivers {
dev := findDev(driver.Idents())
if dev == nil {
debug.Logf("[pci] no pci device found for %v\n", driver.Name())
log.Infof("[pci] no pci device found for %v\n", driver.Name())
continue
}
debug.Logf("[pci] found %x:%x for %s, irq:%d\n", dev.Ident.Vendor, dev.Ident.Device, driver.Name(), dev.IRQNO)
log.Infof("[pci] found %x:%x for %s, irq:%d\n", dev.Ident.Vendor, dev.Ident.Device, driver.Name(), dev.IRQNO)
driver.Init(dev)
pic.EnableIRQ(uint16(dev.IRQLine))
trap.Register(int(dev.IRQNO), driver.Intr)
Expand Down
6 changes: 3 additions & 3 deletions drivers/ps2/mouse/mouse.go
Expand Up @@ -41,12 +41,12 @@ func intr() {
pic.EOI(_IRQ_MOUSE)
for {
st := ps2.ReadCmd()
// debug.Logf("status:%08b", st)
// log.Infof("status:%08b", st)
if st&0x01 == 0 {
break
}
x := ps2.ReadDataNoWait()
// debug.Logf("data:%08b", x)
// log.Infof("data:%08b", x)
handlePacket(x)
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ func handlePacket(v byte) {
default:
}
}
// debug.Logf("x:%d y:%d packet:%v status:%8b", xpos, ypos, packet, status)
// log.Infof("x:%d y:%d packet:%v status:%8b", xpos, ypos, packet, status)
}

func xrel(status byte, value int) int {
Expand Down
13 changes: 5 additions & 8 deletions inet/dhcp/client.go
Expand Up @@ -9,13 +9,12 @@ import (
"context"
"errors"
"fmt"
"log"
"math/rand"
"net"
"sync"
"time"

"github.com/icexin/eggos/debug"
"github.com/icexin/eggos/log"

"gvisor.dev/gvisor/pkg/tcpip"
"gvisor.dev/gvisor/pkg/tcpip/adapters/gonet"
Expand Down Expand Up @@ -55,15 +54,13 @@ func NewClient(s *stack.Stack, nicid tcpip.NICID, linkAddr tcpip.LinkAddress) *C
func (c *Client) Start() {
go func() {
for {
log.Print("DHCP request")
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
err := c.Request(ctx, "")
cancel()
if err == nil {
break
}
}
log.Printf("DHCP acquired IP %s for %s", c.Address(), c.Config().LeaseLength)
}()
}

Expand Down Expand Up @@ -171,7 +168,7 @@ func (c *Client) Request(ctx context.Context, requestedAddr tcpip.Address) error
if err != nil {
return fmt.Errorf("dhcp offer: %v", err)
}
debug.Logf("[dhcp] offer done")
log.Infof("[dhcp] offer done")

var ack bool
var cfg Config
Expand Down Expand Up @@ -207,7 +204,7 @@ func (c *Client) Request(ctx context.Context, requestedAddr tcpip.Address) error
{optReqIPAddr, []byte(addr)},
{optDHCPServer, []byte(cfg.ServerAddress)},
})
debug.Logf("[dhcp] offer ip:%s server:%s", addr, cfg.ServerAddress)
log.Infof("[dhcp] offer ip:%s server:%s", addr, cfg.ServerAddress)
_, err = conn.WriteTo(h, serverAddr)
if err != nil {
return err
Expand Down Expand Up @@ -239,7 +236,7 @@ func (c *Client) Request(ctx context.Context, requestedAddr tcpip.Address) error
if !ack {
return fmt.Errorf("dhcp: request not acknowledged")
}
debug.Logf("[dhcp] lease:%s", cfg.LeaseLength)
log.Infof("[dhcp] lease:%s", cfg.LeaseLength)
if cfg.LeaseLength != 0 {
go c.renewAfter(cfg.LeaseLength)
}
Expand All @@ -261,7 +258,7 @@ func (c *Client) renewAfter(d time.Duration) {
case <-ctx.Done():
case <-timer.C:
if err := c.Request(ctx, c.addr); err != nil {
log.Printf("address renewal failed: %v", err)
log.Errorf("address renewal failed: %v", err)
go c.renewAfter(1 * time.Minute)
}
}
Expand Down

0 comments on commit a2fac43

Please sign in to comment.