Example #1
0
func printColor(s severity) {
	switch s {
	case infoLog:
		ct.Foreground(ct.Cyan, false)
	case warningLog:
		ct.Foreground(ct.Yellow, false)
	case errorLog:
		ct.Foreground(ct.Red, true)
	case fatalLog:
		ct.Foreground(ct.Red, false)
	}
}
Example #2
0
func (tokens LogTokens) Print() {
	defer color.ResetColor()

	var defColor color.Color = color.White
	var defBright bool = true

	for _, token := range tokens {
		if token.Type >= TOKEN_DEBUG {
			defColor, defBright, _ = token.Type.getFgColor()
		}
	}

	for _, token := range tokens {
		fg, bright, specific := token.Type.getFgColor()
		if !specific {
			fg = defColor
			bright = defBright
		}

		color.Foreground(fg, bright)
		fmt.Print(token.Text)
	}

	fmt.Print("\n")
}
Example #3
0
func print(timeFormat string, le *logEntry) {
	var level string
	switch le.level {
	case Info:
		level = "INFO    "
		ct.Foreground(ct.Cyan, false)
	case Warning:
		level = "WARNING "
		ct.Foreground(ct.Yellow, false)
	case Error:
		level = "ERROR   "
		ct.Foreground(ct.Red, false)
	case Critical:
		level = "CRITICAL"
		ct.ChangeColor(ct.Black, false, ct.Red, false)
	case Fatal:
		level = "FATAL   "
		ct.ResetColor()
	}
	fmt.Println(le.time.Format(timeFormat), level, le.message)
	ct.ResetColor()
}
Example #4
0
func (cl *coloredLogger) ScenarioStart(scenarioHeading string) {
	msg := formatScenario(scenarioHeading)
	Log.Info(msg)

	indentedText := indent(msg, scenarioIndentation)
	if level == logging.INFO {
		cl.headingText.WriteString(indentedText + spaces(4))
		cl.writeToConsole(cl.headingText.String(), ct.None, false)
	} else {
		ct.Foreground(ct.Yellow, false)
		ConsoleWrite(indentedText)
		ct.ResetColor()
	}
}
Example #5
0
// outputColorStack is use when -logcolor is enabled.
// colored stack printing is allowed to be inefficient because it's a local development feature.
func outputColorStack(stacks []byte, s severity) {
	r := bytes.NewReader(stacks)
	scanner := bufio.NewScanner(r)
	for scanner.Scan() {
		l := scanner.Bytes()
		if len(l) > 0 && l[0] == "\t"[0] {
			addroffset := bytes.LastIndex(l, []byte(" "))
			linumoffset := bytes.LastIndex(l, []byte(":"))
			if addroffset == -1 || linumoffset == -1 {
				os.Stderr.Write(l)
				goto eol
			}
			srcpath := l[0:linumoffset]
			hlstart := 0
			hlstacksrc.Lock()
			if hlstacksrc.v != nil {
				for _, v := range hlstacksrc.v {
					offs := bytes.Index(srcpath, v)
					if offs > hlstart {
						hlstart = offs
					}
				}
			}
			hlstacksrc.Unlock()
			if hlstart > 0 {
				os.Stderr.Write(srcpath[:hlstart])
				ct.Foreground(ct.Blue, true)
				os.Stderr.Write(srcpath[hlstart:])
				ct.ResetColor()
			} else {
				os.Stderr.Write(srcpath)
			}
			os.Stderr.WriteString(":")
			printColor(s)
			os.Stderr.Write(l[linumoffset+1 : addroffset])
			ct.ResetColor()
			os.Stderr.Write(l[addroffset:])
		} else {
			os.Stderr.Write(l)
		}
	eol:
		os.Stderr.WriteString("\n")
	}
	if err := scanner.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}
}
Example #6
0
func Error(format string, a ...interface{}) {
	defer color.ResetColor()

	color.Foreground(color.Red, true)
	fmt.Println(fmt.Sprintf(format, a...))
}
Example #7
0
func (c *coloredConsole) displayMessage(msg string, color ct.Color) {
	ct.Foreground(color, false)
	defer ct.ResetColor()
	fmt.Fprint(c.writer, msg)
	c.writer.Print()
}
Example #8
0
// output writes the data to the log files and releases the buffer.
func (l *loggingT) output(s severity, buf *buffer, file string, line int, alsoToStderr bool) {
	l.mu.Lock()
	if l.traceLocation.isSet() {
		if l.traceLocation.match(file, line) {
			buf.Write(stacks(false))
		}
	}
	data := buf.Bytes()
	if !flag.Parsed() {
		os.Stderr.Write([]byte("ERROR: logging before flag.Parse: "))
		os.Stderr.Write(data)
	} else {
		if l.toMemory {
			logToMemory(data)
		}
		if alsoToStderr || l.toStderr || s >= l.stderrThreshold.get() {
			if !l.color {
				os.Stderr.Write(data)
			} else {
				// color printing is allowed to be inefficient.
				printColor(s)
				os.Stderr.Write(data[0:14])
				ct.ResetColor()
				os.Stderr.Write(data[14:30])
				ct.Foreground(ct.Blue, true)
				n, _ := os.Stderr.Write([]byte(file))
				ct.ResetColor()
				os.Stderr.WriteString(":")
				printColor(s)
				rest := data[30+n+1:]
				end := bytes.IndexAny(rest, "]")
				os.Stderr.Write(rest[:end])
				ct.Foreground(ct.Blue, true)
				os.Stderr.WriteString("]")
				ct.ResetColor()
				if l.traceLocation.isSet() && l.traceLocation.match(file, line) {
					outputColorStack(rest[end+1:], s)
				} else {
					os.Stderr.Write(rest[end+1:])
				}
			}
		}
		if l.toFile {
			if l.file[s] == nil {
				if err := l.createFiles(s); err != nil {
					os.Stderr.Write(data) // Make sure the message appears somewhere.
					l.exit(err)
				}
			}
			switch s {
			case fatalLog:
				l.file[fatalLog].Write(data)
				fallthrough
			case errorLog:
				l.file[errorLog].Write(data)
				fallthrough
			case warningLog:
				l.file[warningLog].Write(data)
				fallthrough
			case infoLog:
				l.file[infoLog].Write(data)
			}
		}

	}
	if s == fatalLog {
		// If we got here via Exit rather than Fatal, print no stacks.
		if atomic.LoadUint32(&fatalNoStacks) > 0 {
			l.mu.Unlock()
			timeoutFlush(10 * time.Second)
			os.Exit(1)
		}
		// Dump all goroutine stacks before exiting.
		// First, make sure we see the trace for the current goroutine on standard error.
		// If -logtostderr has been specified, the loop below will do that anyway
		// as the first stack in the full dump.
		if !l.toStderr {
			trace := stacks(false)
			if l.color {
				outputColorStack(trace, s)
			} else {
				os.Stderr.Write(trace)
			}

		}
		// Write the stack trace for all goroutines to the files.
		trace := stacks(true)

		if l.toStderr {
			if l.color {
				outputColorStack(trace, s)
			} else {
				os.Stderr.Write(trace)
			}
		}

		logExitFunc = func(error) {} // If we get a write error, we'll still exit below.
		for log := fatalLog; log >= infoLog; log-- {
			if f := l.file[log]; f != nil { // Can be nil if -logtostderr is set.
				f.Write(trace)
			}
		}
		l.mu.Unlock()
		timeoutFlush(10 * time.Second)
		os.Exit(255) // C++ uses -1, which is silly because it's anded with 255 anyway.
	}
	l.putBuffer(buf)
	l.mu.Unlock()
	if stats := severityStats[s]; stats != nil {
		atomic.AddInt64(&stats.lines, 1)
		atomic.AddInt64(&stats.bytes, int64(len(data)))
	}
}
Example #9
0
func (cl *coloredLogger) writeToConsole(text string, color ct.Color, isBright bool) {
	ct.Foreground(color, isBright)
	fmt.Print(text)
	ct.ResetColor()
}
Example #10
0
func (cl *coloredLogger) print(text string, color ct.Color, isBright bool) {
	ct.Foreground(color, isBright)
	fmt.Fprint(cl.writer, text)
	cl.writer.Print()
	ct.ResetColor()
}
Example #11
0
// Changes the color for the messages to green for success.
func successLabel(message string) {
	ct.Foreground(ct.Green, true)
	fmt.Print(message)
	ct.ResetColor()
	fmt.Println("")
}