-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogUtils.pm
More file actions
executable file
·101 lines (101 loc) · 4.93 KB
/
LogUtils.pm
File metadata and controls
executable file
·101 lines (101 loc) · 4.93 KB
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
use strict;
##############################################################################
## Log Utils 0.2.0 #
##############################################################################
## Copyright (C) <2012> <Javier Santillan> #
## #
## This program is free software: you can redistribute it and/or modify #
## it under the terms of the GNU General Public License as published by #
## the Free Software Foundation, either version 3 of the License, or #
## (at your option) any later version. #
## #
## This program is distributed in the hope that it will be useful, #
## but WITHOUT ANY WARRANTY; without even the implied warranty of #
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
## GNU General Public License for more details. #
## #
## You should have received a copy of the GNU General Public License #
## along with this program. If not, see <http://www.gnu.org/licenses/>. #
##############################################################################
package main;
##############################################################################
##############################################################################
## Description: Log message function with tracking support ##
## Author : Javier Santillan ##
## Syntax : logMsgT(FUNCTION_NAME,MESSAGE,CODE,LOG_FILE) ##
## Options : CODE > -1>FATAL_ERROR 0>ERROR 1>WARNING 2>INFO 3>DEBUG ##
##############################################################################
sub logMsgT
{
my ($function_name, $msg_data, $msg_code,$logfile) = @_;
my $msg_prefix;
my $logfile_fh;
if ( $msg_code ==-1 ){$msg_prefix = "FATAL ERROR"; }
elsif ( $msg_code == 0 ){$msg_prefix = "ERROR"; }
elsif ( $msg_code == 1 ){$msg_prefix = "WARNING"; }
elsif ( $msg_code == 2 ){$msg_prefix = "INFO"; }
elsif ( $msg_code == 3 ){$msg_prefix = "DEBUG"; }
else {$msg_prefix = "UNDEFINED"; }
(my $sec,my $min,my $hour,my $day,my $mon,my $year,
my $wday,my $yday,my $isdst)=localtime(time);
my $gdate = sprintf("%4d-%02d-%02dT%02d:%02d:%02d",
$year+1900,$mon+1,$day-1,$hour,$min,$sec);
my $Ddate = sprintf("%4d%02d%02d",$year+1900,$mon+1,$day);
my $Hdate = sprintf("%02d%02d",$hour,$min);
my $msg_timestamp = getTimestamp("localtime","Tdate");
$msg_data = sprintf("[%-.20s]-[%-.11s]-[%.50s]-->[%s]\n",
$msg_timestamp,$msg_prefix,$function_name,$msg_data);
open($logfile_fh,">>$logfile") || print "ERROR, LOGFILE ($logfile) could not be opened\n";
print $logfile_fh "$msg_data";
#print "$msg_data";
close($logfile_fh);
print "$msg_data\n" if ( $msg_code ==-1 );
}
##############################################################################
##############################################################################
## Description: Returns function_caller + function_name for tracking ##
## Author : Javier Santillan ##
## Syntax : getFunctionName(CURRENT_FUNCTION_CALLER,FUNCTION_NAME) ##
##############################################################################
sub getFunctionName
{
my ($current_function_caller,$function_name) = @_;
my @names = split(/::/,$function_name);
return "$current_function_caller|$names[1]";
}
##############################################################################
##############################################################################
## Description: Gets the timestamp of current time ##
## Author : Javier Santillan ##
## Syntax : getTimestamp(TIME_ZONE,TYPE_REQUEST) ##
## Options : TIME_ZONE > localtime, utc ##
## Options : TYPE_REQUEST > TDate, Ddate, Hdate ##
##############################################################################
sub getTimestamp
{
my ($time_zone, $time_request) = @_;
my ($sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst);
if ( $time_zone eq "localtime" )
{
($sec, $min, $hour, $day, $mon, $year,
$wday, $yday, $isdst)=localtime(time);
}
elsif ($time_zone eq "utc")
{
($sec, $min, $hour, $day, $mon, $year,
$wday, $yday, $isdst)=gmtime(time);
}
else
{
($sec, $min, $hour, $day, $mon, $year,
$wday, $yday, $isdst)=localtime(time);
}
my $Tdate = sprintf("%4d-%02d-%02dT%02d:%02d:%02d",
$year+1900,$mon+1,$day-1,$hour,$min,$sec);
my $Ddate = sprintf("%4d%02d%02d",$year+1900,$mon+1,$day);
my $Hdate = sprintf("%02d%02d",$hour,$min);
return $Tdate if ($time_request eq "Tdate");
return $Ddate if ($time_request eq "Ddate");
return $Hdate if ($time_request eq "Hdate");
}
1;