blob: 552021ec3b2019276437c6a4285420a937b10d61 [file] [log] [blame]
Doug Zongker9bd49622009-11-30 14:28:59 -08001#!/usr/bin/env python
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"""
Doug Zongkerabfbbe22010-02-16 14:32:08 -080018Usage: java-event-log-tags.py [-o output_file] <input_file> <merged_tags_file>
Doug Zongker9bd49622009-11-30 14:28:59 -080019
20Generate a java class containing constants for each of the event log
21tags in the given input file.
22
23-h to display this usage message and exit.
24"""
25
26import cStringIO
27import getopt
28import os
Bjorn Bringert76a72272010-02-01 21:39:22 +000029import re
Doug Zongker9bd49622009-11-30 14:28:59 -080030import sys
31
32import event_log_tags
33
34output_file = None
35
36try:
37 opts, args = getopt.getopt(sys.argv[1:], "ho:")
38except getopt.GetoptError, err:
39 print str(err)
40 print __doc__
41 sys.exit(2)
42
43for o, a in opts:
44 if o == "-h":
45 print __doc__
46 sys.exit(2)
47 elif o == "-o":
48 output_file = a
49 else:
50 print >> sys.stderr, "unhandled option %s" % (o,)
51 sys.exit(1)
52
Doug Zongkerabfbbe22010-02-16 14:32:08 -080053if len(args) != 2:
54 print "need exactly two input files, not %d" % (len(args),)
Doug Zongker9bd49622009-11-30 14:28:59 -080055 print __doc__
56 sys.exit(1)
57
58fn = args[0]
59tagfile = event_log_tags.TagFile(fn)
60
Doug Zongkerabfbbe22010-02-16 14:32:08 -080061# Load the merged tag file (which should have numbers assigned for all
62# tags. Use the numbers from the merged file to fill in any missing
63# numbers from the input file.
64merged_fn = args[1]
65merged_tagfile = event_log_tags.TagFile(merged_fn)
66merged_by_name = dict([(t.tagname, t) for t in merged_tagfile.tags])
67for t in tagfile.tags:
68 if t.tagnum is None:
69 t.tagnum = merged_by_name[t.tagname].tagnum
70
Doug Zongker9bd49622009-11-30 14:28:59 -080071if "java_package" not in tagfile.options:
72 tagfile.AddError("java_package option not specified", linenum=0)
73
Doug Zongker5ae770f2009-12-08 12:45:02 -080074hide = True
75if "javadoc_hide" in tagfile.options:
76 hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
77
Doug Zongker9bd49622009-11-30 14:28:59 -080078if tagfile.errors:
79 for fn, ln, msg in tagfile.errors:
80 print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
81 sys.exit(1)
82
83buffer = cStringIO.StringIO()
84buffer.write("/* This file is auto-generated. DO NOT MODIFY.\n"
85 " * Source file: %s\n"
86 " */\n\n" % (fn,))
87
88buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
89
90basename, _ = os.path.splitext(os.path.basename(fn))
Doug Zongker5ae770f2009-12-08 12:45:02 -080091
92if hide:
93 buffer.write("/**\n"
94 " * @hide\n"
95 " */\n")
Doug Zongker9bd49622009-11-30 14:28:59 -080096buffer.write("public class %s {\n" % (basename,))
97buffer.write(" private %s() { } // don't instantiate\n" % (basename,))
98
99for t in tagfile.tags:
100 if t.description:
101 buffer.write("\n /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
102 else:
103 buffer.write("\n /** %d %s */\n" % (t.tagnum, t.tagname))
104
105 buffer.write(" public static final int %s = %d;\n" %
106 (t.tagname.upper(), t.tagnum))
Bjorn Bringert76a72272010-02-01 21:39:22 +0000107
108keywords = frozenset(["abstract", "continue", "for", "new", "switch", "assert",
109 "default", "goto", "package", "synchronized", "boolean",
110 "do", "if", "private", "this", "break", "double",
111 "implements", "protected", "throw", "byte", "else",
112 "import", "public", "throws", "case", "enum",
113 "instanceof", "return", "transient", "catch", "extends",
114 "int", "short", "try", "char", "final", "interface",
115 "static", "void", "class", "finally", "long", "strictfp",
116 "volatile", "const", "float", "native", "super", "while"])
117
118def javaName(name):
119 out = name[0].lower() + re.sub(r"[^A-Za-z0-9]", "", name.title())[1:]
120 if out in keywords:
121 out += "_"
122 return out
123
124javaTypes = ["ERROR", "int", "long", "String", "Object[]"]
125for t in tagfile.tags:
126 methodName = javaName("write_" + t.tagname)
127 if t.description:
128 args = [arg.strip("() ").split("|") for arg in t.description.split(",")]
129 else:
130 args = []
131 argTypesNames = ", ".join([javaTypes[int(arg[1])] + " " + javaName(arg[0]) for arg in args])
132 argNames = "".join([", " + javaName(arg[0]) for arg in args])
133 buffer.write("\n public static void %s(%s) {" % (methodName, argTypesNames))
134 buffer.write("\n android.util.EventLog.writeEvent(%s%s);" % (t.tagname.upper(), argNames))
135 buffer.write("\n }\n")
136
137
Doug Zongker9bd49622009-11-30 14:28:59 -0800138buffer.write("}\n");
139
140event_log_tags.WriteOutput(output_file, buffer)