Browse Source

Added htlogger back+front-end; very primitive remote logging mechanism.

Alois Mahdal 12 years ago
parent
commit
bd192cf942
2 changed files with 97 additions and 0 deletions
  1. 46
    0
      cgi-bin/htlog/htlog.cgi
  2. 51
    0
      lib/htlogr.pm

+ 46
- 0
cgi-bin/htlog/htlog.cgi View File

@@ -0,0 +1,46 @@
1
+#!/usr/bin/perl -w
2
+
3
+## Author: Alois Mahdal at zxcvb cz
4
+# Back-end for very primitive remote logging. Front-end is htlogr.pm
5
+
6
+# This program 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
+# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18
+
19
+use CGI;
20
+
21
+my $LOG_FILE = 'htlog.log';
22
+open my $fh, ">>", $LOG_FILE or die "cannot open log file for appending: $!";
23
+
24
+my $q = CGI->new;
25
+my $msg = ( defined $q->param('msg')    ? $q->param('msg')  : '' );
26
+my $tag = ( defined $q->param('tag')    ? $q->param('tag')  : '-none-' );
27
+
28
+my $message = sprintf("Time: %s, Origin: %s, Tag: %s, Message: %s\n",
29
+    time,
30
+    $ENV{'REMOTE_ADDR'},
31
+    $tag,
32
+    $msg
33
+);
34
+
35
+
36
+print $fh $message;
37
+
38
+print $q->header(
39
+    -type               => 'text/javascript',
40
+    -expires            => 'now',
41
+);
42
+
43
+print "Message logged: $msg\n";
44
+
45
+close $fh or die "cannot close log file: $!";
46
+

+ 51
- 0
lib/htlogr.pm View File

@@ -0,0 +1,51 @@
1
+package htlogr;
2
+
3
+## Author: Alois Mahdal at zxcvb cz
4
+# Front-end for very primitive remote logging service. Use htlog.cgi
5
+# as back-end: put it on a HTTP server and provide URL as "path" option
6
+# when instantiating this class
7
+
8
+# This program is free software: you can redistribute it and/or modify
9
+# it under the terms of the GNU General Public License as published by
10
+# the Free Software Foundation, either version 3 of the License, or
11
+# (at your option) any later version.
12
+
13
+# This program is distributed in the hope that it will be useful,
14
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
+# GNU General Public License for more details.
17
+
18
+# You should have received a copy of the GNU General Public License
19
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
+
21
+
22
+use HTTP::Lite;
23
+use URI::Escape;
24
+use Carp;
25
+
26
+sub new {
27
+    my ($class, $opts)  = @_;
28
+    my $self            = {};
29
+
30
+    foreach (qw/ path /) {
31
+        $self->{$_} = $opts->{$_} or croak("missing mandatory option: $_");
32
+    }
33
+
34
+    $self->{http} = HTTP::Lite->new;
35
+
36
+    return bless $self, $class;
37
+}
38
+
39
+sub log {
40
+    my $self    = shift;
41
+    my $msg     = shift;
42
+    my $tag     = shift;
43
+    my $uri;
44
+    $uri .= $self->{path};
45
+    $uri .= "?msg=" . uri_escape($msg);
46
+    $uri .= "&tag=" . uri_escape($tag)    if $tag;
47
+    $self->{http}->request($uri) or croak("could not htlog message: $!");
48
+    return $self->{http}->body();
49
+}
50
+
51
+1;