Auto generate the error log definition hpp

Change-Id: I93ad7c62aa53e3c670b8c0b1b98b1b58436a38d5
Signed-off-by: Andrew Geissler <andrewg@us.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 59963f8..4009061 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,11 +1,30 @@
 # export these headers
 include_HEADERS = log.hpp elog.hpp
 
+# Generate this header, don't export in a distro
+nodist_include_HEADERS = elog-gen.hpp
+
 # Application test which runs in obmc env (qemu, real hardware)
 bin_PROGRAMS = logging-test
 logging_test_SOURCES = logging_test.cpp
 
+# Be sure to build elog-gen.hpp before compiling
+BUILT_SOURCES = elog-gen.hpp
+
+CLEANFILES=elog-gen.hpp
+
 # systemd required for journal interfaces
 logging_test_LDFLAGS = $(SYSTEMD_LIBS)
 
+ELOG_YAML ?= ${srcdir}/elog.yaml
+ELOG_MAKO ?= ${srcdir}/elog-gen-template.mako.hpp
+REQ_FILES_TO_GEN ?= ${srcdir}/elog-gen.py $(ELOG_YAML) $(ELOG_MAKO)
+
+# Be sure to package up the required script, yaml, and mako template to
+# generate the header file
+EXTRA_DIST = $(REQ_FILES_TO_GEN)
+
+elog-gen.hpp: $(REQ_FILES_TO_GEN)
+	$(AM_V_at)${srcdir}/elog-gen.py -e $(ELOG_YAML) -m $(ELOG_MAKO) -o ${srcdir}/elog-gen.hpp
+
 SUBDIRS = test
\ No newline at end of file
diff --git a/elog-gen.hpp b/elog-gen.hpp
deleted file mode 100644
index 07d0757..0000000
--- a/elog-gen.hpp
+++ /dev/null
@@ -1,98 +0,0 @@
-// This file was autogenerated.  Do not edit!
-// See elog-gen.py for more details
-#pragma once
-
-#include <tuple>
-#include <type_traits>
-#include "log.hpp"
-
-namespace phosphor
-{
-
-namespace logging
-{
-
-namespace _GETSCOM
-{
-struct DEV_ADDR
-{
-    static constexpr auto str = "DEV_ADDR=0x%.8X";
-    static constexpr auto str_short = "DEV_ADDR";
-    using type = std::tuple<std::decay_t<decltype(str)>,int>;
-    explicit constexpr DEV_ADDR(int a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-struct DEV_ID
-{
-    static constexpr auto str = "DEV_ID=%u";
-    static constexpr auto str_short = "DEV_ID";
-    using type = std::tuple<std::decay_t<decltype(str)>,int>;
-    explicit constexpr DEV_ID(int a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-struct DEV_NAME
-{
-    static constexpr auto str = "DEV_NAME=%s";
-    static constexpr auto str_short = "DEV_NAME";
-    using type = std::tuple<std::decay_t<decltype(str)>,const char *>;
-    explicit constexpr DEV_NAME(const char * a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-
-}  // namespace _GETSCOM
-
-struct GETSCOM
-{
-    static constexpr auto err_code = "xyz.openbmc_project.logging.GETSCOM";
-    static constexpr auto err_msg = "Getscom call failed";
-    static constexpr auto L = level::ERR;
-    using DEV_ADDR = _GETSCOM::DEV_ADDR;
-    using DEV_ID = _GETSCOM::DEV_ID;
-    using DEV_NAME = _GETSCOM::DEV_NAME;
-    using metadata_types = std::tuple<DEV_ADDR, DEV_ID, DEV_NAME>;
-};
-
-namespace _FILE_NOT_FOUND
-{
-struct ERRNUM
-{
-    static constexpr auto str = "ERRNUM=0x%.4X";
-    static constexpr auto str_short = "ERRNUM";
-    using type = std::tuple<std::decay_t<decltype(str)>,int>;
-    explicit constexpr ERRNUM(int a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-struct FILE_PATH
-{
-    static constexpr auto str = "FILE_PATH=%s";
-    static constexpr auto str_short = "FILE_PATH";
-    using type = std::tuple<std::decay_t<decltype(str)>,const char *>;
-    explicit constexpr FILE_PATH(const char * a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-struct FILE_NAME
-{
-    static constexpr auto str = "FILE_NAME=%s";
-    static constexpr auto str_short = "FILE_NAME";
-    using type = std::tuple<std::decay_t<decltype(str)>,const char *>;
-    explicit constexpr FILE_NAME(const char * a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-
-}  // namespace _FILE_NOT_FOUND
-
-struct FILE_NOT_FOUND
-{
-    static constexpr auto err_code = "xyz.openbmc_project.logging.FILE_NOT_FOUND";
-    static constexpr auto err_msg = "A required file was not found";
-    static constexpr auto L = level::INFO;
-    using ERRNUM = _FILE_NOT_FOUND::ERRNUM;
-    using FILE_PATH = _FILE_NOT_FOUND::FILE_PATH;
-    using FILE_NAME = _FILE_NOT_FOUND::FILE_NAME;
-    using metadata_types = std::tuple<ERRNUM, FILE_PATH, FILE_NAME>;
-};
-
-
-} // namespace logging
-
-} // namespace phosphor
diff --git a/elog-gen.py b/elog-gen.py
old mode 100644
new mode 100755
index 61cceac..25f40f0
--- a/elog-gen.py
+++ b/elog-gen.py
@@ -18,7 +18,8 @@
 import sys
 import os
 
-def gen_elog_hpp(i_elog_yaml, i_output_hpp):
+
+def gen_elog_hpp(i_elog_yaml, i_input_mako, i_output_hpp):
     r"""
     Read the input yaml file, grab the relevant data and call the mako
     template to generate the header file.
@@ -29,10 +30,10 @@
     """
 
     # Input parameters to mako template
-    errors = dict()     # Main error codes
+    errors = dict()  # Main error codes
     error_msg = dict()  # Error msg that corresponds to error code
     error_lvl = dict()  # Error code log level (debug, info, error, ...)
-    meta = list()       # The meta data names associated (ERRNO, FILE_NAME, ...)
+    meta = list()  # The meta data names associated (ERRNO, FILE_NAME, ...)
     meta_data = dict()  # The meta data info (type, format)
 
     # see elog.yaml for reference
@@ -45,7 +46,7 @@
         prop = ifile['SW'][i]
         error_msg[i] = prop['msg']
         error_lvl[i] = prop['level']
-        tmp_meta=[]
+        tmp_meta = []
         # grab all the meta data fields and info
         for j in prop['meta']:
             str_short = j['str'].split('=')[0]
@@ -55,10 +56,10 @@
             meta_data[str_short]['str_short'] = str_short
             meta_data[str_short]['type'] = j['type']
         meta.append(tmp_meta)
-        err_count+=1
+        err_count += 1
 
     # Debug
-    #for i in errors:
+    # for i in errors:
     #    print "ERROR: " + errors[i]
     #    print " MSG:  " + error_msg[errors[i]]
     #    print " LVL:  " + error_lvl[errors[i]]
@@ -66,21 +67,26 @@
     #    print meta[i]
 
     # Load the mako template and call it with the required data
-    mytemplate = Template(filename='elog-gen-template.mako.hpp')
-    f = open(i_output_hpp,'w')
-    f.write(mytemplate.render(errors=errors,error_msg=error_msg,
-                            error_lvl=error_lvl,meta=meta,
-                            meta_data=meta_data))
+    mytemplate = Template(filename=i_input_mako)
+    f = open(i_output_hpp, 'w')
+    f.write(mytemplate.render(errors=errors, error_msg=error_msg,
+                              error_lvl=error_lvl, meta=meta,
+                              meta_data=meta_data))
     f.close()
 
 
 def main(i_args):
     parser = OptionParser()
 
-    parser.add_option("-e","--elog",dest="elog_yaml",default="elog.yaml",
+    parser.add_option("-e", "--elog", dest="elog_yaml", default="elog.yaml",
                       help="input error yaml file to parse");
 
-    parser.add_option("-o","--output",dest="output_hpp", default="elog-gen.hpp",
+    parser.add_option("-m", "--mako", dest="elog_mako",
+                      default="elog-gen-template.mako.hpp",
+                      help="input mako template file to use");
+
+    parser.add_option("-o", "--output", dest="output_hpp",
+                      default="elog-gen.hpp",
                       help="output hpp to generate, elog-gen.hpp is default");
 
     (options, args) = parser.parse_args(i_args)
@@ -89,8 +95,9 @@
         print "Can not find input yaml file " + options.elog_yaml
         exit(1);
 
-    gen_elog_hpp(options.elog_yaml,options.output_hpp)
+    gen_elog_hpp(options.elog_yaml, options.elog_mako,
+                 options.output_hpp)
 
 # Only run if it's a script
 if __name__ == '__main__':
-    main(sys.argv[1:])
\ No newline at end of file
+    main(sys.argv[1:])