xref: /openbmc/phosphor-logging/tools/elog-gen.py (revision 33ff62a2186f517988b66d1a77aee413904ef2ce)
1#!/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
21
22def gen_elog_hpp(i_elog_yaml, i_input_mako, i_output_hpp):
23    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
33    errors = dict()  # Main error codes
34    error_msg = dict()  # Error msg that corresponds to error code
35    error_lvl = dict()  # Error code log level (debug, info, error, ...)
36    meta = list()  # The meta data names associated (ERRNO, FILE_NAME, ...)
37    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:
43        # Grab the main error and it's info
44        errors[err_count] = i['name']
45        error_msg[i['name']] = i['description']
46        error_lvl[i['name']] = i['level']
47        tmp_meta = []
48        # grab all the meta data fields and info
49        for j in i['meta']:
50            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)
57        err_count += 1
58
59    # Debug
60    # for i in errors:
61    #   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]
66
67    # Load the mako template and call it with the required data
68    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))
73    f.close()
74
75
76def main(i_args):
77    parser = OptionParser()
78
79    parser.add_option("-e", "--elog", dest="elog_yaml", default="elog.yaml",
80                      help="input error yaml file to parse")
81
82    parser.add_option("-m", "--mako", dest="elog_mako",
83                      default="elog-gen-template.mako.hpp",
84                      help="input mako template file to use")
85
86    parser.add_option("-o", "--output", dest="output_hpp",
87                      default="elog-gen.hpp",
88                      help="output hpp to generate, elog-gen.hpp is default")
89
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
94        exit(1)
95
96    gen_elog_hpp(options.elog_yaml, options.elog_mako,
97                 options.output_hpp)
98
99# Only run if it's a script
100if __name__ == '__main__':
101    main(sys.argv[1:])
102