blob: 430f991168ae2be8d0d75900d9670be3a8324abf [file] [log] [blame]
Andrew Geisslerc830e0f2016-10-18 12:51:29 -05001#!/usr/bin/env python
2
3r"""
4This script will parse the input error log yaml file and generate
5a header file which will then be used by the error logging client and
6server to collect and validate the error information generated by the
7openbmc software components.
8
9This code uses a mako template to provide the basic template of the header
10file we're going to generate. We then call it with information from the
11yaml to generate the header file.
12
13"""
14
15from mako.template import Template
16from optparse import OptionParser
17import yaml
18import sys
19import os
20
Andrew Geissler184690d2016-11-03 08:06:31 -050021
Andrew Geisslere8596302016-11-21 16:06:53 -060022def gen_elog_hpp(i_rootdir, i_elog_yaml, i_elog_meta_yaml,
23 i_input_mako, i_output_hpp):
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050024 r"""
25 Read the input yaml file, grab the relevant data and call the mako
26 template to generate the header file.
27
28 Description of arguments:
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060029 i_rootdir base directory to search for yaml files
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050030 i_elog_yaml yaml file describing the error logs
Andrew Geisslere8596302016-11-21 16:06:53 -060031 i_elog_meta_yaml yaml file describing the meta data for elogs
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060032 i_input_mako input mako template file to use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050033 i_output_hpp header file to output the generated code to
34 """
35
36 # Input parameters to mako template
Andrew Geissler184690d2016-11-03 08:06:31 -050037 errors = dict() # Main error codes
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050038 error_msg = dict() # Error msg that corresponds to error code
39 error_lvl = dict() # Error code log level (debug, info, error, ...)
Andrew Geissler184690d2016-11-03 08:06:31 -050040 meta = list() # The meta data names associated (ERRNO, FILE_NAME, ...)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050041 meta_data = dict() # The meta data info (type, format)
42
43 # see elog.yaml for reference
Andrew Geisslere8596302016-11-21 16:06:53 -060044 ifile = yaml.safe_load(open("/".join((i_rootdir, i_elog_yaml))))
45 mfile = yaml.safe_load(open(i_elog_meta_yaml))
Andrew Geisslerdf048c12016-11-10 16:50:35 -060046 err_count = 0
Andrew Geissler5e81bfb2016-11-21 12:49:01 -060047 for i in ifile:
Andrew Geisslere8596302016-11-21 16:06:53 -060048 match = None
49 # Find the corresponding meta data for this entry
50 for m in mfile:
51 if m['name'] == i['name']:
52 match = m
53 break
54 if (match is None):
55 print "Error - Did not find meta data for " + i['name']
56 exit(1)
Andrew Geisslerdf048c12016-11-10 16:50:35 -060057 # Grab the main error and it's info
58 errors[err_count] = i['name']
Andrew Geissler5e81bfb2016-11-21 12:49:01 -060059 error_msg[i['name']] = i['description']
Andrew Geisslere8596302016-11-21 16:06:53 -060060 error_lvl[i['name']] = match['level']
Andrew Geissler184690d2016-11-03 08:06:31 -050061 tmp_meta = []
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050062 # grab all the meta data fields and info
Andrew Geisslere8596302016-11-21 16:06:53 -060063 for j in match['meta']:
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050064 str_short = j['str'].split('=')[0]
65 tmp_meta.append(str_short)
66 meta_data[str_short] = {}
67 meta_data[str_short]['str'] = j['str']
68 meta_data[str_short]['str_short'] = str_short
69 meta_data[str_short]['type'] = j['type']
70 meta.append(tmp_meta)
Andrew Geissler184690d2016-11-03 08:06:31 -050071 err_count += 1
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050072
73 # Debug
Andrew Geissler184690d2016-11-03 08:06:31 -050074 # for i in errors:
Andrew Geisslerdf048c12016-11-10 16:50:35 -060075 # print "ERROR: " + errors[i]
76 # print " MSG: " + error_msg[errors[i]]
77 # print " LVL: " + error_lvl[errors[i]]
78 # print " META: "
79 # print meta[i]
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050080
81 # Load the mako template and call it with the required data
Andrew Geissler184690d2016-11-03 08:06:31 -050082 mytemplate = Template(filename=i_input_mako)
83 f = open(i_output_hpp, 'w')
84 f.write(mytemplate.render(errors=errors, error_msg=error_msg,
85 error_lvl=error_lvl, meta=meta,
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060086 meta_data=meta_data,elog_yaml=i_elog_yaml))
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050087 f.close()
88
89
90def main(i_args):
91 parser = OptionParser()
92
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060093 parser.add_option("-e", "--elog", dest="elog_yaml",
94 default="xyz/openbmc_project/Example/Elog.errors.yaml",
Andrew Geisslerdf048c12016-11-10 16:50:35 -060095 help="input error yaml file to parse")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050096
Andrew Geissler184690d2016-11-03 08:06:31 -050097 parser.add_option("-m", "--mako", dest="elog_mako",
98 default="elog-gen-template.mako.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -060099 help="input mako template file to use")
Andrew Geissler184690d2016-11-03 08:06:31 -0500100
101 parser.add_option("-o", "--output", dest="output_hpp",
102 default="elog-gen.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600103 help="output hpp to generate, elog-gen.hpp is default")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500104
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600105 parser.add_option("-r", "--rootdir", dest="rootdir",
106 default="example",
107 help="Base directory of yaml files to process")
108
109 parser.add_option("-t", "--templatedir", dest="templatedir",
110 default="phosphor-logging/templates/",
111 help="Base directory of files to process")
112
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500113 (options, args) = parser.parse_args(i_args)
114
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600115 # Verify the input yaml file
Andrew Geisslere8596302016-11-21 16:06:53 -0600116 yaml_path = "/".join((options.rootdir, options.elog_yaml))
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600117 if (not (os.path.isfile(yaml_path))):
118 print "Can not find input yaml file " + yaml_path
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600119 exit(1)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500120
Andrew Geisslere8596302016-11-21 16:06:53 -0600121 # the meta data will be defined in a similar file name where we replace
122 # <Interface>.errors.yaml with <Interface>.logging.yaml
123 meta_file = options.elog_yaml.replace("errors", "logging")
124 meta_file = "/".join((options.rootdir, meta_file))
125 if (not (os.path.isfile(meta_file))):
126 print "Can not find meta yaml file " + meta_file
127 exit(1)
128
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600129 # Verify the input mako file
Andrew Geisslere8596302016-11-21 16:06:53 -0600130 template_path = "/".join((options.templatedir, options.elog_mako))
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600131 if (not (os.path.isfile(template_path))):
132 print "Can not find input template file " + template_path
133 exit(1)
134
135 gen_elog_hpp(options.rootdir,
136 options.elog_yaml,
Andrew Geisslere8596302016-11-21 16:06:53 -0600137 meta_file,
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600138 template_path,
Andrew Geissler184690d2016-11-03 08:06:31 -0500139 options.output_hpp)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500140
141# Only run if it's a script
142if __name__ == '__main__':
Andrew Geissler184690d2016-11-03 08:06:31 -0500143 main(sys.argv[1:])