blob: c94e1b48d33237020d4051a63afacb0e8776d737 [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
22def gen_elog_hpp(i_elog_yaml, i_input_mako, i_output_hpp):
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050023 r"""
24 Read the input yaml file, grab the relevant data and call the mako
25 template to generate the header file.
26
27 Description of arguments:
28 i_elog_yaml yaml file describing the error logs
29 i_output_hpp header file to output the generated code to
30 """
31
32 # Input parameters to mako template
Andrew Geissler184690d2016-11-03 08:06:31 -050033 errors = dict() # Main error codes
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050034 error_msg = dict() # Error msg that corresponds to error code
35 error_lvl = dict() # Error code log level (debug, info, error, ...)
Andrew Geissler184690d2016-11-03 08:06:31 -050036 meta = list() # The meta data names associated (ERRNO, FILE_NAME, ...)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050037 meta_data = dict() # The meta data info (type, format)
38
39 # see elog.yaml for reference
40 ifile = yaml.safe_load(open(i_elog_yaml))
Andrew Geisslerdf048c12016-11-10 16:50:35 -060041 err_count = 0
42 for i in ifile['error-codes']:
43 # Grab the main error and it's info
44 errors[err_count] = i['name']
45 error_msg[i['name']] = i['msg']
46 error_lvl[i['name']] = i['level']
Andrew Geissler184690d2016-11-03 08:06:31 -050047 tmp_meta = []
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050048 # grab all the meta data fields and info
Andrew Geisslerdf048c12016-11-10 16:50:35 -060049 for j in i['meta']:
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050050 str_short = j['str'].split('=')[0]
51 tmp_meta.append(str_short)
52 meta_data[str_short] = {}
53 meta_data[str_short]['str'] = j['str']
54 meta_data[str_short]['str_short'] = str_short
55 meta_data[str_short]['type'] = j['type']
56 meta.append(tmp_meta)
Andrew Geissler184690d2016-11-03 08:06:31 -050057 err_count += 1
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050058
59 # Debug
Andrew Geissler184690d2016-11-03 08:06:31 -050060 # for i in errors:
Andrew Geisslerdf048c12016-11-10 16:50:35 -060061 # print "ERROR: " + errors[i]
62 # print " MSG: " + error_msg[errors[i]]
63 # print " LVL: " + error_lvl[errors[i]]
64 # print " META: "
65 # print meta[i]
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050066
67 # Load the mako template and call it with the required data
Andrew Geissler184690d2016-11-03 08:06:31 -050068 mytemplate = Template(filename=i_input_mako)
69 f = open(i_output_hpp, 'w')
70 f.write(mytemplate.render(errors=errors, error_msg=error_msg,
71 error_lvl=error_lvl, meta=meta,
72 meta_data=meta_data))
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050073 f.close()
74
75
76def main(i_args):
77 parser = OptionParser()
78
Andrew Geissler184690d2016-11-03 08:06:31 -050079 parser.add_option("-e", "--elog", dest="elog_yaml", default="elog.yaml",
Andrew Geisslerdf048c12016-11-10 16:50:35 -060080 help="input error yaml file to parse")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050081
Andrew Geissler184690d2016-11-03 08:06:31 -050082 parser.add_option("-m", "--mako", dest="elog_mako",
83 default="elog-gen-template.mako.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -060084 help="input mako template file to use")
Andrew Geissler184690d2016-11-03 08:06:31 -050085
86 parser.add_option("-o", "--output", dest="output_hpp",
87 default="elog-gen.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -060088 help="output hpp to generate, elog-gen.hpp is default")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050089
90 (options, args) = parser.parse_args(i_args)
91
92 if (not (os.path.isfile(options.elog_yaml))):
93 print "Can not find input yaml file " + options.elog_yaml
Andrew Geisslerdf048c12016-11-10 16:50:35 -060094 exit(1)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050095
Andrew Geissler184690d2016-11-03 08:06:31 -050096 gen_elog_hpp(options.elog_yaml, options.elog_mako,
97 options.output_hpp)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050098
99# Only run if it's a script
100if __name__ == '__main__':
Andrew Geissler184690d2016-11-03 08:06:31 -0500101 main(sys.argv[1:])