java-event-log-tags.py revision 76a72277c9347dd8963097501f49df0f367544f3
1#!/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"""
18Usage: java-event-log-tags.py [-o output_file] <input_file>
19
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
29import re
30import 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
53if len(args) != 1:
54  print "need exactly one input file, not %d" % (len(args),)
55  print __doc__
56  sys.exit(1)
57
58fn = args[0]
59tagfile = event_log_tags.TagFile(fn)
60
61if "java_package" not in tagfile.options:
62  tagfile.AddError("java_package option not specified", linenum=0)
63
64hide = True
65if "javadoc_hide" in tagfile.options:
66  hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
67
68if tagfile.errors:
69  for fn, ln, msg in tagfile.errors:
70    print >> sys.stderr, "%s:%d: error: %s" % (fn, ln, msg)
71  sys.exit(1)
72
73buffer = cStringIO.StringIO()
74buffer.write("/* This file is auto-generated.  DO NOT MODIFY.\n"
75             " * Source file: %s\n"
76             " */\n\n" % (fn,))
77
78buffer.write("package %s;\n\n" % (tagfile.options["java_package"][0],))
79
80basename, _ = os.path.splitext(os.path.basename(fn))
81
82if hide:
83  buffer.write("/**\n"
84               " * @hide\n"
85               " */\n")
86buffer.write("public class %s {\n" % (basename,))
87buffer.write("  private %s() { }  // don't instantiate\n" % (basename,))
88
89for t in tagfile.tags:
90  if t.description:
91    buffer.write("\n  /** %d %s %s */\n" % (t.tagnum, t.tagname, t.description))
92  else:
93    buffer.write("\n  /** %d %s */\n" % (t.tagnum, t.tagname))
94
95  buffer.write("  public static final int %s = %d;\n" %
96               (t.tagname.upper(), t.tagnum))
97
98keywords = frozenset(["abstract", "continue", "for", "new", "switch", "assert",
99                      "default", "goto", "package", "synchronized", "boolean",
100                      "do", "if", "private", "this", "break", "double",
101                      "implements", "protected", "throw", "byte", "else",
102                      "import", "public", "throws", "case", "enum",
103                      "instanceof", "return", "transient", "catch", "extends",
104                      "int", "short", "try", "char", "final", "interface",
105                      "static", "void", "class", "finally", "long", "strictfp",
106                      "volatile", "const", "float", "native", "super", "while"])
107
108def javaName(name):
109  out = name[0].lower() + re.sub(r"[^A-Za-z0-9]", "", name.title())[1:]
110  if out in keywords:
111    out += "_"
112  return out
113
114javaTypes = ["ERROR", "int", "long", "String", "Object[]"]
115for t in tagfile.tags:
116  methodName = javaName("write_" + t.tagname)
117  if t.description:
118    args = [arg.strip("() ").split("|") for arg in t.description.split(",")]
119  else:
120    args = []
121  argTypesNames = ", ".join([javaTypes[int(arg[1])] + " " + javaName(arg[0]) for arg in args])
122  argNames = "".join([", " + javaName(arg[0]) for arg in args])
123  buffer.write("\n  public static void %s(%s) {" % (methodName, argTypesNames))
124  buffer.write("\n    android.util.EventLog.writeEvent(%s%s);" % (t.tagname.upper(), argNames))
125  buffer.write("\n  }\n")
126
127
128buffer.write("}\n");
129
130event_log_tags.WriteOutput(output_file, buffer)
131