java-event-log-tags.py revision abfbbe2e1dc0d8dc01b87492427c670dab70f81f
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> <merged_tags_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) != 2:
54  print "need exactly two input files, not %d" % (len(args),)
55  print __doc__
56  sys.exit(1)
57
58fn = args[0]
59tagfile = event_log_tags.TagFile(fn)
60
61# 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
71if "java_package" not in tagfile.options:
72  tagfile.AddError("java_package option not specified", linenum=0)
73
74hide = True
75if "javadoc_hide" in tagfile.options:
76  hide = event_log_tags.BooleanFromString(tagfile.options["javadoc_hide"][0])
77
78if 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))
91
92if hide:
93  buffer.write("/**\n"
94               " * @hide\n"
95               " */\n")
96buffer.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))
107
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
138buffer.write("}\n");
139
140event_log_tags.WriteOutput(output_file, buffer)
141