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