gen-event-enum.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. # Write out C++ enum definitions that represent the different event types.
  5. #
  6. # The events are defined in files provided as command-line arguments.
  7. from __future__ import print_function
  8. import sys
  9. import parse_events
  10. banner = """/* This file is auto-generated, see gen-event-enum.py. */
  11. """
  12. file_header = """\
  13. #ifndef mozilla_TelemetryEventEnums_h
  14. #define mozilla_TelemetryEventEnums_h
  15. namespace mozilla {
  16. namespace Telemetry {
  17. namespace EventID {
  18. """
  19. file_footer = """\
  20. } // namespace EventID
  21. } // namespace mozilla
  22. } // namespace Telemetry
  23. #endif // mozilla_TelemetryEventEnums_h
  24. """
  25. def main(output, *filenames):
  26. # Load the events first.
  27. if len(filenames) > 1:
  28. raise Exception('We don\'t support loading from more than one file.')
  29. events = parse_events.load_events(filenames[0])
  30. grouped = dict()
  31. index = 0
  32. for e in events:
  33. category = e.category
  34. if not category in grouped:
  35. grouped[category] = []
  36. grouped[category].append((index, e))
  37. index += len(e.enum_labels)
  38. # Write the enum file.
  39. print(banner, file=output)
  40. print(file_header, file=output);
  41. for category,indexed in grouped.iteritems():
  42. category_cpp = indexed[0][1].category_cpp
  43. print("// category: %s" % category, file=output)
  44. print("enum class %s : uint32_t {" % category_cpp, file=output)
  45. for event_index,e in indexed:
  46. cpp_guard = e.cpp_guard
  47. if cpp_guard:
  48. print("#if defined(%s)" % cpp_guard, file=output)
  49. for offset,label in enumerate(e.enum_labels):
  50. print(" %s = %d," % (label, event_index + offset), file=output)
  51. if cpp_guard:
  52. print("#endif", file=output)
  53. print("};\n", file=output)
  54. print("const uint32_t EventCount = %d;\n" % index, file=output)
  55. print(file_footer, file=output)
  56. if __name__ == '__main__':
  57. main(sys.stdout, *sys.argv[1:])