Index: PRESUBMIT.py |
diff --git a/PRESUBMIT.py b/PRESUBMIT.py |
index 4d5c85391b6477f644b3edfabd28599e29f8dd53..515da1d0b38a040556c54eb010c94bdab8f4ae98 100644 |
--- a/PRESUBMIT.py |
+++ b/PRESUBMIT.py |
@@ -1304,6 +1304,77 @@ def _CheckJavaStyle(input_api, output_api): |
black_list=_EXCLUDED_PATHS + input_api.DEFAULT_BLACK_LIST) |
+def _CheckAndroidCrLogUsage(input_api, output_api): |
+ """Checks that new logs using org.chromium.base.Log: |
+ - Are using 'TAG' as variable name for the tags (warn) |
+ - Are using the suggested name format for the tags: "cr.<PackageTag>" (warn) |
+ - Are using a tag that is shorter than 23 characters (error) |
+ """ |
+ cr_log_import_pattern = input_api.re.compile( |
+ r'^import org\.chromium\.base\.Log;$', input_api.re.MULTILINE); |
+ # Extract the tag from lines like `Log.d(TAG, "*");` or `Log.d("TAG", "*");` |
+ cr_log_pattern = input_api.re.compile(r'^\s*Log\.\w\((?P<tag>\"?\w+\"?)\,') |
+ log_decl_pattern = input_api.re.compile( |
+ r'^\s*private static final String TAG = "(?P<name>(.*)")', |
+ input_api.re.MULTILINE) |
+ log_name_pattern = input_api.re.compile(r'^cr[.\w]*') |
+ |
+ REF_MSG = ('See base/android/java/src/org/chromium/base/README_logging.md ' |
+ 'or contact dgn@chromium.org for more info.') |
+ sources = lambda x: input_api.FilterSourceFile(x, white_list=(r'.*\.java$',)) |
+ tag_errors = [] |
+ tag_decl_errors = [] |
+ tag_length_errors = [] |
+ |
+ for f in input_api.AffectedSourceFiles(sources): |
+ file_content = input_api.ReadFile(f) |
+ has_modified_logs = False |
+ |
+ # Per line checks |
+ if cr_log_import_pattern.search(file_content): |
+ for line_num, line in f.ChangedContents(): |
+ |
+ # Check if the new line is doing some logging |
+ match = cr_log_pattern.search(line) |
+ if match: |
+ has_modified_logs = True |
+ |
+ # Make sure it uses "TAG" |
+ if not match.group('tag') == 'TAG': |
+ tag_errors.append("%s:%d" % (f.LocalPath(), line_num)) |
+ |
+ # Per file checks |
+ if has_modified_logs: |
+ # Make sure the tag is using the "cr" prefix and is not too long |
+ match = log_decl_pattern.search(file_content) |
+ tag_name = match.group('name') if match else '' |
+ if not log_name_pattern.search(tag_name ): |
+ tag_decl_errors.append(f.LocalPath()) |
+ if len(tag_name) > 23: |
+ tag_length_errors.append(f.LocalPath()) |
+ |
+ results = [] |
+ if tag_decl_errors: |
+ results.append(output_api.PresubmitPromptWarning( |
+ 'Please define your tags using the suggested format: .\n' |
+ '"private static final String TAG = "cr.<package tag>".\n' + REF_MSG, |
+ tag_decl_errors)) |
+ |
+ if tag_length_errors: |
+ results.append(output_api.PresubmitError( |
+ 'The tag length is restricted by the system to be at most ' |
+ '23 characters.\n' + REF_MSG, |
+ tag_length_errors)) |
+ |
+ if tag_errors: |
+ results.append(output_api.PresubmitPromptWarning( |
+ 'Please use a variable named "TAG" for your log tags.\n' + REF_MSG, |
+ tag_errors)) |
+ |
+ return results |
+ |
+ |
+# TODO(dgn): refactor with _CheckAndroidCrLogUsage |
def _CheckNoNewUtilLogUsage(input_api, output_api): |
"""Checks that new logs are using org.chromium.base.Log.""" |
@@ -1456,6 +1527,14 @@ def _CheckNoDeprecatedJS(input_api, output_api): |
return results |
+def _AndroidSpecificOnUploadChecks(input_api, output_api): |
+ """Groups checks that target android code.""" |
+ results = [] |
+ results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) |
+ results.extend(_CheckAndroidCrLogUsage(input_api, output_api)) |
+ return results |
+ |
+ |
def _CommonChecks(input_api, output_api): |
"""Checks common to both upload and commit.""" |
results = [] |
@@ -1728,7 +1807,7 @@ def CheckChangeOnUpload(input_api, output_api): |
results.extend( |
input_api.canned_checks.CheckGNFormatted(input_api, output_api)) |
results.extend(_CheckUmaHistogramChanges(input_api, output_api)) |
- results.extend(_CheckNoNewUtilLogUsage(input_api, output_api)) |
+ results.extend(_AndroidSpecificOnUploadChecks(input_api, output_api)) |
return results |