• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright (C) 2009 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""
18Generate a java class containing constants for each of the event log
19tags in the given input file.
20"""
21
22from io import StringIO
23import argparse
24import os
25import os.path
26import re
27import sys
28
29import event_log_tags
30
31parser = argparse.ArgumentParser(description=__doc__)
32parser.add_argument('-o', dest='output_file')
33parser.add_argument('file')
34args = parser.parse_args()
35
36fn = args.file
37tagfile = event_log_tags.TagFile(fn)
38
39if "java_package" not in tagfile.options:
40  tagfile.AddError("java_package option not specified", linenum=0)
41
42hide = True
43if "javadoc_hide" in tagfile.options:
44  hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
45
46if tagfile.errors:
47  for fn, ln, msg in tagfile.errors:
48    print("%s:%d: error: %s" % (fn, ln, msg), file=sys.stderr)
49  sys.exit(1)
50
51buffer = StringIO()
52buffer.write("/* This file is auto-generated.  DO NOT MODIFY.\n"
53             " * Source file: %s\n"
54             " */\n\n" % (fn,))
55
56# .rstrip(";") to avoid an empty top-level statement errorprone error
57buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0].rstrip(";"),))
58
59basename, _ = os.path.splitext(os.path.basename(fn))
60
61if hide:
62  buffer.write("/**\n"
63               " * @hide\n"
64               " */\n")
65buffer.write("public class %s {\n" % (basename,))
66buffer.write("  private %s() { }  // don't instantiate\n" % (basename,))
67
68for t in tagfile.tags:
69  if t.description:
70    buffer.write("\n  /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
71  else:
72    buffer.write("\n  /** %d %s */\n" % (t.tagnum, t.tagname))
73
74  buffer.write("  public static final int %s = %d;\n" %
75               (t.tagname.upper(), t.tagnum))
76
77keywords = frozenset(["abstract", "continue", "for", "new", "switch", "assert",
78                      "default", "goto", "package", "synchronized", "boolean",
79                      "do", "if", "private", "this", "break", "double",
80                      "implements", "protected", "throw", "byte", "else",
81                      "import", "public", "throws", "case", "enum",
82                      "instanceof", "return", "transient", "catch", "extends",
83                      "int", "short", "try", "char", "final", "interface",
84                      "static", "void", "class", "finally", "long", "strictfp",
85                      "volatile", "const", "float", "native", "super", "while"])
86
87def javaName(name):
88  out = name[0].lower() + re.sub(r"[^A-Za-z0-9]", "", name.title())[1:]
89  if out in keywords:
90    out += "_"
91  return out
92
93javaTypes = ["ERROR", "int", "long", "String", "Object[]", "float"]
94for t in tagfile.tags:
95  methodName = javaName("write_" + t.tagname)
96  if t.description:
97    fn_args = [arg.strip("() ").split("|") for arg in t.description.split(",")]
98  else:
99    fn_args = []
100  argTypesNames = ", ".join([javaTypes[int(arg[1])] + " " + javaName(arg[0]) for arg in fn_args])
101  argNames = "".join([", " + javaName(arg[0]) for arg in fn_args])
102  buffer.write("\n  public static void %s(%s) {" % (methodName, argTypesNames))
103  buffer.write("\n    android.util.EventLog.writeEvent(%s%s);" % (t.tagname.upper(), argNames))
104  buffer.write("\n  }\n")
105
106
107buffer.write("}\n");
108
109output_dir = os.path.dirname(args.output_file)
110if not os.path.exists(output_dir):
111  os.makedirs(output_dir)
112
113event_log_tags.WriteOutput(args.output_file, buffer)
114