summaryrefslogtreecommitdiffstats
path: root/gen-headers.py
diff options
context:
space:
mode:
authorBala.FA <barumuga@redhat.com>2013-05-10 12:30:17 +0530
committerVijay Bellur <vbellur@redhat.com>2013-07-19 03:55:15 -0700
commitf75957ab6baef8907c8421f44f785956fbf48038 (patch)
treecc561f2a15a8b8ac3ac569ead28f8c7d97dbd85c /gen-headers.py
parent4c0f4c8a89039b1fa1c9c015fb6f273268164c20 (diff)
log: error code generation support
error code and message are generated at compile time by reading a json file which contains information of elements for each error code. This framework provides error handling and ability to do more cleaner log messages to users. error-codes.json file contains error description is below format { "ERR_NAME": {"code": ERR_NUM, "message": {"LOCALE": "ERR_MESSAGE"}} } At compile time autogen.sh calls gen-headers.py which produces C header file libglusterfs/src/gf-error-codes.h. This header has a function const char *_gf_get_message (int code); which returns respective ERR_MESSAGE for given ERR_NUM. Change-Id: Ieefbf4c470e19a0175c28942e56cec98a3c94ff0 BUG: 928648 Signed-off-by: Bala.FA <barumuga@redhat.com> Reviewed-on: http://review.gluster.org/4977 Reviewed-by: Niels de Vos <ndevos@redhat.com> Tested-by: Gluster Build System <jenkins@build.gluster.com> Reviewed-by: Vijay Bellur <vbellur@redhat.com>
Diffstat (limited to 'gen-headers.py')
-rwxr-xr-xgen-headers.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/gen-headers.py b/gen-headers.py
new file mode 100755
index 00000000000..ef9fa77117c
--- /dev/null
+++ b/gen-headers.py
@@ -0,0 +1,54 @@
+#!/usr/bin/python
+
+import sys
+try:
+ import json
+except ImportError:
+ import simplejson as json
+from string import Template
+
+
+def getLogBook(logFile='error-codes.json'):
+ fp = open(logFile)
+ return json.load(fp)
+
+
+def genCHeader(logBook,
+ infile='gf-error-codes.h.template',
+ outfile='gf-error-codes.h'):
+ fp = open('gf-error-codes.h.template')
+ s = fp.read()
+ fp.close()
+ template = Template(s)
+
+ defineLines = []
+ caseLines = []
+ for name, value in logBook.iteritems():
+ nameDef = "GF_%s" % (name.upper(),)
+ code = value['code']
+ msgNameDef = "%s_MSG" % (nameDef,)
+ msg = value['message']['en']
+
+ defineLines.append("#define %-20s %d" % (nameDef, code))
+ defineLines.append("#define %-20s %s" % (msgNameDef,
+ json.dumps(msg)))
+ caseLines.append(" case %s: return _(%s);" % \
+ (nameDef, msgNameDef))
+
+ d = {'DEFINES': "\n".join(defineLines),
+ 'CASES': "\n".join(caseLines)}
+ #print template.substitute(d)
+
+ fp = open(outfile, 'w')
+ fp.write(template.substitute(d))
+ fp.close()
+
+
+if __name__ == "__main__":
+ try:
+ logBook = getLogBook()
+ genCHeader(logBook)
+ sys.exit(0)
+ except IOError, e:
+ print str(e)
+ sys.exit(-1)