Example #1
0
// write handler of logger.
func (l *clogger) Write(p []byte) (n int, err error) {
	mutex.Lock()
	defer mutex.Unlock()
	for _, line := range strings.Split(string(p), "\n") {
		if len(line) > 0 && line[len(line)-1] == '\n' {
			line = line[0 : len(line)-2]
		}
		if line != "" {
			log.Printf("[%s] %s", term.Color(l.proc, colors[l.idx]), term.Color(line, colors[l.idx]))
		}
	}
	n = len(p)
	return
}
Example #2
0
File: errors.go Project: mewmew/uc
// Error returns an error string with position information.
//
// The error format is as follows.
//
//    (file:line:column): error: text
func (e *Error) Error() string {
	// Use colors.
	pos := fmt.Sprintf("(byte offset %d)", e.Pos)
	prefix := "error:"
	text := e.Text
	if UseColor {
		pos = term.Color(pos, term.Bold)
		prefix = term.RedBold(prefix)
		text = term.Color(text, term.Bold)
	}
	src := e.Src
	if src == nil {
		// If Src is nil, the error format is as follows.
		//
		//    (byte offset %d) error: text
		return fmt.Sprintf("%s %s %s", pos, prefix, text)
	}
	// The error format is as follows.
	//
	//    (file:line) error: text
	//       1 = y
	//         ^
	line, col := src.Position(e.Pos)
	end := len(src.Input)
	if len(src.Lines) > line {
		end = src.Lines[line]
	}
	srcLine := src.Input[src.Lines[line-1]:end]
	srcLine = strings.Replace(srcLine, "\t", " ", -1)
	srcLine = strings.TrimRight(srcLine, "\n\r")
	arrow := fmt.Sprintf("%*s", col, "^")
	pos = fmt.Sprintf("(%s:%d)", src.Path, line)
	if UseColor {
		pos = term.Color(pos, term.Bold)
		arrow = term.Color(arrow, term.Bold)
	}
	return fmt.Sprintf("%s %s %s\n%s\n%s", pos, prefix, text, srcLine, arrow)
}