Skip to content

Instantly share code, notes, and snippets.

@yeonsh
Created April 10, 2015 14:19
Show Gist options
  • Save yeonsh/811a32f4af42ee98578f to your computer and use it in GitHub Desktop.
Save yeonsh/811a32f4af42ee98578f to your computer and use it in GitHub Desktop.
Change log format to make it conform to Linux g++ rules.
import os
import re
regex = re.compile(r"(.*)(GLOG[A-Z]*\([A-Z]+::[A-Z_]+\s*,\s*)(__FUNCTION__)\s*(\")([^\"]+\")(\s*,\s*.*){0,1}(\s*\)\s*;){0,1}")
def test_code():
s=[]
s.append(' GLOGNOFLA(LOGLEVEL::LL_DEBUG, __FUNCTION__ " toSign [%s]");')
s.append(' GLOGNOFLA(LOGLEVEL::LL_DEBUG, __FUNCTION__ " toSign [%s]", toSign.c_str());')
s.append('GLOGNOFLA(LOGLEVEL::LL_ERROR, __FUNCTION__ " failed(%s) : %s", rtn.curlerror.c_str(), curl->GetLastURL().c_str());')
for s1 in s:
print "###", s1
result1 = regex.match(s1)
m = result1.groups()
#print repr(m)
out = m[0] + m[1] + m[3] + "%s" + m[4] + ", " + m[2]
if len(m) > 5:
for i in range(5, len(m)):
if m[i] is not None:
out += m[i]
print ">>>", out
def replace_log(filename):
tf = open(filename)
all_lines = tf.read()
tf.close()
if "__FUNCTION__" not in all_lines:
print "NOT FOUND. SKIP."
return
newfilename = filename + ".tmp"
newf = open(newfilename, "w")
with open(filename) as f:
for line in f:
if "__FUNCTION__" in line:
reg_result = regex.match(line)
if reg_result:
match_group = reg_result.groups()
newl = modified_line(match_group)
newf.write(newl+"\n")
else:
print "WARNING!!! NOT MATCHED", line
newf.write(line)
else:
newf.write(line)
newf.close()
try:
os.remove(filename+".bak")
except OSError:
pass
os.rename(filename, filename+".bak")
os.rename(newfilename, filename)
def modified_line(m):
#print repr(m)
out = m[0] + m[1] + m[3] + "%s" + m[4] + ", " + m[2]
if len(m) > 5:
for i in range(5, len(m)):
if m[i] is not None:
out += m[i]
return out
for root, dirs, files in os.walk(".", topdown=True):
for name in files:
if name.endswith(".cpp"):
filename = os.path.join(root, name)
print "Processing: ", filename
replace_log(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment