Permalink
Browse files

Merge pull request #4 from gnanderson/openSUSE

Gofixes and changes to build against current weekly/tip using new go install tool
  • Loading branch information...
2 parents 195cb2d + b74aa75 commit 0ed5c21fcd8b90c511722e3965827e19d4b13092 @StepLg committed Feb 2, 2012
View
@@ -0,0 +1,66 @@
+package erx
+
+import (
+ "fmt"
+ "io"
+ "strconv"
+
+ "strings"
+)
+
+func FormatConsole(w io.Writer, err Error, tab string) {
+ formatConsole_gen(w, err, tab, 0)
+}
+
+func formatConsole_gen(w io.Writer, err Error, tab string, level int) {
+ w.Write([]uint8(strings.Repeat(tab, level) + transformPath(err.File()) + ":"))
+ w.Write([]uint8(strconv.Itoa(err.Line()) + " " + err.Message() + "\n"))
+ funcFile, funcLine := err.Func().FileLine(err.Func().Entry())
+ w.Write([]uint8(strings.Repeat(tab, level) + transformPath(funcFile) + ":"))
+ w.Write([]uint8(strconv.Itoa(funcLine) + " " + err.Func().Name() + "\n"))
+ level++
+ if len(err.Variables()) > 0 {
+ w.Write([]uint8(strings.Repeat(tab, level) + "Scope variables:\n"))
+ for name, val := range err.Variables() {
+ w.Write([]uint8(strings.Repeat(tab, level+1) + name + "\t: "))
+ switch i := val.(type) {
+ case string:
+ w.Write([]uint8(i))
+ case fmt.Stringer:
+ w.Write([]uint8(i.String()))
+ default:
+ w.Write([]uint8(fmt.Sprint(i)))
+ }
+ w.Write([]uint8("\n"))
+ }
+ }
+ subErrs := err.Errors()
+ if len(subErrs) > 0 {
+ w.Write([]uint8(strings.Repeat(tab, level)))
+ w.Write([]uint8("Scope errors:\n"))
+ for _, subErr := range subErrs {
+ switch i := subErr.(type) {
+ case Error:
+ formatConsole_gen(w, i, tab, level+1)
+ case error:
+ w.Write([]uint8(strings.Repeat(tab, level+1)))
+ w.Write([]uint8(i.Error() + "\n"))
+ default:
+ w.Write([]uint8("???\n"))
+ }
+ }
+ }
+}
+
+// Cut from path first dirs
+func transformPath(path string) string {
+ // finding path in pathCuts to cut
+ for curPath := pathCuts.Front(); curPath != nil; curPath = curPath.Next() {
+ if pathStr, isString := curPath.Value.(string); isString {
+ if len(pathStr) <= len(path) && path[0:len(pathStr)] == pathStr {
+ return path[len(pathStr):]
+ }
+ }
+ }
+ return path
+}
View
@@ -0,0 +1,87 @@
+package erx
+
+import (
+ "fmt"
+ "io"
+ "strconv"
+
+ "encoding/xml"
+ "strings"
+)
+
+/**
+ * err -- erx error
+ * indent -- flag to add indentation to result xml file
+ */
+func FormatSimpleXML(w io.Writer, err Error, indent bool) {
+ formatSimpleXML_gen(w, err, indent, 0)
+}
+
+func escape(w io.Writer, str string) {
+ xml.Escape(w, []uint8(str))
+}
+
+/**
+ *This function create XML format of error
+ *return string
+ */
+func formatSimpleXML_gen(w io.Writer, err Error, indent bool, level int) {
+ tab := ""
+ hyphen := ""
+ if indent {
+ tab = "\t"
+ hyphen = "\n"
+ }
+ if level == 0 {
+ w.Write([]uint8("<?xml version=\"1.0\" encoding=\"UTF-8\">" + hyphen))
+ }
+ w.Write([]uint8(strings.Repeat(tab, level) + "<error>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<message>"))
+ escape(w, err.Message())
+ w.Write([]uint8("</message>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<file>"))
+ escape(w, transformPath(err.File()))
+ w.Write([]uint8("</file>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<line>"))
+ escape(w, strconv.Itoa(err.Line()))
+ w.Write([]uint8("</line>" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "<variables>" + hyphen))
+ if len(err.Variables()) > 0 {
+ for name, val := range err.Variables() {
+ w.Write([]uint8(strings.Repeat(tab, level+2) + "<variable name=\""))
+ escape(w, name)
+ w.Write([]uint8("\">"))
+ switch i := val.(type) {
+ case string:
+ escape(w, i)
+ case fmt.Stringer:
+ escape(w, i.String())
+ default:
+ escape(w, fmt.Sprint(i))
+ }
+ w.Write([]uint8("</variable>" + hyphen))
+ }
+ }
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "</variables>" + hyphen))
+ subErrs := err.Errors()
+ if len(subErrs) > 0 {
+ w.Write([]uint8(strings.Repeat(tab, level)))
+ for _, subErr := range subErrs {
+ switch i := subErr.(type) {
+ case Error:
+ formatSimpleXML_gen(w, i, indent, level+1)
+ w.Write([]uint8(hyphen))
+ case error:
+ w.Write([]uint8(strings.Repeat(tab, level) + "<error type=\"2\">" + hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+2)))
+ escape(w, i.Error())
+ w.Write([]uint8(hyphen))
+ w.Write([]uint8(strings.Repeat(tab, level+1) + "</error>" + hyphen))
+ default:
+ w.Write([]uint8("???" + hyphen))
+ }
+ }
+ }
+
+ w.Write([]uint8(strings.Repeat(tab, level) + "</error>"))
+}
@@ -3,6 +3,7 @@ package erx
import (
"container/list"
"runtime"
+ "io"
)
@@ -17,9 +18,9 @@ func AddPathCut(path string) {
type Error interface {
Message() string
- Errors() *list.List
+ Errors() []interface{}
Variables() ErrorVariables
-
+
File() string
Line() int
Func() *runtime.Func
@@ -30,25 +31,25 @@ type Error interface {
type error_realization struct {
message string
-
+
file string
line int
- errors *list.List
+ errors *list.List
variables ErrorVariables
-
+
funcInfo *runtime.Func
}
func newErrorInitializer(level int) *error_realization {
- err := error_realization{"", "", 0, list.New(), make(map[string] interface{}), nil}
+ err := error_realization{"", "", 0, list.New(), make(map[string]interface{}), nil}
pc, file, line, ok := runtime.Caller(level)
if ok {
err.file, err.line = file, line
} else {
err.file, err.line = "???", 666
}
-
+
err.funcInfo = runtime.FuncForPC(pc)
return &err
}
@@ -66,15 +67,24 @@ func NewSequent(msg string, error interface{}) Error {
return Error(err)
}
+func AutoOutput(w io.Writer, outputType string, err Error) {
+ switch outputType {
+ case "XML":
+ FormatSimpleXML(w, err, true)
+ default:
+ FormatConsole(w, err, "\t")
+ }
+}
+
func NewSequentLevel(msg string, error interface{}, level int) Error {
- err := newErrorInitializer(level+2)
+ err := newErrorInitializer(level + 2)
err.message = msg
err.AddE(error)
return Error(err)
}
func (e *error_realization) Message() string {
- return e.message;
+ return e.message
}
func (e *error_realization) File() string {
@@ -89,8 +99,18 @@ func (e *error_realization) Func() *runtime.Func {
return e.funcInfo
}
-func (e *error_realization) Errors() *list.List {
- return e.errors
+func (e *error_realization) Errors() []interface{} {
+ len := 0
+ for item := e.errors.Front(); item != nil; item = item.Next() {
+ len += 1
+ }
+ res := make([]interface{}, len)
+ id := 0
+ for item := e.errors.Front(); item != nil; item = item.Next() {
+ res[id] = item.Value
+ id += 1
+ }
+ return res
}
func (e *error_realization) Variables() ErrorVariables {
View
@@ -1,6 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=panic
-GOFILES=panic.go
-
-include $(GOROOT)/src/Make.cmd
View
@@ -4,78 +4,89 @@ import (
"strings"
"strconv"
"os"
+ "io"
"bufio"
"fmt"
"runtime"
- "../../src/erx/_obj/erx"
+ "github.com/StepLg/go-erx/erx"
+ "flag"
)
func init() {
// add path prefix to erx cut list
_, file, _, _ := runtime.Caller(0)
index := strings.LastIndex(file, "/")
- if index!= -1 {
- dirName := file[0:strings.LastIndex(file, "/")+1]
+ if index != -1 {
+ dirName := file[0 : strings.LastIndex(file, "/")+1]
fmt.Println(dirName)
erx.AddPathCut(dirName)
}
+
}
func fileSum(fileName string) (result int) {
- makeError := func(err interface{}) (res erx.Error) {
- res = erx.NewSequentLevel("Sum integers from file.", err, 1)
- res.AddV("file name", fileName)
- return
- }
+ defer func() {
+ if err := recover(); err != nil {
+ res := erx.NewSequent("Sum integers from file.", err)
+ res.AddV("file name", fileName)
+ panic(res)
+ }
+ }()
result = 0
- f, err := os.OpenFile(fileName, os.O_RDONLY, 0000)
- if f==nil {
- panic(makeError(erx.NewSequent("Open file.", err)))
+ f, err := os.Open(fileName)
+ if f == nil {
+ panic(erx.NewSequent("Open file.", err))
}
-
+
reader := bufio.NewReader(f)
-
+
line, err := reader.ReadString('\n')
lineNum := 1
- for err==nil || err==os.EOF {
- if strings.TrimSpace(line)!="" {
+ for err == nil || err == io.EOF {
+ if strings.TrimSpace(line) != "" {
chunkNum := 1
for _, chunk := range strings.Split(line, " ") {
var curInt int
curInt, err = strconv.Atoi(strings.TrimSpace(chunk))
- if err!=nil {
+ if err != nil {
errErx := erx.NewSequent("Convert string to integer.", err)
errErx.AddV("chunk", chunk)
errErx.AddV("line num", lineNum)
errErx.AddV("chunk num", chunkNum)
- panic(makeError(errErx))
+ panic(errErx)
}
result += curInt
}
- }
+ }
- if err==os.EOF {
+ if err == io.EOF {
break
}
line, err = reader.ReadString('\n')
lineNum++
}
-
- if err!=os.EOF {
- panic(makeError(erx.NewSequent("Reading from file.", err)))
+
+ if err != io.EOF {
+ panic(erx.NewSequent("Reading from file.", err))
}
return
}
+func flagMode_get() string {
+ var mode *string = flag.String("mode", "console", "mode out: XML, console")
+ flag.Parse()
+ return *mode
+}
+
func main() {
defer func() {
if err := recover(); err != nil {
if errErx, ok := err.(erx.Error); ok {
- formatter := erx.NewStringFormatter(" ")
- fmt.Println(formatter.Format(errErx))
+ erx.AutoOutput(os.Stdout, flagMode_get(), errErx)
}
}
}()
+
fileSum("ints.txt")
}
View
@@ -1,6 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=simple
-GOFILES=simple.go
-
-include $(GOROOT)/src/Make.cmd
Oops, something went wrong.

0 comments on commit 0ed5c21

Please sign in to comment.