Subversion Repositories DevTools

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
227 dpurdie 1
#==============================================================================
2
# **** Source Information ****
3
#
4
# Program Name        : DeployUtils::Logger.pm
5
#
6
# Program Type        : Perl Module (.pm)
7
#
8
# Original Author(s)  : G Christidis (gchristi)
9
#
10
# Description / Purpose:
11
#       A Logging Package for the deploy utilities.
12
#
13
#==============================================================================
14
 
15
#------------------------------------------------------------------------------
16
# Package definition
17
#------------------------------------------------------------------------------
18
package DeployUtils::Logger;
19
 
20
use strict;
21
use IO::Handle;
22
 
23
BEGIN
24
{
25
    # automatically export what we need into namespace of caller.
26
    use Exporter();
27
    our (@ISA, @EXPORT);
28
    @ISA         = qw(Exporter);
29
    @EXPORT      = qw(setLogLevel getLogLevel LogNorm LogError LogWarn LogInfo LogDebug LogRaw $LOG_LEVEL_ERROR $LOG_LEVEL_WARN $LOG_LEVEL_NORM $LOG_LEVEL_INFO $LOG_LEVEL_DEBUG );
30
    autoflush STDERR;
31
    autoflush STDOUT;
32
}
33
 
34
#------------------------------------------------------------------------------
35
# Constants global to this package and exported
36
#------------------------------------------------------------------------------
37
our ($LOG_LEVEL_ERROR)        = 1;
38
our ($LOG_LEVEL_WARN)         = 2;
39
our ($LOG_LEVEL_NORM)         = 3;
40
our ($LOG_LEVEL_INFO)         = 4;
41
our ($LOG_LEVEL_DEBUG)        = 5;
42
 
43
#------------------------------------------------------------------------------
44
# Constants private to this package
45
#------------------------------------------------------------------------------
46
my ($_LOG_FORMAT)             = "%-7s %s";
47
my ($_LOG_LEVEL)              = $LOG_LEVEL_NORM;
48
 
49
#------------------------------------------------------------------------------
50
sub setLogLevel
51
#
52
# Description:
53
#       This sub-routine is used to set the log level from the calling script.
54
#
55
#------------------------------------------------------------------------------
56
{
57
    # correct number of parameters?
58
    if ( ($#_+1) != 1 )
59
    {
60
        LogError("Incorrect number of params passed to " .
61
                  "setLogLevel() function. " .
62
                  "Check your config.");
63
    }
64
 
65
    my ($logLevel) = shift;
66
 
67
    if ( $logLevel >= $LOG_LEVEL_ERROR && $logLevel <= $LOG_LEVEL_DEBUG )
68
    {
69
        $_LOG_LEVEL = "$logLevel";
70
    }
71
    else
72
    {
73
        $_LOG_LEVEL = $LOG_LEVEL_DEBUG;
74
        LogDebug("Setting log level to max level [$LOG_LEVEL_DEBUG] (Debug).");
75
    }
76
    return 1;
77
}
78
 
79
 
80
#------------------------------------------------------------------------------
81
sub getLogLevel
82
#
83
# Description:
84
#       This sub-routine is used to get the current log level
85
#
86
#------------------------------------------------------------------------------
87
{
88
    return $_LOG_LEVEL;
89
}
90
 
91
 
92
#------------------------------------------------------------------------------
93
sub LogNorm
94
#
95
# Description:
96
#       This sub-routine is used to generate a consistent log message format.
97
#       If 1st param is -n then no \n is printed, the message should be next param
98
#------------------------------------------------------------------------------
99
{
100
    if ( $_LOG_LEVEL >= $LOG_LEVEL_NORM )
101
    {
102
        my ($m_arg) = shift;
103
        my ($term) = "\n";
104
 
105
        if ( $m_arg eq "-n" )
106
        {
107
            $term = "";
108
            $m_arg = shift;
109
        }
110
 
111
        printf(STDERR "$_LOG_FORMAT$term", "[NORM]", $m_arg);
112
    }
113
    return 1;
114
}
115
 
116
 
117
#------------------------------------------------------------------------------
118
sub LogError
119
#
120
# Description:
121
#       This sub-routine is used to generate a consistent log message format.
122
#       if param is -n then no \n is printed
123
#       if param is -x then will not exit
124
#------------------------------------------------------------------------------
125
{
126
    my $exitFlag = 1;
127
 
128
    if ( $_LOG_LEVEL >= $LOG_LEVEL_ERROR )
129
    {
130
        my $term = "\n";
131
        my ($m_arg) = shift;
132
 
133
        while ( $m_arg eq "-n" || $m_arg eq "-x" )
134
        {
135
            $term = "" if ( $m_arg eq "-n" );
136
            $exitFlag = 0 if ( $m_arg eq "-x" );
137
            $m_arg = shift;
138
        }
139
        printf(STDERR "$_LOG_FORMAT$term", "[ERROR]", $m_arg);
140
        printf(STDERR "$_LOG_FORMAT$term", "[ERROR]", "Terminating.") if ( $exitFlag );
141
    }
142
 
143
    # here we must exit unless -x is supplied
144
    exit(1) if ( $exitFlag );
145
    return 1;
146
}
147
 
148
 
149
#------------------------------------------------------------------------------
150
sub LogWarn
151
#
152
# Description:
153
#       This sub-routine is used to generate a consistent log message format.
154
#       If 1st param is -n then no \n is printed, the message should be next param
155
#------------------------------------------------------------------------------
156
{
157
    if ( $_LOG_LEVEL >= $LOG_LEVEL_WARN )
158
    {
159
        my ($m_arg) = shift;
160
        my ($term) = "\n";
161
 
162
        if ( $m_arg eq "-n" )
163
        {
164
            $term = "";
165
            $m_arg = shift;
166
        }
167
 
168
        printf(STDERR "$_LOG_FORMAT$term", "[WARN]", $m_arg);
169
    }
170
    return 1;
171
}
172
 
173
 
174
#------------------------------------------------------------------------------
175
sub LogInfo
176
#
177
# Description:
178
#       This sub-routine is used to generate a consistent informational log 
179
#       message with a predefined format.
180
#       If 1st param is -n then no \n is printed, the message should be next param
181
#------------------------------------------------------------------------------
182
{
183
    if ( $_LOG_LEVEL >= $LOG_LEVEL_INFO )
184
    {
185
        my ($m_arg) = shift;
186
        my ($term) = "\n";
187
 
188
        if ( $m_arg eq "-n" )
189
        {
190
            $term = "";
191
            $m_arg = shift;
192
        }
193
        printf(STDERR "$_LOG_FORMAT$term", "[INFO]", $m_arg);
194
    }
195
    return 1;
196
}
197
 
198
 
199
#------------------------------------------------------------------------------
200
sub LogDebug
201
#
202
# Description:
203
#       This sub-routine is used to generate a consistent debug log 
204
#       message with a predefined format.
205
#       If 1st param is -n then no \n is printed, the message should be next param
206
#------------------------------------------------------------------------------
207
{
208
    if ( $_LOG_LEVEL >= $LOG_LEVEL_DEBUG )
209
    {
210
        my ($m_arg) = shift;
211
        my ($term) = "\n";
212
 
213
        if ( $m_arg eq "-n" )
214
        {
215
            $term = "";
216
            $m_arg = shift;
217
        }
218
        printf(STDERR "$_LOG_FORMAT$term", "[DEBUG]", $m_arg);
219
    }
220
    return 1;
221
}
222
 
223
 
224
#==============================================================================
225
#   LogRaw
226
#
227
# Description
228
#       This sub-routine is used to output raw data passed to logger
229
#==============================================================================
230
sub LogRaw
231
{
232
    printf(STDERR @_);
233
}   # LogRaw
234
1;