You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/06/14 18:54:31 UTC
svn commit: r1798737 - in /pivot/trunk/core/src/org/apache/pivot/util:
ClassUtils.java Console.java
Author: rwhitcomb
Date: Wed Jun 14 18:54:31 2017
New Revision: 1798737
URL: http://svn.apache.org/viewvc?rev=1798737&view=rev
Log:
Code enhancement: Add and new "ClassUtils" class and two new methods to Console
(for debugging purposes):
ClassUtils is meant to hold static methods for dealing with "classes" (mostly by
reflection). The only method (so far) examines the call stack hierarchy to get
the identity/description of one of the callers of a method. Useful for debugging
"who gets called from where".
Then add two new methods to Console that will use this ClassUtils method to
describe the calling method and log the message along with it:
* logMethod(String, args) -> get the calling method name and format a message
from the format string and optional arguments.
* logMethod(String, String, args) -> same as above, but with an optional prefix
to the rest of the message so particular debug printouts can be easily
spotted in a log file.
Added:
pivot/trunk/core/src/org/apache/pivot/util/ClassUtils.java
Modified:
pivot/trunk/core/src/org/apache/pivot/util/Console.java
Added: pivot/trunk/core/src/org/apache/pivot/util/ClassUtils.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/ClassUtils.java?rev=1798737&view=auto
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/ClassUtils.java (added)
+++ pivot/trunk/core/src/org/apache/pivot/util/ClassUtils.java Wed Jun 14 18:54:31 2017
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pivot.util;
+
+/**
+ * Utility class for dealing with classes.
+ */
+public class ClassUtils {
+ public static final String UNKNOWN_CALLER = "<unknown caller>";
+
+ /**
+ * Return the description (name, location) of a caller of this method.
+ *
+ * @param level 0 = the caller of this method, 1 = its caller, etc.
+ * @return The {@link #UNKNOWN_CALLER} string if the level is
+ * out of range or a formatted string that describes the
+ * caller at the given level.
+ * @throws IllegalArgumentException if the level value is negative.
+ */
+ public static String getCallingMethod(int level) {
+ Utils.checkNonNegative(level, "level");
+
+ StackTraceElement[] elements = Thread.currentThread().getStackTrace();
+ // level + 2 because 0 = inside "getStackTrace", 1 = inside here, so
+ // 2 is the caller of this method, etc.
+ if (elements == null || level + 2 >= elements.length)
+ return UNKNOWN_CALLER;
+ return elements[level + 2].toString();
+ }
+
+}
Modified: pivot/trunk/core/src/org/apache/pivot/util/Console.java
URL: http://svn.apache.org/viewvc/pivot/trunk/core/src/org/apache/pivot/util/Console.java?rev=1798737&r1=1798736&r2=1798737&view=diff
==============================================================================
--- pivot/trunk/core/src/org/apache/pivot/util/Console.java (original)
+++ pivot/trunk/core/src/org/apache/pivot/util/Console.java Wed Jun 14 18:54:31 2017
@@ -46,4 +46,34 @@ public class Console {
System.err.println(message != null ? message : "");
}
+ /**
+ * Log a message along with the calling method's name to the system console.
+ *
+ * @param message The message to be logged to {@link System#out},
+ * or a format string using the remaining args (can be {@code null}).
+ * @param args The optional arguments used to format the final message.
+ */
+ public static final void logMethod(String message, Object... args) {
+ logOutput(ClassUtils.getCallingMethod(1) + ": " +
+ (message == null ? "" : String.format(message, args)));
+ }
+
+ /**
+ * Log a message with an identifying prefix, along with the calling
+ * method's name to the system console.
+ *
+ * @param prefix Any kind of "marker" prefix to the message (can be {@code null}).
+ * @param message The message to be logged to {@link System#out},
+ * or a format string using the remaining args (can be {@code null}).
+ * @param args The optional arguments used to format the final message.
+ */
+ public static final void logMethod(String prefix, String message, Object... args) {
+ logOutput(
+ (prefix == null ? "" : prefix + " ") +
+ ClassUtils.getCallingMethod(1) +
+ ": " +
+ (message == null ? "" : String.format(message, args))
+ );
+ }
+
}