func (f *Formatter) sprintfColor(c *color.Color, format string, args ...interface{}) string { if f.DisabledColor || c == nil { return fmt.Sprintf(format, args...) } else { return c.SprintfFunc()(format, args...) } }
// returns a function that will format and writes the line extracted from the logs of a given container func write(prefix string, color *ansi.Color, timestamps bool) func(dest io.Writer, token []byte) (n int, err error) { return func(dest io.Writer, token []byte) (n int, err error) { countingWriter := countingWriter{Writer: dest} if color != nil { ansi.Output = &countingWriter color.Set() } _, err = countingWriter.Write([]byte(prefix)) if err == nil { if !timestamps { // timestamps are always present in the incoming stream for // sorting purposes, so we strip them if the user didn't ask // for them const timestampPrefixLength = 31 strip := timestampPrefixLength if string(token[0]) == "[" { // it seems that timestamps are wrapped in [] for events // streamed in real time during a `docker logs -f` strip = strip + 2 } token = token[strip:] } _, err = countingWriter.Write(token) } if err == nil { if color != nil { ansi.Unset() } _, err = dest.Write([]byte("\n")) } return countingWriter.written, err } }
// Print the codeword contents and type to terminal. For debugging. func (c *Codeword) Print() { payload := "" var color *color.Color = blue switch c.Type { case CodewordTypeAddress: payload = c.Adress() color = red case CodewordTypeMessage: payload = "" color = green default: color = blue } parity := utils.TernaryStr(c.ValidParity, "", "*") color.Printf("%s %s %s ", c.Type, payload, parity) corr := c.BitCorrections if corr > 0 { color.Printf("%d bits corrected", corr) } println("") }
func (l *Log) format(timestamp bool, level int, format string, args []interface{}) string { ts := "" if timestamp { f := l.Palette.Timestamp.SprintfFunc() ts = f( "%s", time.Now().Format(l.TimeFmt), ) } var p *color.Color switch level { case say: p = l.Palette.Say case notice: p = l.Palette.Notice case warn: p = l.Palette.Warn case shout: p = l.Palette.Shout case header: p = l.Palette.Header default: panic("unknown log level") } return ts + p.SprintfFunc()(format, args...) }
func printDatagram(out *color.Color, d *protocol.Datagram) { re := regexp.MustCompilePOSIX("^00000") out.Printf("FROM: %s\n", d.From) out.Printf("SIZE: %d\n", len(d.Data)) data := strings.TrimLeft(hex.Dump(d.Data), "0") out.Printf("DATA: 000" + re.ReplaceAllString(data, " ")) color.Set(color.Bold) }
func (s *RangeHistogram) ResumeHistogram(out *color.Color) { out.Printf("\n %s:", s.name_) sortedHisto := make([]int, len(s.data_)) minKey := 999999999 maxKey := 0 for key, _ := range s.data_ { if key < minKey { minKey = key } if key > maxKey { maxKey = key } } for key, val := range s.data_ { sortedHisto[key-minKey] = val } for k, v := range sortedHisto { out.Printf("\n [%d]: %d", k, v) } }
func (b *Buntstift) printf(Color *color.Color, format string, a ...interface{}) (n int, err error) { Color.Set() defer b.unsetColor() return fmt.Fprintf(Output, format, a...) }
// Print a thick line break (of '=' characters). func boldLine(c *color.Color) { c.Set() defer colorUnset() lineBreak("=") }
// Print a thin line break (of '-' characters). func line(c *color.Color) { c.Set() defer colorUnset() lineBreak("-") }
func Render(dst io.Writer, src eventstream.EventStream) int { var buildConfig event.TaskConfig exitStatus := 0 for { ev, err := src.NextEvent() if err != nil { if err == io.EOF { return exitStatus } else { fmt.Fprintf(dst, "failed to parse next event: %s\n", err) return 255 } } switch e := ev.(type) { case event.Log: fmt.Fprintf(dst, "%s", e.Payload) case event.InitializeTask: buildConfig = e.TaskConfig if buildConfig.Image != "" { fmt.Fprintf(dst, "\x1b[1minitializing with %s\x1b[0m\n", buildConfig.Image) } else { fmt.Fprintf(dst, "\x1b[1minitializing\x1b[0m\n") } case event.StartTask: argv := strings.Join(append([]string{buildConfig.Run.Path}, buildConfig.Run.Args...), " ") fmt.Fprintf(dst, "\x1b[1mrunning %s\x1b[0m\n", argv) case event.FinishTask: exitStatus = e.ExitStatus case event.Error: errCol := ui.ErroredColor.SprintFunc() fmt.Fprintf(dst, "%s\n", errCol(e.Message)) case event.Status: var printColor *color.Color switch e.Status { case "started": continue case "succeeded": printColor = ui.SucceededColor case "failed": printColor = ui.FailedColor if exitStatus == 0 { exitStatus = 1 } case "errored": printColor = ui.ErroredColor if exitStatus == 0 { exitStatus = 2 } case "aborted": printColor = ui.AbortedColor if exitStatus == 0 { exitStatus = 3 } default: fmt.Fprintf(dst, "unknown status: %s", e.Status) return 255 } printColorFunc := printColor.SprintFunc() fmt.Fprintf(dst, "%s\n", printColorFunc(e.Status)) return exitStatus } } return 255 }
func (s *ScalarStatistic) Resume(out *color.Color) { out.Printf("\n %s: %d", s.name_, s.total_) }
func (s *RangeStatistic) ResumeRangeAv(out *color.Color) { out.Printf("\n %s: min=%d, max=%d av=%f", s.name_, s.min_, s.max_, float64(s.total_)/float64(s.count_)) }
func (s *RangeStatistic) ResumeRange(out *color.Color) { out.Printf("\n %s: min=%d, max=%d", s.name_, s.min_, s.max_) }
func (s *ScalarStatistic) ResumeAv(out *color.Color) { out.Printf("\n %s av: %f", s.name_, float64(s.total_)/float64(s.count_)) }
// consolePrintln - same as print with a new line. func consolePrintln(tag string, c *color.Color, a ...interface{}) { privateMutex.Lock() defer privateMutex.Unlock() switch tag { case "Debug": output := color.Output color.Output = stderrColoredOutput if isatty.IsTerminal(os.Stderr.Fd()) { c.Print(ProgramName() + ": <DEBUG> ") c.Println(a...) } else { fmt.Fprint(color.Output, ProgramName()+": <DEBUG> ") fmt.Fprintln(color.Output, a...) } color.Output = output case "Fatal": fallthrough case "Error": output := color.Output color.Output = stderrColoredOutput if isatty.IsTerminal(os.Stderr.Fd()) { c.Print(ProgramName() + ": <ERROR> ") c.Println(a...) } else { fmt.Fprint(color.Output, ProgramName()+": <ERROR> ") fmt.Fprintln(color.Output, a...) } color.Output = output case "Info": if isatty.IsTerminal(os.Stdout.Fd()) { c.Print(ProgramName() + ": ") c.Println(a...) } else { fmt.Fprint(color.Output, ProgramName()+": ") fmt.Fprintln(color.Output, a...) } default: if isatty.IsTerminal(os.Stdout.Fd()) { c.Println(a...) } else { fmt.Fprintln(color.Output, a...) } } }
// consolePrintln - same as print with a new line. func consolePrintln(tag string, c *color.Color, a ...interface{}) { privateMutex.Lock() defer privateMutex.Unlock() switch tag { case "Debug": // if no arguments are given do not invoke debug printer. if len(a) == 0 { return } output := color.Output color.Output = stderrColoredOutput if isatty.IsTerminal(os.Stderr.Fd()) { c.Print(ProgramName() + ": <DEBUG> ") c.Println(a...) } else { fmt.Fprint(color.Output, ProgramName()+": <DEBUG> ") fmt.Fprintln(color.Output, a...) } color.Output = output case "Fatal": fallthrough case "Error": // if no arguments are given do not invoke fatal and error printer. if len(a) == 0 { return } output := color.Output color.Output = stderrColoredOutput if isatty.IsTerminal(os.Stderr.Fd()) { c.Print(ProgramName() + ": <ERROR> ") c.Println(a...) } else { fmt.Fprint(color.Output, ProgramName()+": <ERROR> ") fmt.Fprintln(color.Output, a...) } color.Output = output case "Info": // if no arguments are given do not invoke info printer. if len(a) == 0 { return } if isatty.IsTerminal(os.Stdout.Fd()) { c.Print(ProgramName() + ": ") c.Println(a...) } else { fmt.Fprint(color.Output, ProgramName()+": ") fmt.Fprintln(color.Output, a...) } default: if isatty.IsTerminal(os.Stdout.Fd()) { c.Println(a...) } else { fmt.Fprintln(color.Output, a...) } } }
// getPrefix generates the log prefix in the given color func getPrefix(level string, color *color.Color) string { currentTime := time.Now().Format("15:04:05") toColoredString := color.SprintFunc() return toColoredString(fmt.Sprintf("%s[%s] ", level, currentTime)) }
func newDesc(s string, c *color.Color) desc { return desc{s, c.SprintFunc()} }
func (s *RangeHistogram) ResumeHistogramUnsort(out *color.Color) { out.Printf("\n %s: %d", s.name_, s.total_) for k, v := range s.data_ { out.Printf("\n [%d]: %d", k, v) } }