Merge branch 'master' of https://github.com/adefaria/clearscm
[clearscm.git] / clients / Ameriquest / triggers / LogActivity.pl
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         LogActivity.pl
5 # Description:  This trigger will log all activity into a "log" file of HTML
6 #               format. Logfiles are kept per day thus the date appears as
7 #               part of their names.
8 #
9 #               This script requires one parameter, which is a path to a
10 #               folder where to store the log files. Generally this is a UNC
11 #               path to an area under some web server's DocumentRoot.
12 #               
13 # Author:       Andrew@DeFaria.com
14 # Created:      May 18, 2004
15 # Language:     Perl
16 # Modifications:
17 #
18 # (c) Copyright 2004, Andrew@DeFaria.com, all rights reserved
19 #
20 ################################################################################
21 use strict;
22 use warnings;
23 use File::Spec;
24
25 # This will be set in the BEGIN block but by putting them here the become
26 # available for the whole script.
27 my (
28   $abs_path,
29   $me,
30   $bin_path,
31   $triggers_path,
32   $lib_path,
33   $log_path,
34   $windows
35 );
36
37 BEGIN {
38   # Extract relative path and basename from script name.
39   $0 =~ /(.*)[\/\\](.*)/;
40
41   $abs_path     = (!defined $1) ? "." : File::Spec->rel2abs ($1);
42   $me           = (!defined $2) ? $0  : $2;
43
44   # Check to see if we are running on Windows
45   $windows      = ($^O =~ /MSWin/) ? "yes" : "no";
46
47   # Setup paths
48   $bin_path             = "$abs_path";
49   $triggers_path        = "$abs_path/../triggers";
50   $lib_path             = "$abs_path/../lib";
51   $log_path             = "$abs_path/../log";
52
53   # Add the appropriate path to our modules to @INC array.
54   unshift (@INC, "$lib_path");
55 } # BEGIN
56
57 use TriggerUtils;
58
59 use Time::localtime;
60
61 if (!defined $ARGV [0]) {
62   clearlogmsg "Must specify a logpath!";
63   exit 1;
64 } # if
65
66 my $logpath = $ARGV [0];
67
68 sub Log {
69   my $logfile   = shift;
70   my $vob       = shift;
71   my $title     = shift;
72   my $time      = shift;
73   my $user      = shift;
74   my $type      = shift;
75   my $action    = shift;
76   my $path      = shift;
77   my $element   = shift;
78   my $version   = shift;
79   my $comments  = shift;
80
81   $logfile = $logpath . "\\" . $logfile;
82
83   if (-e "$logfile") {
84      my $status = open LOG, ">>$logfile";
85
86      if (!defined $status) {
87        clearlogmsg "Unable to open log file $logfile - $!";
88        return 1;
89      } # if
90
91      print LOG <<END;
92 <tr>
93   <td><font size=-1>$time</font></td>
94   <td><font size=-1>$user</font></td>
95   <td><font size=-1>$type</font></td>
96   <td><font size=-1>$action</font></td>
97   <td><font size=-1>$path</font></td>
98   <td><font size=-1>$element</font></td>
99   <td><font size=-1>$version</font></td>
100   <td><font size=-1>$comments</font></td>
101 </tr>
102 END
103   } else {
104      my $status = open LOG, ">>$logfile";
105
106      if (!defined $status) {
107        clearlogmsg "Unable to open log file $logfile - $!";
108        return 1;
109      } # if
110
111      print LOG <<END;
112 <html>
113   <head>
114     <title>$title</title>
115   </head>
116   <body>
117     <h2 align=center>$title</h2>
118     <table align=center border=1 cellspacing=0 cellpadding=2>
119       <tr bgcolor="teal" align="center">
120         <th><font color="white" size=-1>Time</font></th>
121         <th><font color="white" size=-1>User</font></th>
122         <th><font color="white" size=-1>Type</font></th>
123         <th><font color="white" size=-1>Action</font></th>
124         <th><font color="white" size=-1>Path</font></th>
125         <th><font color="white" size=-1>Name</font></th>
126         <th><font color="white" size=-1>Version</font></td>
127         <th><font color="white" size=-1>Comment</font></th>
128       </tr>
129       <tr>
130         <td><font size=-1>$time</font></td>
131         <td><font size=-1>$user</font></td>
132         <td><font size=-1>$type</font></td>
133         <td><font size=-1>$action</font></td>
134         <td><font size=-1>$path</font></td>
135         <td><font size=-1>$element</font></td>
136         <td><font size=-1>$version</font></td>
137         <td><font size=-1>$comments</font></td>
138       </tr>
139 END
140   } # if
141
142   close LOG;
143
144   return 0;
145 } # Log
146
147 # Format $curdate as yyyy-mm-dd and $curtime as hh:mm [AP]m
148 my $time        = localtime;
149 my $year        = $time->year + 1900; 
150 my $month       = ($time->mon < 9)   ? "0" . ($time->mon + 1) : $time->mon + 1;
151 my $day         = ($time->mday < 10) ? "0" . $time->mday      : $time->mday;
152 my $hours       = $time->hour;
153 my $minutes     = ($time->min < 10)  ? "0" . $time->min     : $time->min;
154 my $ampm        = "Am";
155
156 if ($hours > 12) {
157   $ampm = "Pm";
158   $hours -= 12;
159 } elsif ($hours eq 12) {
160   $ampm = "Pm";
161 } # if
162
163 my $curtime = $hours . ":" . $minutes . " $ampm";
164 my $curdate = $year  . "-" . $month   . "-" . $day;
165
166 # Get Clearcase environment variables that we'll need
167 my $type        = $ENV {CLEARCASE_ELTYPE};
168 my $user        = $ENV {CLEARCASE_USER};
169 my $vob         = $ENV {CLEARCASE_VOB_PN};
170
171 # Remove leading "\" from vob, just cause it looks ugly! :-)
172 $vob =~ s/^\\//;
173
174 my $version     = $ENV {CLEARCASE_ID_STR};
175
176 # $version is N/A for mkelem and rmname
177 $version = "<font color=#666666>N/A</font>" if (!defined $version);
178
179 my $comments    = $ENV {CLEARCASE_COMMENT};
180
181 # $comments is N/A for rmname
182 $comments = "<font color=#666666>N/A</font>" if (!defined $comments);
183
184 my $pname       = $ENV {CLEARCASE_PN};
185 my $action      = $ENV {CLEARCASE_OP_KIND};
186
187 my $title = "Activity in vob $vob on $month/$day/$year";
188
189 # Extract element
190 my $element     = substr ($pname, rindex ($pname, "\\") + 1);
191 my $path        = substr ($pname, rindex ($pname, $vob) + length ($vob) + 1);
192
193 $path = ($path eq $element) ? ".\\" : substr ($path,  0, rindex ($path, $element) - 1);
194
195 my $logfile = "${vob}_$curdate.html";
196
197 # Create/Add to HTML logfile.
198 exit (Log $logfile, $vob, $title, $curtime, $user, $type, $action, $path, $element, $version, $comments);