summaryrefslogtreecommitdiff
path: root/src/plugins/preflighting.core/src/com/motorolamobility/preflighting/core/verbose/DebugVerboseOutputter.java
blob: 6a48e95a4992b0bbd56bd2f6f7cb3ac493012fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed 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 com.motorolamobility.preflighting.core.verbose;

import java.io.PrintStream;

import com.motorolamobility.preflighting.core.i18n.PreflightingCoreNLS;

/**
 * Abstract class responsible for defining verbosity levels, setting the current verbosity level,
 * and printing verbose messages according to verbosity level set.
 * The verbosity level will be passed as parameter to the application, and if not passed,
 * {@link DebugVerboseOutputter#DEFAULT_VERBOSE_LEVEL} will be assumed (default verbosity level).
 * The default stream used by this outputter is system.err.
 */
public abstract class DebugVerboseOutputter
{
    public enum VerboseLevel
    {
        /**
         * Default verbosity level (no added verbosity).
         */
        v0
        {
            /* (non-Javadoc)
             * @see java.lang.Enum#toString()
             */
            @Override
            public String toString()
            {
                return ""; //$NON-NLS-1$
            }
        },

        /**
         * Indicate the application flow as individual resources or conditions are checked.
         */
        v1
        {
            /* (non-Javadoc)
             * @see java.lang.Enum#toString()
             */
            @Override
            public String toString()
            {
                return PreflightingCoreNLS.VerboseOutputter_InfoVerboseLevelString;
            }
        },

        /**
         * Debugging mode indicates a great deal of information about every aspect of the execution
         * flow. This could be as functions are entered or exited.
         */
        v2
        {
            /* (non-Javadoc)
             * @see java.lang.Enum#toString()
             */
            @Override
            public String toString()
            {
                return PreflightingCoreNLS.VerboseOutputter_DebugVerboseLeveString;
            }
        };
    }

    private static final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$

    private static PrintStream myStream = System.out;

    /**
     * The default verbosity level ({@link VerboseLevel#v0}).
     */
    public static final VerboseLevel DEFAULT_VERBOSE_LEVEL = VerboseLevel.v0;

    /**
     * The current verbosity level used by the application.
     */
    private static VerboseLevel currentVerboseLevel = DEFAULT_VERBOSE_LEVEL;

    /**
     * Retrieve the current verbosity level used by the application.
     * 
     * @return Current verbosity level used by the application.
     */
    public static VerboseLevel getCurrentVerboseLevel()
    {
        return currentVerboseLevel;
    }

    /**
     * Set the current verbosity level to the given value.
     * If <code>null</code> is passed, the default verbosity level
     * ({@link DebugVerboseOutputter#DEFAULT_VERBOSE_LEVEL}) will be set.
     * 
     * @param level The verbosity level to be set.
     */
    public static void setCurrentVerboseLevel(VerboseLevel level)
    {
        if (level != null)
        {
            currentVerboseLevel = level;
        }
        else
        {
            currentVerboseLevel = DEFAULT_VERBOSE_LEVEL;
        }
    }

    /**
     * Print the message with the given verbosity level, if and only
     * if the current verbosity level allows it to be printed (current
     * level is greater or equal to the passed level).
     * 
     * @param message Message to be printed.
     * @param level The verbosity level in which the message should be printed.
     */
    public static void printVerboseMessage(String message, VerboseLevel level)
    {
        if (currentVerboseLevel.compareTo(level) >= 0)
        {
            if ((message != null) && (message.length() > 0))
            {
                getPrintStream().println(level + message);
            }
            else
            {
                getPrintStream().println();
            }
            getPrintStream().flush();
        }
    }

    /**
     * Return a string with the message with the given verbosity level, if and only
     * if the current verbosity level allows it to be printed (current
     * level is greater or equal to the passed level).
     * 
     * @param message Message to be printed.
     * @param level The verbosity level in which the message should be printed.
     * @return a string with the message with the given verbosity level.
     */
    public static String printVerboseMessageToString(String message, VerboseLevel level)
    {
        String strMsg = "";

        if (currentVerboseLevel.compareTo(level) >= 0)
        {
            if ((message != null) && (message.length() > 0))
            {
                strMsg = level + message;
            }
            else
            {
                strMsg = NEWLINE;
            }
        }
        return strMsg;
    }

    /**
     * Set the stream for printing the verbose output.
     * 
     * @param printStream The stream output.
     */
    public static void setStream(PrintStream printStream)
    {
        myStream = printStream;
    }

    /**
     * Retrieve the print stream to be used for printing messages.
     * 
     * @return The print stream.
     */
    private static PrintStream getPrintStream()
    {
        return myStream;
    }
}