3 # Copyright (C) 2009 The Android Open Source Project
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 Usage: java-event-log-tags.py [-o output_file] <input_file> <merged_tags_file>
20 Generate a java class containing constants for each of the event log
21 tags in the given input file.
23 -h to display this usage message and exit.
38 opts, args = getopt.getopt(sys.argv[1:], "ho:")
39 except getopt.GetoptError, err:
51 print >> sys.stderr, "unhandled option %s" % (o,)
55 print "need exactly two input files, not %d" % (len(args),)
60 tagfile = event_log_tags.TagFile(fn)
62 # Load the merged tag file (which should have numbers assigned for all
63 # tags. Use the numbers from the merged file to fill in any missing
64 # numbers from the input file.
66 merged_tagfile = event_log_tags.TagFile(merged_fn)
67 merged_by_name = dict([(t.tagname, t) for t in merged_tagfile.tags])
68 for t in tagfile.tags:
70 if t.tagname in merged_by_name:
71 t.tagnum = merged_by_name[t.tagname].tagnum
73 # We're building something that's not being included in the
74 # product, so its tags don't appear in the merged file. Assign
75 # them all an arbitrary number so we can emit the java and
76 # compile the (unused) package.
79 if "java_package" not in tagfile.options:
80 tagfile.AddError("java_package option not specified", linenum=0)
83 if "javadoc_hide" in tagfile.options:
84 hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
87 for fn, ln, msg in tagfile.errors:
88 print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
91 buffer = cStringIO.StringIO()
92 buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n"
93 " * Source file: %s\n"
96 buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
98 basename, _ = os.path.splitext(os.path.basename(fn))
104 buffer.write("public class %s {\n" % (basename,))
105 buffer.write(" private %s() { } // don't instantiate\n" % (basename,))
107 for t in tagfile.tags:
109 buffer.write("\n /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
111 buffer.write("\n /** %d %s */\n" % (t.tagnum, t.tagname))
113 buffer.write(" public static final int %s = %d;\n" %
114 (t.tagname.upper(), t.tagnum))
116 keywords = frozenset(["abstract", "continue", "for", "new", "switch", "assert",
117 "default", "goto", "package", "synchronized", "boolean",
118 "do", "if", "private", "this", "break", "double",
119 "implements", "protected", "throw", "byte", "else",
120 "import", "public", "throws", "case", "enum",
121 "instanceof", "return", "transient", "catch", "extends",
122 "int", "short", "try", "char", "final", "interface",
123 "static", "void", "class", "finally", "long", "strictfp",
124 "volatile", "const", "float", "native", "super", "while"])
127 out = name[0].lower() + re.sub(r"[^A-Za-z0-9]", "", name.title())[1:]
132 javaTypes = ["ERROR", "int", "long", "String", "Object[]", "float"]
133 for t in tagfile.tags:
134 methodName = javaName("write_" + t.tagname)
136 args = [arg.strip("() ").split("|") for arg in t.description.split(",")]
139 argTypesNames = ", ".join([javaTypes[int(arg[1])] + " " + javaName(arg[0]) for arg in args])
140 argNames = "".join([", " + javaName(arg[0]) for arg in args])
141 buffer.write("\n public static void %s(%s) {" % (methodName, argTypesNames))
142 buffer.write("\n android.util.EventLog.writeEvent(%s%s);" % (t.tagname.upper(), argNames))
143 buffer.write("\n }\n")
148 output_dir = os.path.dirname(output_file)
149 if not os.path.exists(output_dir):
150 os.makedirs(output_dir)
152 event_log_tags.WriteOutput(output_file, buffer)