1 | # ------------------------------------------------------------------------------ |
---|
2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | # ------------------------------------------------------------------------------ |
---|
19 | # NAME |
---|
20 | # FCM1::Timer |
---|
21 | # |
---|
22 | # DESCRIPTION |
---|
23 | # This is a package of timer utility used by the FCM command. |
---|
24 | # |
---|
25 | # ------------------------------------------------------------------------------ |
---|
26 | |
---|
27 | package FCM1::Timer; |
---|
28 | |
---|
29 | # Standard pragma |
---|
30 | use warnings; |
---|
31 | use strict; |
---|
32 | |
---|
33 | # Exports |
---|
34 | our (@ISA, @EXPORT, @EXPORT_OK); |
---|
35 | |
---|
36 | sub timestamp_command; |
---|
37 | |
---|
38 | require Exporter; |
---|
39 | @ISA = qw(Exporter); |
---|
40 | @EXPORT = qw(timestamp_command); |
---|
41 | |
---|
42 | # ------------------------------------------------------------------------------ |
---|
43 | |
---|
44 | # Module level variables |
---|
45 | my %cmd_start_time = (); # Command start time, (key = command, value = time) |
---|
46 | |
---|
47 | # ------------------------------------------------------------------------------ |
---|
48 | # SYNOPSIS |
---|
49 | # $string = &FCM1::Timer::timestamp_command ($command[, $status]); |
---|
50 | # |
---|
51 | # DESCRIPTION |
---|
52 | # This function returns a string adding to $command a prefix according the |
---|
53 | # value of $status. If $status is not specified or does not match the word |
---|
54 | # "end", the status is assumed to be "start". At "start", the prefix will |
---|
55 | # contain the current timestamp. If $status is the word "end", the prefix |
---|
56 | # will contain the total time taken since this function was called with the |
---|
57 | # same $command at the "start" status. |
---|
58 | # ------------------------------------------------------------------------------ |
---|
59 | |
---|
60 | sub timestamp_command { |
---|
61 | (my $command, my $status) = @_; |
---|
62 | |
---|
63 | my $prefix; |
---|
64 | if ($status and $status =~ /end/i) { |
---|
65 | # Status is "end", insert time taken |
---|
66 | my $lapse = time () - $cmd_start_time{$command}; |
---|
67 | $prefix = sprintf "# Time taken: %12d s=> ", $lapse; |
---|
68 | |
---|
69 | } else { |
---|
70 | # Status is "start", insert time stamp |
---|
71 | $cmd_start_time{$command} = time; |
---|
72 | |
---|
73 | (my $sec, my $min, my $hour, my $mday, my $mon, my $year) = localtime; |
---|
74 | $prefix = sprintf "# Start: %04d-%02d-%02d %02d:%02d:%02d=> ", |
---|
75 | $year + 1900, $mon + 1, $mday, $hour, $min, $sec; |
---|
76 | } |
---|
77 | |
---|
78 | return $prefix . $command . "\n"; |
---|
79 | } |
---|
80 | |
---|
81 | # ------------------------------------------------------------------------------ |
---|
82 | |
---|
83 | 1; |
---|
84 | |
---|
85 | __END__ |
---|