blob: 25f40f062c2a82735248f3ce6348791ac79d3f8d [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))
41 err_count = 0;
42 for i in ifile['SW'].keys():
43 # Grab the main error
44 errors[err_count] = i
45 # Grab it's sub-items
46 prop = ifile['SW'][i]
47 error_msg[i] = prop['msg']
48 error_lvl[i] = prop['level']
Andrew Geissler184690d2016-11-03 08:06:31 -050049 tmp_meta = []
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050050 # grab all the meta data fields and info
51 for j in prop['meta']:
52 str_short = j['str'].split('=')[0]
53 tmp_meta.append(str_short)
54 meta_data[str_short] = {}
55 meta_data[str_short]['str'] = j['str']
56 meta_data[str_short]['str_short'] = str_short
57 meta_data[str_short]['type'] = j['type']
58 meta.append(tmp_meta)
Andrew Geissler184690d2016-11-03 08:06:31 -050059 err_count += 1
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050060
61 # Debug
Andrew Geissler184690d2016-11-03 08:06:31 -050062 # for i in errors:
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050063 # print "ERROR: " + errors[i]
64 # print " MSG: " + error_msg[errors[i]]
65 # print " LVL: " + error_lvl[errors[i]]
66 # print " META: "
67 # print meta[i]
68
69 # Load the mako template and call it with the required data
Andrew Geissler184690d2016-11-03 08:06:31 -050070 mytemplate = Template(filename=i_input_mako)
71 f = open(i_output_hpp, 'w')
72 f.write(mytemplate.render(errors=errors, error_msg=error_msg,
73 error_lvl=error_lvl, meta=meta,
74 meta_data=meta_data))
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050075 f.close()
76
77
78def main(i_args):
79 parser = OptionParser()
80
Andrew Geissler184690d2016-11-03 08:06:31 -050081 parser.add_option("-e", "--elog", dest="elog_yaml", default="elog.yaml",
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050082 help="input error yaml file to parse");
83
Andrew Geissler184690d2016-11-03 08:06:31 -050084 parser.add_option("-m", "--mako", dest="elog_mako",
85 default="elog-gen-template.mako.hpp",
86 help="input mako template file to use");
87
88 parser.add_option("-o", "--output", dest="output_hpp",
89 default="elog-gen.hpp",
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050090 help="output hpp to generate, elog-gen.hpp is default");
91
92 (options, args) = parser.parse_args(i_args)
93
94 if (not (os.path.isfile(options.elog_yaml))):
95 print "Can not find input yaml file " + options.elog_yaml
96 exit(1);
97
Andrew Geissler184690d2016-11-03 08:06:31 -050098 gen_elog_hpp(options.elog_yaml, options.elog_mako,
99 options.output_hpp)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500100
101# Only run if it's a script
102if __name__ == '__main__':
Andrew Geissler184690d2016-11-03 08:06:31 -0500103 main(sys.argv[1:])