xref: /openbmc/phosphor-logging/tools/elog-gen.py (revision f1f2cfa943d08f270976e42e1f386fd34e723003)
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_rootdir, 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_rootdir                    base directory to search for yaml files
29    i_elog_yaml                  yaml file describing the error logs
30    i_input_mako                 input mako template file to use
31    i_output_hpp                 header file to output the generated code to
32    """
33
34    # Input parameters to mako template
35    errors = dict()  # Main error codes
36    error_msg = dict()  # Error msg that corresponds to error code
37    error_lvl = dict()  # Error code log level (debug, info, error, ...)
38    meta = list()  # The meta data names associated (ERRNO, FILE_NAME, ...)
39    meta_data = dict()  # The meta data info (type, format)
40
41    # see elog.yaml for reference
42    ifile = yaml.safe_load(open("/".join((i_rootdir,i_elog_yaml))))
43    err_count = 0
44    for i in ifile:
45        # Grab the main error and it's info
46        errors[err_count] = i['name']
47        error_msg[i['name']] = i['description']
48        error_lvl[i['name']] = i['level']
49        tmp_meta = []
50        # grab all the meta data fields and info
51        for j in i['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)
59        err_count += 1
60
61    # Debug
62    # for i in errors:
63    #   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
70    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,elog_yaml=i_elog_yaml))
75    f.close()
76
77
78def main(i_args):
79    parser = OptionParser()
80
81    parser.add_option("-e", "--elog", dest="elog_yaml",
82                      default="xyz/openbmc_project/Example/Elog.errors.yaml",
83                      help="input error yaml file to parse")
84
85    parser.add_option("-m", "--mako", dest="elog_mako",
86                      default="elog-gen-template.mako.hpp",
87                      help="input mako template file to use")
88
89    parser.add_option("-o", "--output", dest="output_hpp",
90                      default="elog-gen.hpp",
91                      help="output hpp to generate, elog-gen.hpp is default")
92
93    parser.add_option("-r", "--rootdir", dest="rootdir",
94                      default="example",
95                      help="Base directory of yaml files to process")
96
97    parser.add_option("-t", "--templatedir", dest="templatedir",
98                      default="phosphor-logging/templates/",
99                      help="Base directory of files to process")
100
101    (options, args) = parser.parse_args(i_args)
102
103    # Verify the input yaml file
104    yaml_path = "/".join((options.rootdir,options.elog_yaml))
105    if (not (os.path.isfile(yaml_path))):
106        print "Can not find input yaml file " + yaml_path
107        exit(1)
108
109    # Verify the input mako file
110    template_path = "/".join((options.templatedir,options.elog_mako))
111    if (not (os.path.isfile(template_path))):
112        print "Can not find input template file " + template_path
113        exit(1)
114
115    gen_elog_hpp(options.rootdir,
116                 options.elog_yaml,
117                 template_path,
118                 options.output_hpp)
119
120# Only run if it's a script
121if __name__ == '__main__':
122    main(sys.argv[1:])
123