-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_levels.py
executable file
·174 lines (149 loc) · 3.7 KB
/
gen_levels.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
# Levels contain level names and codes. It is important to note that code is an integer.
# Python allows other values but underlying go code won't allow anything except integers.
levels = {
"debug": 100,
"info": 200,
"notice": 250,
"warn": 300,
"error": 400,
"critical": 500,
"alert": 550,
"fatal": 600,
"emergency": 600
}
# Function for replacing part of file between marks
def replace(file, mark, string):
s = ""
mode = "print"
warn = "/* WARNING: This part is autogenerated! Do not change! */\n\n"
unwarn = "\n/* END OF WARNING */\n"
for line in open(file):
lines = line.strip()
if lines == "/********* "+mark+" *********/":
s = s+line
mode = "skip"
elif lines == "/********* End "+mark+" *********/":
s = s+warn+string+unwarn+line
mode = "print"
elif mode == "skip":
pass
else:
s = s+line
with open(file, "w") as f:
f.write(s)
#########################
# Entry simple messages #
#########################
s = ""
for level in levels:
f = level.title()
s = s+"""// %s level message.
func (e *Entry) %s(msg string) {
e.Logger.log(%sLevel, e, msg)
}
""" % (f, f, f)
replace("entry.go", "Entry simple", s)
###########################
# Entry formated messages #
###########################
s = ""
for level in levels:
f = level.title()
s = s+"""// %sf level formatted message.
func (e *Entry) %sf(msg string, v ...interface{}) {
e.%s(fmt.Sprintf(msg, v...))
}
""" % (f, f, f)
replace("entry.go", "Entry formated", s)
#######################
# Log simple messages #
#######################
s = ""
for level in levels:
f = level.title()
s = s+"""// %s level message.
func (l *Logger) %s(msg string) {
NewEntry(l).%s(msg)
}
""" % (f, f, f)
replace("logger.go", "Log simple", s)
#########################
# Log formated messages #
#########################
s = ""
for level in levels:
f = level.title()
s = s+"""// %sf level formatted message.
func (l *Logger) %sf(msg string, v ...interface{}) {
NewEntry(l).%sf(msg, v...)
}
""" % (f, f, f)
replace("logger.go", "Log formated", s)
############################
# Pkg wide simple messages #
############################
s = ""
for level in levels:
f = level.title()
s = s+"""// %s level message.
func %s(msg string) {
Log.%s(msg)
}
""" % (f, f, f)
replace("pkg.go", "Pkg wide simple", s)
##############################
# Pkg wide formated messages #
##############################
s = ""
for level in levels:
f = level.title()
s = s+"""// %sf level message.
func %sf(msg string, v ...interface{}) {
Log.%sf(msg, v...)
}
""" % (f, f, f)
replace("pkg.go", "Pkg wide formated", s)
#############################
# Interface simple messages #
#############################
s = ""
for level in levels:
f = level.title()
s = s+" %s(msg string)\n" % (f)
replace("interface.go", "Interface simple", s)
###############################
# Interface formated messages #
###############################
s = ""
for level in levels:
f = level.title()
s = s+" %sf(msg string, v ...interface{})\n" % (f)
replace("interface.go", "Interface formated", s)
#################
# Level numbers #
#################
s = ""
for level, n in levels.items():
f = level.lower()
s = s+" %sLevel = %d\n" % (f.title(),n)
replace("levels.go", "Level numbers", s)
###########################
# Levels names to numbers #
###########################
s = ""
c = set()
for level, n in levels.items():
f = level.lower()
if n not in c:
c.add(n)
s = s+" %sLevel: \"%s\",\n" % (f.title(), f)
replace("levels.go", "Level no:name", s)
###########################
# Levels numbers to names #
###########################
s = ""
for level, n in levels.items():
f = level.lower()
s = s+" \"%s\": %sLevel,\n" % (f, f.title())
replace("levels.go", "Level name:no", s)