Android: How to Include Line Numbers, Class Names, and Method Names in Your Log Statements

Today, I’m going to share with you a handy tip for getting the most out of your Android log. I recommend that you create a wrapper class around the native Android Log class in order to:

  1. Have the ability to turn on or off logging.
  2. Have the ability to log handy things like line number, class names, and method names.

I’m going to show you a simple implementation of a Logger class that does these things:

import android.util.Log;

public class Logger
{
    private static boolean LOGGING_ENABLED;

    private static final int STACK_TRACE_LEVELS_UP = 5;

    public static void verbose(String tag, String message)
    {
	if (LOGGING_ENABLED)
	{
            Log.v(tag, getClassNameMethodNameAndLineNumber() + message);
	}
    }

    /**
     * Get the current line number. Note, this will only work as called from
     * this class as it has to go a predetermined number of steps up the stack
     * trace. In this case 5.
     * 
     * @author kvarela
     * @return int - Current line number.
     */
    private static int getLineNumber()
    {
	return Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getLineNumber();
    }

    /**
     * Get the current class name. Note, this will only work as called from this
     * class as it has to go a predetermined number of steps up the stack trace.
     * In this case 5.
     * 
     * @author kvarela
     * @return String - Current line number.
     */
    private static String getClassName()
    {
	String fileName = Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getFileName();

	// kvarela: Removing ".java" and returning class name
	return fileName.substring(0, fileName.length() - 5);
    }

    /**
     * Get the current method name. Note, this will only work as called from
     * this class as it has to go a predetermined number of steps up the stack
     * trace. In this case 5.
     * 
     * @author kvarela
     * @return String - Current line number.
     */
    private static String getMethodName()
    {
	return Thread.currentThread().getStackTrace()[STACK_TRACE_LEVELS_UP].getMethodName();
    }

    /**
     * Returns the class name, method name, and line number from the currently
     * executing log call in the form .()-
     * 
     * @author kvarela
     * @return String - String representing class name, method name, and line
     *         number.
     */
    private static String getClassNameMethodNameAndLineNumber()
    {
	return "[" + getClassName() + "." + getMethodName() + "()-" + getLineNumber() + "]: ";
    }
}

 
As you can see, this uses Thread.currentThread().getStackTrace()[] to climb up the stack trace and get the information from the proper line of code. In my case, it has to climb 5 levels up to get this information. Yours could be different depending on how many levels of abstraction you have when you call getStackTrace(). I found the easiest way is just to experiment with different numbers.

Android FrameLayout Example

The Android FrameLayout is one of the lesser used layouts in the Android system, but it definitely has its purposes. The main reason you’d use a FrameLayout is when you only want to show one View at a time inside that FrameLayout. You can think of the Views as if they were in a stack with the most recently added View at the top.
Let’s walk through a simple example. The following code will first draw a red square with width 250dp in the FrameLayout and then draw a white square with width 150dp in the FrameLayout. Since the white square comes after the red square, the white square will be drawn on top of the red square.



    
    

    
    

Here’s a screen shot:

FrameLayout Example
FrameLayout Example

Agile Methodology Possibly Not Best For Programmer Productivity

I’m currently working in a strictly agile-based development team. It’s ironic, but I love the nimbleness and high-level of communication that a structured agile environment gives us. I think agile methodologies are the best way to properly manage everybody’s expectations — developers, project managers, product owners, and any other stakeholders.

I’m not sure, however, that agile methodologies are the best way to get every ounce out of every programmer. I think that agile methodologies limit a programmer’s vision in a couple of ways:

  1. Grandiose epics are split up into smaller stories and even smaller tasks. Sometimes a programmer can get tunnel vision only focusing on a small task and overlooking the epic.
  2. Daily stand-ups don’t encourage strategy and vision; they only encourage task completion. When do the programmers, who are undoubtedly the smartest people in the office 🙂 give their input into strategy and vision?

What do you think? Are programmers’ brains fully utilized in an agile environment?

How to Enter Current Date/Time in Google Spreadsheet

I’ve been using Google Docs more and more since it’s an easy place for me to keep docs that I update from multiple machines. Google Docs is obviously not as robust as Microsoft Excel, but they are adding features rapidly.

One function I just discovered is the now() function. It will give you the current date and time and you can use it spreadsheet calculations. Quite handy if you ask me!

5 Tips For Being a Satisfied Negotiator

In my organizational behavior class, I’m currently studying how to get a favorable outcome while negotiating. Here are some tips.

1. Know What You Want
In order to be prepared, you should know what your target value is and either your lower or upper limit.

2. Know What You Can Get
In order to make an informed decision, you must know what you can get if go elsewhere. Once you know what you can get elsewhere, then you are prepared to walk away if necessary.

3. Be Aware of Time
You can use time to your favor. For example, if time is running out, you have a good chance of your opponent making concessions. Also, the longer you’ve been negotiating, the more the other party has “invested” in the negotiation, and may make concessions in this way as well.

4. Tread Carefully With Your First Offer
If your first offer is too high, your opponent may swing wildly in the low direction. If your first offer is too low, your opponent may refuse to continue negotiations.

5. Make Concessions
Concessions are seen as an act of good faith in a negotiation. The more you make, the more good faith you will build. So make small concessions, but make them often.