package store;

use warnings;
use strict;

use POSIX;

use CouchDB;

=for MongoDB

use MongoDB;
my $conn = MongoDB::Connection->new;
my $db = $conn->get_database( 'pxelator' );
our $audit = $db->get_collection( 'audit' );

=cut

sub audit {
	my $data = pop @_;

	my $url = join(' ', @_);
	$url =~ s/\s+-\S+//g;	# remove command line options
	$url =~ s/\W+/-/g;

	my $time = time();

	my @caller = caller(0);
	$caller[3] = (caller(1))[3];
	$caller[3] =~ s{^.+::}{}; # stip package name from sub
	$data->{package} = {
		time => $time,
		name => $caller[0],
		line => $caller[2],
		caller  => $caller[3],
	};

	if ( $ENV{DEBUG} ) {

		my $caller;
		my $depth = 0;
		while ( my @c = caller($depth) ) {
			push @$caller, [ @c ];
			$depth++;
		}

		$data->{caller} = $caller;

	}

#	carp 'audit ', dump($data);

#	$time = int($time); # reduce granularity for url
	$time = strftime("%Y-%m-%d.%H:%M:%S", localtime $time);
	my $package = $caller[0];

	CouchDB::_store_audit( "$time.$package.$url" => $data );

#	$data->{_id} = "$time.$package.$url"; $audit->insert( $data );
}


1;
