frameworkplugins/com.nokia.s60tools.util/src/com/nokia/s60tools/util/debug/DbgUtility.java
author dpodwall
Tue, 12 Jan 2010 13:17:53 -0600
changeset 0 61163b28edca
permissions -rw-r--r--
initial EPL conversion
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     1
/*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     2
* Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     3
* All rights reserved.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     4
* This component and the accompanying materials are made available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     6
* which accompanies this distribution, and is available
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     8
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
     9
* Initial Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    11
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    12
* Contributors:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    13
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    14
* Description:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    15
*
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    16
*/
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    17
 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    18
 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    19
package com.nokia.s60tools.util.debug;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    20
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    21
import com.nokia.s60tools.util.internal.Messages;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    22
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    23
/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    24
 * Simple console debug utility that can be configured
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    25
 * via property settings given as command line parameter 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    26
 * to JVM (-Dproperty=value).
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    27
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    28
 * JVM parameter usage example:
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    29
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    30
 * <code>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    31
 * <pre>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    32
 *  -Dcom.nokia.s60tools.debug=true -Dcom.nokia.s60tools.debugpriority=class|operation
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    33
 * </pre>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    34
 * </code>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    35
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    36
 * The aforementioned JVM arguments enable debugging, and print out
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    37
 * all the debug messages with priority <code>PRIORITY_CLASS</code> 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    38
 * and <code>PRIORITY_OPERATION</code> but omit debug prints 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    39
 * with <code>PRIORITY_LOOP</code>.<br><br> 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    40
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    41
 * After the following parameter is added in the end of the previous parameters
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    42
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    43
 * <code>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    44
 * <pre> 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    45
 *  -Dcom.nokia.s60tools.debugfilter=MyClass
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    46
 * </pre>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    47
 * </code>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    48
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    49
 * only those debug messages that start with string "MyClass" are printed to stdout.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    50
 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    51
 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    52
public class DbgUtility {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    53
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    54
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    55
	 * Debug prints using this priority generate lots of information because priority
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    56
	 * should be used only when debugging happens in a tight loop.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    57
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    58
	public static String PRIORITY_LOOP = "loop"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    59
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    60
	 * Debug prints using this priority generate a moderate amount of information because priority 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    61
	 * should be used only when there is need to debug operation level information.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    62
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    63
	public static String PRIORITY_OPERATION = "operation"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    64
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    65
	 * Debug prints using this priority generate a small amount of information because priority 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    66
	 * should be used only when a class is initialized or deinitialized.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    67
	 */	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    68
	public static String PRIORITY_CLASS = "class";	 //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    69
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    70
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    71
	 * Value for this property can be any OR-combination of 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    72
	 * the priority types that are declared above.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    73
	 * 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    74
	 *  For example, <code>loop | operation | class</code>
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    75
	 *   
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    76
	 * The default value is <code>class</code>. 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    77
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    78
	private static String PROPERTY_PRIORITY = "com.nokia.s60tools.debugpriority"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    79
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    80
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    81
	 * Value for this property can be either <code>true</code> or <code>false</code>.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    82
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    83
	private static String PROPERTY_DEBUG = "com.nokia.s60tools.debug"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    84
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    85
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    86
	 * This property filters messages in a such way that it passes through
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    87
	 * only the mesages starting with the string defined in this property.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    88
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    89
	private static String PROPERTY_DEBUG_FILTER = "com.nokia.s60tools.debugfilter"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    90
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    91
	//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    92
	// Setting up the default values.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    93
	//
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    94
	private static String DEFAULT_DEBUG_PRIORITY = "class"; //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    95
	private static String DEFAULT_DEBUG_FILTER = "";	 //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    96
	private static String CURRENT_DEBUG_PRIORITY = System.getProperty(PROPERTY_PRIORITY, DEFAULT_DEBUG_PRIORITY);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    97
	private static String CURRENT_DEBUG_FILTER = System.getProperty(PROPERTY_DEBUG_FILTER, DEFAULT_DEBUG_FILTER);		
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    98
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
    99
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   100
	 * Prints message to stdout if it is higher or equal with the current 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   101
	 * debug priority and passes the currently used debugging filter.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   102
	 * @param priorityString Priority type of the message.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   103
	 * @param msg Message string.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   104
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   105
	public static void println(String priorityString, String msg){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   106
		if (Boolean.getBoolean(PROPERTY_DEBUG)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   107
			&&
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   108
			priorityMatchesWithCurrentDebugSetting(priorityString)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   109
			&&
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   110
			passesFilter(msg)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   111
			) {
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   112
			System.out.println(msg);
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   113
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   114
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   115
	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   116
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   117
	 * Sets a new debug priority.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   118
	 * @param priority New debug priority
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   119
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   120
	public static void setCurrentDebugPriority(String priority){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   121
		if(
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   122
			(priority.compareToIgnoreCase(PRIORITY_CLASS)== 0)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   123
			||
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   124
			(priority.compareToIgnoreCase(PRIORITY_LOOP)== 0)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   125
			||
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   126
			(priority.compareToIgnoreCase(PRIORITY_OPERATION)== 0)
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   127
			){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   128
			CURRENT_DEBUG_PRIORITY = priority;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   129
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   130
		else
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   131
		{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   132
			throw new IllegalArgumentException(Messages.getString("DbgUtility.Invalid_Debug_Priority_RunTime_Exception_Msg")); //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   133
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   134
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   135
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   136
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   137
	 * Debug priority filter.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   138
	 * @param priorityString Priority of the currently handled message.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   139
	 * @return Returns <code>true</code> if the debug message passes 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   140
	 *                 the filter, otherwise return <code>false</code>.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   141
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   142
	private static boolean priorityMatchesWithCurrentDebugSetting(String priorityString){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   143
		return (CURRENT_DEBUG_PRIORITY.toLowerCase()).indexOf(priorityString) >= 0;	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   144
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   145
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   146
	/**
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   147
	 * Debug string start prefix filter.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   148
	 * @param msg Debug message to be checked for filtering.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   149
	 * @return Returns <code>true</code> if the debug message passes 
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   150
	 *                 the filter, otherwise return <code>false</code>.
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   151
	 */
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   152
	private static boolean passesFilter(String msg){
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   153
		if(!CURRENT_DEBUG_FILTER.equalsIgnoreCase("")){ //$NON-NLS-1$
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   154
			return msg.startsWith(CURRENT_DEBUG_FILTER);	
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   155
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   156
		else{
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   157
			return true;
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   158
		}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   159
	}
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   160
61163b28edca initial EPL conversion
dpodwall
parents:
diff changeset
   161
}