blob: c72aa982cd2cdf8cb213f64604ba1b7e26fdd114 [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
Deepak Kodihalli99161192017-01-16 04:00:07 -060022def get_cpp_type(i_type):
23 typeMap = {
24 'int16': 'int16_t',
25 'int32': 'int32_t',
26 'int64': 'int64_t',
27 'uint16': 'uint16_t',
28 'uint32': 'uint32_t',
29 'uint64': 'uint64_t',
30 'double': 'double',
31 # const char* aids usage of constexpr
32 'string': 'const char*'}
33
34 return typeMap[i_type]
35
36
Andrew Geisslere8596302016-11-21 16:06:53 -060037def gen_elog_hpp(i_rootdir, i_elog_yaml, i_elog_meta_yaml,
38 i_input_mako, i_output_hpp):
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050039 r"""
40 Read the input yaml file, grab the relevant data and call the mako
41 template to generate the header file.
42
43 Description of arguments:
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060044 i_rootdir base directory to search for yaml files
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050045 i_elog_yaml yaml file describing the error logs
Andrew Geisslere8596302016-11-21 16:06:53 -060046 i_elog_meta_yaml yaml file describing the meta data for elogs
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -060047 i_input_mako input mako template file to use
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050048 i_output_hpp header file to output the generated code to
49 """
50
51 # Input parameters to mako template
Andrew Geissler184690d2016-11-03 08:06:31 -050052 errors = dict() # Main error codes
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050053 error_msg = dict() # Error msg that corresponds to error code
54 error_lvl = dict() # Error code log level (debug, info, error, ...)
Andrew Geissler184690d2016-11-03 08:06:31 -050055 meta = list() # The meta data names associated (ERRNO, FILE_NAME, ...)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050056 meta_data = dict() # The meta data info (type, format)
57
58 # see elog.yaml for reference
Andrew Geisslere8596302016-11-21 16:06:53 -060059 ifile = yaml.safe_load(open("/".join((i_rootdir, i_elog_yaml))))
60 mfile = yaml.safe_load(open(i_elog_meta_yaml))
Andrew Geisslerdf048c12016-11-10 16:50:35 -060061 err_count = 0
Andrew Geissler5e81bfb2016-11-21 12:49:01 -060062 for i in ifile:
Andrew Geisslere8596302016-11-21 16:06:53 -060063 match = None
64 # Find the corresponding meta data for this entry
65 for m in mfile:
66 if m['name'] == i['name']:
67 match = m
68 break
69 if (match is None):
70 print "Error - Did not find meta data for " + i['name']
71 exit(1)
Andrew Geisslerdf048c12016-11-10 16:50:35 -060072 # Grab the main error and it's info
73 errors[err_count] = i['name']
Andrew Geissler5e81bfb2016-11-21 12:49:01 -060074 error_msg[i['name']] = i['description']
Andrew Geisslere8596302016-11-21 16:06:53 -060075 error_lvl[i['name']] = match['level']
Andrew Geissler184690d2016-11-03 08:06:31 -050076 tmp_meta = []
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050077 # grab all the meta data fields and info
Andrew Geisslere8596302016-11-21 16:06:53 -060078 for j in match['meta']:
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050079 str_short = j['str'].split('=')[0]
80 tmp_meta.append(str_short)
81 meta_data[str_short] = {}
82 meta_data[str_short]['str'] = j['str']
83 meta_data[str_short]['str_short'] = str_short
Deepak Kodihalli99161192017-01-16 04:00:07 -060084 meta_data[str_short]['type'] = get_cpp_type(j['type'])
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050085 meta.append(tmp_meta)
Andrew Geissler184690d2016-11-03 08:06:31 -050086 err_count += 1
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050087
88 # Debug
Andrew Geissler184690d2016-11-03 08:06:31 -050089 # for i in errors:
Andrew Geisslerdf048c12016-11-10 16:50:35 -060090 # print "ERROR: " + errors[i]
91 # print " MSG: " + error_msg[errors[i]]
92 # print " LVL: " + error_lvl[errors[i]]
93 # print " META: "
94 # print meta[i]
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050095
96 # Load the mako template and call it with the required data
Andrew Geissler184690d2016-11-03 08:06:31 -050097 mytemplate = Template(filename=i_input_mako)
98 f = open(i_output_hpp, 'w')
99 f.write(mytemplate.render(errors=errors, error_msg=error_msg,
100 error_lvl=error_lvl, meta=meta,
Deepak Kodihalli99161192017-01-16 04:00:07 -0600101 meta_data=meta_data, elog_yaml=i_elog_yaml))
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500102 f.close()
103
104
105def main(i_args):
106 parser = OptionParser()
107
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600108 parser.add_option("-e", "--elog", dest="elog_yaml",
109 default="xyz/openbmc_project/Example/Elog.errors.yaml",
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600110 help="input error yaml file to parse")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500111
Andrew Geissler184690d2016-11-03 08:06:31 -0500112 parser.add_option("-m", "--mako", dest="elog_mako",
113 default="elog-gen-template.mako.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600114 help="input mako template file to use")
Andrew Geissler184690d2016-11-03 08:06:31 -0500115
116 parser.add_option("-o", "--output", dest="output_hpp",
117 default="elog-gen.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600118 help="output hpp to generate, elog-gen.hpp is default")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500119
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600120 parser.add_option("-r", "--rootdir", dest="rootdir",
121 default="example",
122 help="Base directory of yaml files to process")
123
124 parser.add_option("-t", "--templatedir", dest="templatedir",
125 default="phosphor-logging/templates/",
126 help="Base directory of files to process")
127
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500128 (options, args) = parser.parse_args(i_args)
129
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600130 # Verify the input yaml file
Andrew Geisslere8596302016-11-21 16:06:53 -0600131 yaml_path = "/".join((options.rootdir, options.elog_yaml))
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600132 if (not (os.path.isfile(yaml_path))):
133 print "Can not find input yaml file " + yaml_path
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600134 exit(1)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500135
Andrew Geisslere8596302016-11-21 16:06:53 -0600136 # the meta data will be defined in a similar file name where we replace
Deepak Kodihallid3103782017-01-16 03:32:55 -0600137 # <Interface>.errors.yaml with <Interface>.metadata.yaml
138 meta_file = options.elog_yaml.replace("errors", "metadata")
Andrew Geisslere8596302016-11-21 16:06:53 -0600139 meta_file = "/".join((options.rootdir, meta_file))
140 if (not (os.path.isfile(meta_file))):
141 print "Can not find meta yaml file " + meta_file
142 exit(1)
143
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600144 # Verify the input mako file
Andrew Geisslere8596302016-11-21 16:06:53 -0600145 template_path = "/".join((options.templatedir, options.elog_mako))
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600146 if (not (os.path.isfile(template_path))):
147 print "Can not find input template file " + template_path
148 exit(1)
149
150 gen_elog_hpp(options.rootdir,
151 options.elog_yaml,
Andrew Geisslere8596302016-11-21 16:06:53 -0600152 meta_file,
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600153 template_path,
Andrew Geissler184690d2016-11-03 08:06:31 -0500154 options.output_hpp)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500155
156# Only run if it's a script
157if __name__ == '__main__':
Andrew Geissler184690d2016-11-03 08:06:31 -0500158 main(sys.argv[1:])