-
Notifications
You must be signed in to change notification settings - Fork 69
/
color.go
124 lines (109 loc) · 4 KB
/
color.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// package gxlog is based on log4go.
// color.go provides colorful terminal log output functions.
package gxlog
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
)
import (
"github.com/mattn/go-isatty"
)
var (
// Normal colors
// NORMAL = []byte{'\033', '0', 'm'}
NORMAL = []byte{'\033', '0'}
NBlack = []byte{'\033', '[', '3', '0', 'm'}
NRed = []byte{'\033', '[', '3', '1', 'm'}
NGreen = []byte{'\033', '[', '3', '2', 'm'}
NYellow = []byte{'\033', '[', '3', '3', 'm'}
NBlue = []byte{'\033', '[', '3', '4', 'm'}
NMagenta = []byte{'\033', '[', '3', '5', 'm'}
NCyan = []byte{'\033', '[', '3', '6', 'm'}
NWhite = []byte{'\033', '[', '3', '7', 'm'}
// Bright colors
BBlack = []byte{'\033', '[', '3', '0', ';', '1', 'm'}
BRed = []byte{'\033', '[', '3', '1', ';', '1', 'm'}
BGreen = []byte{'\033', '[', '3', '2', ';', '1', 'm'}
BYellow = []byte{'\033', '[', '3', '3', ';', '1', 'm'}
BBlue = []byte{'\033', '[', '3', '4', ';', '1', 'm'}
BMagenta = []byte{'\033', '[', '3', '5', ';', '1', 'm'}
BCyan = []byte{'\033', '[', '3', '6', ';', '1', 'm'}
BWhite = []byte{'\033', '[', '3', '7', ';', '1', 'm'}
UnderlineTwinkleHighLight = []byte{'\033', '[', '1', ';', '6', ';', '4', '0', 'm'}
reset = []byte{'\033', '[', '0', 'm'}
)
func funcFileLine() string {
tm := time.Unix(time.Now().Unix(), 0)
funcName, file, line, _ := runtime.Caller(3)
return "[" + tm.Format("2006-01-02/15:04:05 ") +
runtime.FuncForPC(funcName).Name() +
": " + filepath.Base(file) +
": " + fmt.Sprintf("%d", line) +
"] "
}
func CPrintf(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stdout, string(color)+funcFileLine()+"%s"+string(reset), logStr)
} else {
fmt.Fprintf(os.Stdout, "%s", logStr)
}
}
func CPrintfln(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stdout, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
} else {
fmt.Fprintf(os.Stdout, "%s\n", logStr)
}
}
func CEPrintf(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset), logStr)
} else {
fmt.Fprintf(os.Stderr, "%s", logStr)
}
}
func CEPrintfln(color []byte, format string, args ...interface{}) {
logStr := fmt.Sprintf(format, args...)
if isatty.IsTerminal(os.Stdout.Fd()) {
fmt.Fprintf(os.Stderr, string(color)+funcFileLine()+"%s"+string(reset)+"\n", logStr)
} else {
fmt.Fprintf(os.Stderr, "%s\n", logStr)
}
}
func CDebug(format string, args ...interface{}) {
CPrintfln(NORMAL, format, args...)
}
func CInfo(format string, args ...interface{}) {
CPrintfln(NGreen, format, args...)
}
func CWarn(format string, args ...interface{}) {
CEPrintfln(BMagenta, format, args...)
}
func CError(format string, args ...interface{}) {
CEPrintfln(NRed, format, args...)
}
func CFatal(format string, args ...interface{}) {
CEPrintfln(BRed, format, args...)
}