From f75957ab6baef8907c8421f44f785956fbf48038 Mon Sep 17 00:00:00 2001 From: "Bala.FA" Date: Fri, 10 May 2013 12:30:17 +0530 Subject: 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 Reviewed-on: http://review.gluster.org/4977 Reviewed-by: Niels de Vos Tested-by: Gluster Build System Reviewed-by: Vijay Bellur --- Makefile.am | 4 +++- autogen.sh | 10 ++++++++ error-codes.json | 4 ++++ gen-headers.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ gf-error-codes.h.template | 33 +++++++++++++++++++++++++++ glusterfs.spec.in | 3 +++ libglusterfs/src/Makefile.am | 2 +- 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 error-codes.json create mode 100755 gen-headers.py create mode 100644 gf-error-codes.h.template diff --git a/Makefile.am b/Makefile.am index c5a8af6d..970ffdf7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,9 @@ EXTRA_DIST = autogen.sh \ COPYING-GPLV2 COPYING-LGPLV3 \ INSTALL README AUTHORS THANKS NEWS \ - glusterfs.spec glusterfs-api.pc.in + glusterfs.spec glusterfs-api.pc.in \ + error-codes.json gf-error-codes.h.template \ + gen-headers.py SUBDIRS = argp-standalone libglusterfs rpc api xlators glusterfsd \ $(FUSERMOUNT_SUBDIR) doc extras cli diff --git a/autogen.sh b/autogen.sh index d3e513a0..f937e6be 100755 --- a/autogen.sh +++ b/autogen.sh @@ -75,6 +75,16 @@ if [ "x$MISSING" != "x" ]; then exit 1 fi +## generate gf-error-codes.h from error-codes.json +echo "Generate gf-error-codes.h ..." +if ./gen-headers.py; then + if ! mv -fv gf-error-codes.h libglusterfs/src/gf-error-codes.h; then + exit 1 + fi +else + exit 1 +fi + ## Do the autogeneration echo Running ${ACLOCAL}... $ACLOCAL -I ./contrib/aclocal diff --git a/error-codes.json b/error-codes.json new file mode 100644 index 00000000..5121049d --- /dev/null +++ b/error-codes.json @@ -0,0 +1,4 @@ +{ + "ERR_DEV": {"code": 9999, + "message": {"en": "devel error"}} +} diff --git a/gen-headers.py b/gen-headers.py new file mode 100755 index 00000000..ef9fa771 --- /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) diff --git a/gf-error-codes.h.template b/gf-error-codes.h.template new file mode 100644 index 00000000..ab6020d6 --- /dev/null +++ b/gf-error-codes.h.template @@ -0,0 +1,33 @@ +/***************************************************************/ +/** **/ +/** DO NOT EDIT THIS FILE **/ +/** THIS IS AUTO-GENERATED FROM LOG BOOK **/ +/** YOUR CHANGES WILL BE LOST IN NEXT BUILD **/ +/** **/ +/***************************************************************/ + +#ifndef _GF_ERROR_CODES_H +#define _GF_ERROR_CODES_H + +#include + +#define _(STRING) gettext(STRING) + + +/** START: ERROR CODE DEFINITIONS **/ +$DEFINES +/** END: ERROR CODE DEFINITIONS **/ + + +/** START: FUNCTION RETURNS MESSAGE OF GIVEN ERROR CODE **/ +const char * +_gf_get_message (int code) { + switch (code) { +$CASES + default: return NULL; + } +} +/** END: FUNCTION RETURNS MESSAGE OF GIVEN ERROR CODE **/ + + +#endif diff --git a/glusterfs.spec.in b/glusterfs.spec.in index 82bff38d..85c656d7 100644 --- a/glusterfs.spec.in +++ b/glusterfs.spec.in @@ -69,6 +69,9 @@ Source0: @PACKAGE_NAME@-@PACKAGE_VERSION@.tar.gz BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX) +%if ( 0%{?rhel} && 0%{?rhel} <= 5 ) +BuildRequires: python-simplejson +%endif %if ( 0%{?_with_systemd:1} ) BuildRequires: systemd-units Requires(post): systemd-units diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am index 046e1b98..80753db3 100644 --- a/libglusterfs/src/Makefile.am +++ b/libglusterfs/src/Makefile.am @@ -29,7 +29,7 @@ libglusterfs_la_SOURCES = dict.c xlator.c logging.c \ event-poll.c event-epoll.c -nodist_libglusterfs_la_SOURCES = y.tab.c graph.lex.c +nodist_libglusterfs_la_SOURCES = y.tab.c graph.lex.c gf-error-codes.h BUILT_SOURCES = graph.lex.c -- cgit