Explorar el Código

Updated: Added logging to file. We chdir.

We need to chdir to the exe file so we know where our
config files are, and where we will create logfiles.
Steve Thielemann hace 4 años
padre
commit
191975b6de
Se han modificado 6 ficheros con 107 adiciones y 21 borrados
  1. 1 0
      door/convert.go
  2. 24 9
      door/door.go
  3. 9 9
      door/input.go
  4. 6 1
      door/input_test.go
  5. 65 2
      door/menu_test.go
  6. 2 0
      door/panel.go

+ 1 - 0
door/convert.go

@@ -6,6 +6,7 @@ package door
 // This converts CP437 to Unicode for rendering in Unicode terminals
 // This converts CP437 to Unicode for rendering in Unicode terminals
 // like the linux telnet client.
 // like the linux telnet client.
 // This does everything but: \x07, \x08, \x0a and \x0d.
 // This does everything but: \x07, \x08, \x0a and \x0d.
+// 0x07, 0x08, 0x09, 0x0a, 0x0c, 0x0d, 0x1b (ANSI spec)
 func CP437_to_Unicode(cp437 string) string {
 func CP437_to_Unicode(cp437 string) string {
 	var result string
 	var result string
 
 

+ 24 - 9
door/door.go

@@ -22,7 +22,9 @@ import (
 	"bufio"
 	"bufio"
 	"flag"
 	"flag"
 	"fmt"
 	"fmt"
+	"log"
 	"os"
 	"os"
+	"path/filepath"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"syscall"
 	"syscall"
@@ -208,10 +210,10 @@ func (d *Door) detect() {
 		r, err := syscall.Read(d.READFD, buffer)
 		r, err := syscall.Read(d.READFD, buffer)
 		results = string(buffer[:r])
 		results = string(buffer[:r])
 		output := strings.Replace(results, "\x1b", "^[", -1)
 		output := strings.Replace(results, "\x1b", "^[", -1)
-		fmt.Println("DETECT:", r, err, output)
+		log.Println("DETECT:", r, err, output)
 	} else {
 	} else {
 		// local telnet echos the reply  :()
 		// local telnet echos the reply  :()
-		fmt.Println("DETECT: Nothing received.")
+		log.Println("DETECT: Nothing received.")
 		return
 		return
 	}
 	}
 
 
@@ -249,7 +251,7 @@ func (d *Door) detect() {
 					width := results[:pos]
 					width := results[:pos]
 
 
 					Width, err = strconv.Atoi(width)
 					Width, err = strconv.Atoi(width)
-					fmt.Printf("Width: %s, %d, %v\n", results, Width, err)
+					log.Printf("Width: %s, %d, %v\n", results, Width, err)
 				}
 				}
 			} else {
 			} else {
 				Height = 0
 				Height = 0
@@ -257,26 +259,39 @@ func (d *Door) detect() {
 			}
 			}
 		}
 		}
 	}
 	}
-	fmt.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
+	log.Printf("Unicode %v Screen: %d X %d\n", Unicode, Width, Height)
 }
 }
 
 
 // Initialize door framework.  Parse commandline, read dropfile,
 // Initialize door framework.  Parse commandline, read dropfile,
 // detect terminal capabilities.
 // detect terminal capabilities.
-func (d *Door) Init() {
+func (d *Door) Init(doorname string) {
 	var dropfile string
 	var dropfile string
 	d.Pushback = NewFIFOBuffer(5)
 	d.Pushback = NewFIFOBuffer(5)
 
 
+	// Get path to binary, and chdir to it.
+	binaryPath, _ := os.Executable()
+	binaryPath = filepath.Dir(binaryPath)
+	_ = os.Chdir(binaryPath)
+
 	flag.StringVar(&dropfile, "d", "", "Path to dropfile")
 	flag.StringVar(&dropfile, "d", "", "Path to dropfile")
 	flag.Parse()
 	flag.Parse()
 	if len(dropfile) == 0 {
 	if len(dropfile) == 0 {
 		flag.PrintDefaults()
 		flag.PrintDefaults()
 		os.Exit(2)
 		os.Exit(2)
 	}
 	}
-	fmt.Printf("Loading: %s\n", dropfile)
 
 
 	d.ReadDropfile(dropfile)
 	d.ReadDropfile(dropfile)
+	logfilename := fmt.Sprintf("%s-%d.log", doorname, d.Config.Node)
+	logf, err := os.OpenFile(logfilename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
+	if err != nil {
+		panic(fmt.Sprintf("Error creating log file %s: %v", logfilename, err))
+	}
+
+	log.SetOutput(logf)
+	//= log.New(logf, fmt.Sprintf("%s-%d", doorname, d.Config.Node), log.Ldate|log.Ltime|log.Lshortfile)
+	log.Printf("Loading dropfile %s\n", dropfile)
 
 
-	fmt.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
+	log.Printf("BBS %s, User %s / Handle %s / File %d\n", d.Config.BBSID, d.Config.Real_name, d.Config.Handle, d.Config.Comm_handle)
 	d.detect()
 	d.detect()
 	if Unicode {
 	if Unicode {
 		BOXES = BOXES_UNICODE
 		BOXES = BOXES_UNICODE
@@ -295,12 +310,12 @@ func (d *Door) Write(output string) {
 	buffer := []byte(output)
 	buffer := []byte(output)
 	n, err := syscall.Write(d.WRITEFD, buffer)
 	n, err := syscall.Write(d.WRITEFD, buffer)
 	if err != nil {
 	if err != nil {
-		fmt.Println("Write error/HANGUP?", n)
+		log.Println("Write error/HANGUP?", n)
 		d.Disconnected = true
 		d.Disconnected = true
 	}
 	}
 	// No, this isn't it.  The # of bytes in buffer == bytes written.
 	// No, this isn't it.  The # of bytes in buffer == bytes written.
 	if n != len(buffer) {
 	if n != len(buffer) {
-		fmt.Printf("Write fail: %d != %d\n", len(buffer), n)
+		log.Printf("Write fail: %d != %d\n", len(buffer), n)
 	}
 	}
 }
 }
 
 

+ 9 - 9
door/input.go

@@ -1,7 +1,7 @@
 package door
 package door
 
 
 import (
 import (
-	"fmt"
+	"log"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
 	"syscall"
 	"syscall"
@@ -101,7 +101,7 @@ func (d *Door) getch() int {
 	r, err := syscall.Read(d.READFD, buffer)
 	r, err := syscall.Read(d.READFD, buffer)
 	if r != 1 {
 	if r != 1 {
 		// I'm getting write error here... (when disconnected)
 		// I'm getting write error here... (when disconnected)
-		fmt.Printf("Read said ready, but didn't read a character %d %v.\n", r, err)
+		log.Printf("Read said ready, but didn't read a character %d %v.\n", r, err)
 		// hangup
 		// hangup
 		d.Disconnected = true
 		d.Disconnected = true
 		return -2
 		return -2
@@ -116,7 +116,7 @@ func (d *Door) getkey_or_pushback() int {
 
 
 	if false {
 	if false {
 		key := d.getch()
 		key := d.getch()
-		fmt.Printf("%d / %X\n", key, key)
+		log.Printf("%d / %X\n", key, key)
 		return key
 		return key
 	} else {
 	} else {
 		return d.getch()
 		return d.getch()
@@ -151,7 +151,7 @@ func (d *Door) GetKey() int {
 			if c2 != 0x00 && c2 != 0x0a {
 			if c2 != 0x00 && c2 != 0x0a {
 				// wasn't 0x00 or 0x0a
 				// wasn't 0x00 or 0x0a
 				d.Pushback.Push(c2)
 				d.Pushback.Push(c2)
-				// fmt.Printf("Push 0x0d trailer %d / %x\n", c2, c2)
+				// log.Printf("Push 0x0d trailer %d / %x\n", c2, c2)
 			}
 			}
 		}
 		}
 		return c
 		return c
@@ -217,7 +217,7 @@ func (d *Door) GetKey() int {
 		case 0x53:
 		case 0x53:
 			return XKEY_DELETE
 			return XKEY_DELETE
 		default:
 		default:
-			fmt.Printf("ERROR Doorway mode: 0x00 %x\n", c2)
+			log.Printf("ERROR Doorway mode: 0x00 %x\n", c2)
 			return XKEY_UNKNOWN
 			return XKEY_UNKNOWN
 		}
 		}
 	}
 	}
@@ -264,7 +264,7 @@ func (d *Door) GetKey() int {
 			return XKEY_INSERT
 			return XKEY_INSERT
 		case "[1":
 		case "[1":
 			// Syncterm is lost, could be F1..F5?
 			// Syncterm is lost, could be F1..F5?
-			fmt.Printf("ERROR (Syncterm) Extended %#v\n", extended)
+			log.Printf("ERROR (Syncterm) Extended %#v\n", extended)
 			return XKEY_UNKNOWN
 			return XKEY_UNKNOWN
 		case "[2~":
 		case "[2~":
 			return XKEY_INSERT // terminal
 			return XKEY_INSERT // terminal
@@ -301,7 +301,7 @@ func (d *Door) GetKey() int {
 		case "Ot":
 		case "Ot":
 			return XKEY_F5 // syncterm
 			return XKEY_F5 // syncterm
 		default:
 		default:
-			fmt.Printf("ERROR Extended %#v\n", extended)
+			log.Printf("ERROR Extended %#v\n", extended)
 			return XKEY_UNKNOWN
 			return XKEY_UNKNOWN
 		}
 		}
 
 
@@ -336,7 +336,7 @@ retry:
 		errno, ok := err.(syscall.Errno)
 		errno, ok := err.(syscall.Errno)
 		if ok && errno == syscall.EINTR {
 		if ok && errno == syscall.EINTR {
 			// Should I retry my syscall.Select here?  Yes! -1 is not return value.
 			// Should I retry my syscall.Select here?  Yes! -1 is not return value.
-			fmt.Printf("WaitKey: EINTR\n")
+			log.Printf("WaitKey: EINTR\n")
 			goto retry
 			goto retry
 			// return -1
 			// return -1
 		}
 		}
@@ -348,7 +348,7 @@ retry:
 			This seemed to happen when I called WaitKey many times with usecs.
 			This seemed to happen when I called WaitKey many times with usecs.
 		*/
 		*/
 
 
-		fmt.Println("-1 : ", err)
+		log.Println("-1 : ", err)
 		// hangup ?!
 		// hangup ?!
 		return -2
 		return -2
 	}
 	}

+ 6 - 1
door/input_test.go

@@ -43,11 +43,16 @@ func TestDoorInputConnection(t *testing.T) {
 	tmpFile.Close()
 	tmpFile.Close()
 
 
 	d := Door{}
 	d := Door{}
+
+	// If I call d.Init() more then once flag complains about flag redefined.
 	// Reset flags
 	// Reset flags
 	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
 	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
 	// preset commandline args so door can init
 	// preset commandline args so door can init
 	os.Args = []string{"door", "-d", tmpFile.Name()}
 	os.Args = []string{"door", "-d", tmpFile.Name()}
-	d.Init()
+	d.Init("input-test")
+
+	// clean up logfile
+	defer os.Remove("input-test-12.log")
 
 
 	// Ok!
 	// Ok!
 	if !Unicode {
 	if !Unicode {

+ 65 - 2
door/menu_test.go

@@ -6,6 +6,7 @@ import (
 	"io/ioutil"
 	"io/ioutil"
 	"net"
 	"net"
 	"os"
 	"os"
+	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
 )
 )
@@ -50,7 +51,10 @@ func TestMenuConnection(t *testing.T) {
 	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
 	flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
 	// preset commandline args so door can init
 	// preset commandline args so door can init
 	os.Args = []string{"door", "-d", tmpFile.Name()}
 	os.Args = []string{"door", "-d", tmpFile.Name()}
-	d.Init()
+	d.Init("menu-test")
+
+	// clean up log file
+	defer os.Remove("menu-test-12.log")
 
 
 	// Ok!
 	// Ok!
 	if !Unicode {
 	if !Unicode {
@@ -66,12 +70,71 @@ func TestMenuConnection(t *testing.T) {
 	}
 	}
 
 
 	// These are the commands sent to detect ... throw this all away.
 	// These are the commands sent to detect ... throw this all away.
-	buffer = make([]byte, 128)
+	buffer = make([]byte, 1024)
 	server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
 	server.SetReadDeadline(time.Now().Add(time.Millisecond * 20))
 	_, err = server.Read(buffer)
 	_, err = server.Read(buffer)
 	// t.Errorf("Buffer : %#v\n", buffer[:r])
 	// t.Errorf("Buffer : %#v\n", buffer[:r])
 	server.SetReadDeadline(time.Time{})
 	server.SetReadDeadline(time.Time{})
 
 
 	// Ready to test!
 	// Ready to test!
+	m := Menu{Panel: Panel{Width: 10,
+		X:     2,
+		Y:     2,
+		Style: DOUBLE,
+	}}
+
+	// Use simple renders for testing
+	m.SelectedR = func(text string) string {
+		return ColorText("BLACK ON WHITE") + text
+	}
+	m.UnselectedR = func(text string) string {
+		return ColorText("WHI ON BLA") + text
+	}
+
+	m.AddSelection("A", "ART")
+	m.AddSelection("B", "BOO")
+	m.AddSelection("C", "Cat")
+
+	var choice int
+
+	// preload the key buffer with commands
+	// BUG:  I can't use \x1b[A because of the way extended codes work there.
+	keys := "\x00\x50\x00\x50\r" // down, down, ENTER
+	server.Write([]byte(keys))
+
+	choice = m.Choose(&d)
+
+	if choice != 3 {
+		t.Errorf("Expected menu option C, got %d / %c", choice, m.GetOption(choice))
+	}
+
+	// Read the display output
+	server.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
+	r, err := server.Read(buffer)
+	server.SetReadDeadline(time.Time{})
+	output := string(buffer[:r])
+	// t.Errorf("OUTPUT: (len %d) %#v", r, output)
+
+	parts := strings.SplitAfter(output, "H")
+	for _, part := range parts {
+		t.Logf("Got: %#v", part)
+	}
+
+	m.Chosen = 1
+
+	keys = "\x00\x48\r" // up, ENTER
+	server.Write([]byte(keys))
+
+	choice = m.Choose(&d)
+
+	if choice != 1 {
+		t.Errorf("Expected menu option A, got %d / %c", choice, m.GetOption(choice))
+	}
+
+	// Read the display output
+	server.SetReadDeadline(time.Now().Add(time.Millisecond * 200))
+	r, err = server.Read(buffer)
+	server.SetReadDeadline(time.Time{})
+	output = string(buffer[:r])
 
 
 }
 }

+ 2 - 0
door/panel.go

@@ -105,6 +105,8 @@ func (p *Panel) UpdateLine(index int) string {
 	var output string
 	var output string
 	var style int = int(p.Style)
 	var style int = int(p.Style)
 
 
+	p.Lines[index].Update()
+
 	row := p.Y + index
 	row := p.Y + index
 	col := p.X
 	col := p.X
 	if style > 0 {
 	if style > 0 {