summaryrefslogtreecommitdiff
path: root/pkgtools/pkglint/files/logging.go
blob: 48db8890f61b2e0eb25ec89db84e38c613017f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main

import (
	"io"
)

const noFile = ""
const noLines = ""

type LogLevel struct {
	traditionalName string
	gccName         string
}

var (
	llFatal = &LogLevel{"FATAL", "fatal"}
	llError = &LogLevel{"ERROR", "error"}
	llWarn  = &LogLevel{"WARN", "warning"}
	llNote  = &LogLevel{"NOTE", "note"}
	llDebug = &LogLevel{"DEBUG", "debug"}
)

var dummyLine = NewLine(noFile, noLines, "", nil)

func logf(out io.Writer, level *LogLevel, fname, lineno, format string, args ...interface{}) bool {
	if fname != noFile {
		fname = cleanpath(fname)
	}

	var text, sep string
	if !G.opts.GccOutput {
		text += sep + level.traditionalName + ":"
		sep = " "
	}
	if fname != noFile {
		text += sep + fname
		sep = ": "
		if lineno != noLines {
			text += ":" + lineno
		}
	}
	if G.opts.GccOutput {
		text += sep + level.gccName + ":"
		sep = " "
	}
	text += sep + sprintf(format, args...) + "\n"
	io.WriteString(out, text)
	return true
}

func fatalf(fname, lineno, format string, args ...interface{}) {
	logf(G.logErr, llFatal, fname, lineno, format, args...)
	panic(pkglintFatal{})
}
func errorf(fname, lineno, format string, args ...interface{}) bool {
	G.errors++
	return logf(G.logOut, llError, fname, lineno, format, args...)
}
func warnf(fname, lineno, format string, args ...interface{}) bool {
	G.warnings++
	return logf(G.logOut, llWarn, fname, lineno, format, args...)
}
func notef(fname, lineno, format string, args ...interface{}) bool {
	return logf(G.logOut, llNote, fname, lineno, format, args...)
}
func debugf(fname, lineno, format string, args ...interface{}) bool {
	return logf(G.logOut, llDebug, fname, lineno, format, args...)
}

type pkglintFatal struct{}