blob: 63b8969fb01e753bd43fb7815527841353fe4ebe [file] [log] [blame]
Matt Spinlerd9c2fea2020-03-05 16:53:41 -06001#!/usr/bin/env python3
Andrew Geisslerc830e0f2016-10-18 12:51:29 -05002
3r"""
Deepak Kodihalli160d3e02017-01-17 04:25:22 -06004This script will parse error log yaml file(s) and generate
Andrew Geisslerc830e0f2016-10-18 12:51:29 -05005a 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.
Andrew Geisslerc830e0f2016-10-18 12:51:29 -050012"""
13
14from mako.template import Template
15from optparse import OptionParser
16import yaml
17import sys
18import os
19
Andrew Geissler184690d2016-11-03 08:06:31 -050020
Deepak Kodihalli5d1aace2017-01-18 00:17:39 -060021def order_inherited_errors(i_errors, i_parents):
22 # the ordered list of errors
23 errors = list()
24 has_inheritance = False
25 for error in i_errors:
26 if(i_parents[error] is not None):
27 has_inheritance = True
28 break
29
30 if(has_inheritance):
31 # Order the error codes list such that an error is never placed
32 # before it's parent. This way generated code can ensure parent
Gunnar Millsdeae3ca2017-10-25 17:22:22 -050033 # definitions precede child error definitions.
Deepak Kodihalli5d1aace2017-01-18 00:17:39 -060034 while(len(errors) < len(i_errors)):
35 for error in i_errors:
36 if(error in errors):
37 # already ordererd
38 continue
39 if((not i_parents[error]) or (i_parents[error] in errors)):
40 # parent present, or has no parent, either way this error
41 # can be added
42 errors.append(error)
43 else:
44 # no inherited errors
45 errors = i_errors
46
47 return errors
48
49
Deepak Kodihallicbd12c82017-01-17 08:50:55 -060050def check_error_inheritance(i_errors, i_parents):
Deepak Kodihalli5d1aace2017-01-18 00:17:39 -060051 for error in i_errors:
52 if(i_parents[error] and (i_parents[error] not in i_errors)):
Patrick Williams6f299132017-02-16 13:27:39 -060053 print(error + " inherits " + i_parents[error] +
54 " but the latter is not defined")
Deepak Kodihallicbd12c82017-01-17 08:50:55 -060055 return False
56 return True
57
58
Adriana Kobylak465aaec2017-02-20 11:58:03 -060059# Return the yaml files with their directory structure plus the file name
60# without the yaml extension, which will be used to set the namespaces.
61# Ex: file xyz/openbmc_project/Error/Callout/Device.errors.yaml
62# will have namespce xyz/openbmc_project/Error/Callout/Device
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -060063def get_error_yaml_files(i_yaml_dir, i_test_dir):
Adriana Kobylak371a38b2017-02-19 13:45:42 -060064 yaml_files = dict()
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -060065 if i_yaml_dir != "None":
66 for root, dirs, files in os.walk(i_yaml_dir):
Matt Spinlerd9c2fea2020-03-05 16:53:41 -060067 for files in \
68 [file for file in files if file.endswith('.errors.yaml')]:
Adriana Kobylak465aaec2017-02-20 11:58:03 -060069 splitdir = root.split(i_yaml_dir)[1] + "/" + files[:-12]
70 if splitdir.startswith("/"):
71 splitdir = splitdir[1:]
Adriana Kobylak371a38b2017-02-19 13:45:42 -060072 yaml_files[(os.path.join(root, files))] = splitdir
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -060073 for root, dirs, files in os.walk(i_test_dir):
Matt Spinlerd9c2fea2020-03-05 16:53:41 -060074 for files in [file for file in files if file.endswith('.errors.yaml')]:
Adriana Kobylak465aaec2017-02-20 11:58:03 -060075 splitdir = root.split(i_test_dir)[1] + "/" + files[:-12]
Adriana Kobylak371a38b2017-02-19 13:45:42 -060076 yaml_files[(os.path.join(root, files))] = splitdir
Deepak Kodihalli160d3e02017-01-17 04:25:22 -060077 return yaml_files
78
79
80def get_meta_yaml_file(i_error_yaml_file):
81 # the meta data will be defined in file name where we replace
82 # <Interface>.errors.yaml with <Interface>.metadata.yaml
83 meta_yaml = i_error_yaml_file.replace("errors", "metadata")
84 return meta_yaml
85
86
Deepak Kodihalli99161192017-01-16 04:00:07 -060087def get_cpp_type(i_type):
88 typeMap = {
Lei YUcea3f892020-12-03 14:27:10 +080089 'boolean': 'bool',
90 'int8': 'int8_t',
Deepak Kodihalli99161192017-01-16 04:00:07 -060091 'int16': 'int16_t',
92 'int32': 'int32_t',
93 'int64': 'int64_t',
Lei YUcea3f892020-12-03 14:27:10 +080094 'uint8': 'uint8_t',
Deepak Kodihalli99161192017-01-16 04:00:07 -060095 'uint16': 'uint16_t',
96 'uint32': 'uint32_t',
97 'uint64': 'uint64_t',
98 'double': 'double',
99 # const char* aids usage of constexpr
100 'string': 'const char*'}
101
102 return typeMap[i_type]
103
104
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -0600105def gen_elog_hpp(i_yaml_dir, i_test_dir, i_output_hpp,
Adriana Kobylak371a38b2017-02-19 13:45:42 -0600106 i_template_dir, i_elog_mako):
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500107 r"""
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600108 Read yaml file(s) under input yaml dir, grab the relevant data and call
109 the mako template to generate the output header file.
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500110
111 Description of arguments:
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -0600112 i_yaml_dir directory containing base error yaml files
113 i_test_dir directory containing test error yaml files
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600114 i_output_hpp name of the to be generated output hpp
115 i_template_dir directory containing error mako templates
116 i_elog_mako error mako template to render
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500117 """
118
119 # Input parameters to mako template
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600120 errors = list() # Main error codes
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500121 error_msg = dict() # Error msg that corresponds to error code
122 error_lvl = dict() # Error code log level (debug, info, error, ...)
Deepak Kodihalli5d1aace2017-01-18 00:17:39 -0600123 meta = dict() # The meta data names associated (ERRNO, FILE_NAME, ...)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500124 meta_data = dict() # The meta data info (type, format)
Deepak Kodihalli5d1aace2017-01-18 00:17:39 -0600125 parents = dict()
Deepak Kodihalli38f69742017-02-28 02:18:09 -0600126 metadata_process = dict() # metadata that have the 'process' keyword set
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500127
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -0600128 error_yamls = get_error_yaml_files(i_yaml_dir, i_test_dir)
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600129
130 for error_yaml in error_yamls:
131 # Verify the error yaml file
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600132 if (not (os.path.isfile(error_yaml))):
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500133 print("Cannot find input yaml file " + error_yaml)
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600134 exit(1)
135
136 # Verify the metadata yaml file
137 meta_yaml = get_meta_yaml_file(error_yaml)
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600138
139 # Verify the input mako file
Patrick Williamsc5e3e5e2021-04-16 10:18:49 -0500140 template_path = os.path.join(i_template_dir, i_elog_mako)
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600141 if (not (os.path.isfile(template_path))):
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500142 print("Cannot find input template file " + template_path)
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600143 exit(1)
Patrick Williamsc5e3e5e2021-04-16 10:18:49 -0500144 template_path = os.path.abspath(template_path)
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600145
146 get_elog_data(error_yaml,
147 meta_yaml,
Adriana Kobylak371a38b2017-02-19 13:45:42 -0600148 error_yamls[error_yaml],
149 # Last arg is a tuple
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600150 (errors,
151 error_msg,
152 error_lvl,
153 meta,
Deepak Kodihalli82b7de72017-01-17 07:59:29 -0600154 meta_data,
Deepak Kodihalli38f69742017-02-28 02:18:09 -0600155 parents,
156 metadata_process))
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600157
Deepak Kodihallicbd12c82017-01-17 08:50:55 -0600158 if(not check_error_inheritance(errors, parents)):
Patrick Williams6f299132017-02-16 13:27:39 -0600159 print("Error - failed to validate error inheritance")
Deepak Kodihallicbd12c82017-01-17 08:50:55 -0600160 exit(1)
161
Deepak Kodihalli5d1aace2017-01-18 00:17:39 -0600162 errors = order_inherited_errors(errors, parents)
163
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600164 # Load the mako template and call it with the required data
165 yaml_dir = i_yaml_dir.strip("./")
166 yaml_dir = yaml_dir.strip("../")
167 template = Template(filename=template_path)
168 f = open(i_output_hpp, 'w')
169 f.write(template.render(
Deepak Kodihalli38f69742017-02-28 02:18:09 -0600170 errors=errors,
171 error_msg=error_msg,
172 error_lvl=error_lvl,
173 meta=meta,
Adriana Kobylak465aaec2017-02-20 11:58:03 -0600174 meta_data=meta_data,
Deepak Kodihalli38f69742017-02-28 02:18:09 -0600175 parents=parents,
176 metadata_process=metadata_process))
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600177 f.close()
178
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600179def get_elog_data(i_elog_yaml,
180 i_elog_meta_yaml,
Adriana Kobylak371a38b2017-02-19 13:45:42 -0600181 i_namespace,
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600182 o_elog_data):
183 r"""
184 Parse the error and metadata yaml files in order to pull out
185 error metadata.
186
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500187 Use default values if metadata yaml file is not found.
188
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600189 Description of arguments:
190 i_elog_yaml error yaml file
191 i_elog_meta_yaml metadata yaml file
Adriana Kobylak371a38b2017-02-19 13:45:42 -0600192 i_namespace namespace data
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600193 o_elog_data error metadata
194 """
Deepak Kodihalli38f69742017-02-28 02:18:09 -0600195 (errors, error_msg, error_lvl, meta,
196 meta_data, parents, metadata_process) = o_elog_data
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600197 ifile = yaml.safe_load(open(i_elog_yaml))
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500198
199 #for all the errors in error yaml file
200 for error in ifile:
201 if 'name' not in error:
Patrick Williams4952aa62017-06-03 15:47:55 -0500202 print("Error - Did not find name in entry %s in file %s " % (
203 str(error), i_elog_yaml))
Andrew Geisslere8596302016-11-21 16:06:53 -0600204 exit(1)
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500205 fullname = i_namespace.replace('/', '.') + ('.') + error['name']
Adriana Kobylak465aaec2017-02-20 11:58:03 -0600206 errors.append(fullname)
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500207
208 if 'description' in error:
209 error_msg[fullname] = error['description'].strip()
210
211 #set default values
212 error_lvl[fullname] = "ERR"
213 parents[fullname] = None
214
215 #check if meta data yaml file is found
216 if not os.path.isfile(i_elog_meta_yaml):
217 continue
218 mfile = yaml.safe_load(open(i_elog_meta_yaml))
219
220 # Find the meta data entry
221 match = None
222 for meta_entry in mfile:
223 if meta_entry['name'] == error['name']:
224 match = meta_entry
225 break
226
227 if match is None:
William A. Kennington IIIb17e8762018-10-04 18:58:14 -0700228 print("Error - Did not find error named %s in %s" % (
229 error['name'], i_elog_meta_yaml))
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500230 continue
231
232 error_lvl[fullname] = match.get('level', 'ERR')
233
234 # Get 0th inherited error (current support - single inheritance)
235 if 'inherits' in match:
236 parents[fullname] = match['inherits'][0]
237
Lei YU80aa4762018-07-19 10:29:56 +0800238 # Put all errors in meta[] even the meta is empty
239 # so that child errors could inherits such error without meta
240 tmp_meta = []
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500241 if 'meta' in match:
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500242 # grab all the meta data fields and info
243 for i in match['meta']:
244 str_short = i['str'].split('=')[0]
Deepak Kodihalli7924b172017-03-08 00:04:33 -0600245 tmp_meta.append(str_short)
246 meta_data[str_short] = {}
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500247 meta_data[str_short]['str'] = i['str']
Deepak Kodihalli7924b172017-03-08 00:04:33 -0600248 meta_data[str_short]['str_short'] = str_short
Marri Devender Raodf7cb5c2017-05-14 08:30:40 -0500249 meta_data[str_short]['type'] = get_cpp_type(i['type'])
250 if ('process' in i) and (True == i['process']):
Deepak Kodihalli38f69742017-02-28 02:18:09 -0600251 metadata_process[str_short] = fullname + "." + str_short
Lei YU80aa4762018-07-19 10:29:56 +0800252 meta[fullname] = tmp_meta
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500253
254 # Debug
Andrew Geissler184690d2016-11-03 08:06:31 -0500255 # for i in errors:
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600256 # print "ERROR: " + errors[i]
257 # print " MSG: " + error_msg[errors[i]]
258 # print " LVL: " + error_lvl[errors[i]]
259 # print " META: "
260 # print meta[i]
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500261
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500262
263def main(i_args):
264 parser = OptionParser()
265
Andrew Geissler184690d2016-11-03 08:06:31 -0500266 parser.add_option("-m", "--mako", dest="elog_mako",
267 default="elog-gen-template.mako.hpp",
Andrew Geisslerdf048c12016-11-10 16:50:35 -0600268 help="input mako template file to use")
Andrew Geissler184690d2016-11-03 08:06:31 -0500269
270 parser.add_option("-o", "--output", dest="output_hpp",
Adriana Kobylakebd59fa2017-02-06 20:55:39 -0600271 default="elog-errors.hpp",
272 help="output hpp to generate, elog-errors.hpp default")
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500273
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600274 parser.add_option("-y", "--yamldir", dest="yamldir",
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -0600275 default="None",
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600276 help="Base directory of yaml files to process")
277
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -0600278 parser.add_option("-u", "--testdir", dest="testdir",
279 default="./tools/example/",
280 help="Unit test directory of yaml files to process")
281
Andrew Geisslerf1f2cfa2016-11-21 15:16:45 -0600282 parser.add_option("-t", "--templatedir", dest="templatedir",
283 default="phosphor-logging/templates/",
284 help="Base directory of files to process")
285
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500286 (options, args) = parser.parse_args(i_args)
287
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600288 gen_elog_hpp(options.yamldir,
Adriana Kobylak6a9db6e2017-02-21 12:58:05 -0600289 options.testdir,
Deepak Kodihalli160d3e02017-01-17 04:25:22 -0600290 options.output_hpp,
291 options.templatedir,
Adriana Kobylak371a38b2017-02-19 13:45:42 -0600292 options.elog_mako)
Andrew Geisslerc830e0f2016-10-18 12:51:29 -0500293
294# Only run if it's a script
295if __name__ == '__main__':
Andrew Geissler184690d2016-11-03 08:06:31 -0500296 main(sys.argv[1:])