NAME

SmartLog - A Smart log that is either limited (circular) or of unspecified length (unbounded, not circular).

SYNOPSIS

use SmartLog;

# instantiate
my $log=SmartLog->new();
# add a log entry
$log->add("message",time());
# start iteration over log elements in forward order
$log->resetNext();
# iterate
while (my @data=$log->getNext()) {
   my $message=$data[0];
   my $time=$data[1];

   print "$time: $message\n";
}
# start iteration over log elements in reverse order
$log->resetNextReverse();
# get first N log entry(ies)
my $items=$log->getFirst (10);
# get last N log entry(ies);
my $items=$log->getLast (11);
# get first N log entry(ies) as a string
my $str=$log->getFirstAsString (10);
# get last N log entry(ies) as a string
my $str=$log->getLastAsString (11,"%m ");
# convert a log entry to a string in given format
my $str=$log->entryAsString($entry,"%m (%t)");
# reset the log instance and its contents
$log->reset();

DESCRIPTION

A pseudo circular log that is either limited (circular) or of unspecified length (unbounded, not circular in effect). In other words it is a circular list where the size is optionally set.

CONSTRUCTOR

new()

Instantiates the class.

No input is required, but accepts the following parameter:

Returns the instantiated class.

METHODS

add()

Adds a log entry to the instance.

Accepts these parameters in the following order:

Returns 1 upon completion (always successful).

resetNext()

Resets the iteration over the items in the circular log. It resets a pointer to where it is in the log to the first and oldest item (tail).

resetNextReverse()

Resets the iteration over items in the circular log. It sets the pointer to the head of the log and marks the iteration to go in reverse.

getNext()

Gets the next item in the circular log. If resetNext() or resetNextReverse() have not been called at any time since instantiation or a call to the reset()-method, it will call resetNext() for you (it favorizes a forward direction iteration.

Returns a LIST-reference upon success. The LIST-structure is as follows:

[ MESSAGE. TIME ]

So that the log item message comes first and then the timestamp of the message.

Returns undef upon failure (no items or no more items).

getFirst()

Get first N log entries as a list pointer.

It accepts one input and that is the number of entries to return. This parameters is optional and if none is given it will default to 1.

Remember that the method always resets the getNext()-counter like when calling the resetNext()-method.

It returns a LIST-reference upon success, undef if no entries.

The LIST-reference structure is like this:

[ [message,time],
  [message,time],
  [message,time],
]

getLast()

Get last N log entries as a list pointer.

It accepts one input and that is the number of entries to return. This parameters is optional and if none is given it will default to 1.

Remember that the method always resets the getNext()-counter like when calling the resetNextReverse()-method.

It returns a LIST-reference upon success, undef if no entries.

The LIST-reference structure is like this:

[ [message,time],
  [message,time],
  [message,time],
]

The order of the list will be in first to last, so the last log message of the log will come last in the list.

getFirstAsString()

Gets the first N log entries as a string.

It accepts two input parameters. The first is the number of log entries to return, the second is the format of the string to return them in. Both parameters are optional and will default to 1 and "time: message " respectively.

For the structure of the format-parameter see the entryAsString()-method.

The returned string will be a concatenation of all the log entries in the given format, so remember to add some kind of spacing at the end (between each entry in other words).

It returns the string of the log entries upon success, or undef if no log entries could be retrieved.

getLastAsString()

Gets the last N log entries as a string.

It accepts two input parameters. The first is the number of log entries to return, the second is the format of the string to return them in. Both parameters are optional and will default to 1 and "time: message " respectively.

For the structure of the format-parameter see the entryAsString()-method.

The returned string will be a concatenation of all the log entries in the given format, so remember to add some kind of spacing at the end (between each entry in other words).

It returns the string of the log entries upon success, or undef if no log entries could be retrieved.

entryAsString()

Convert a single log entry into a string of given format.

It accepts two parameters in the following order: entry, format.

Entry is an ARRAY-reference to the log entry in question. Format is the string format to use when converting the entry. Format is optional and will default to "%t: %m".

The format can be specified as a string in the following way:

"Time: (%t) Message: (%m)"

Where %t is designated as the time part of the log entry and %m as the message part. You can repeat %t and %m as many times as you like in the string or omit one or both (if you like to shoot yourself in the leg, go ahead).

It returns the converted string upon success or undef upon failure (missing or wrong entry format).

reset()

Reset the log entries and getNext()-pointers so that the log entry collection can start anew.

getLog()

Returns a copy of the log HASH at that moment in time.

No input is accepted.

Return value is a HASH of the entire log-data, including circular list pointers, head- and tail positions etc.

updated()

Returns the timestamp of the last log entry update (head) or undef if no updates yet.

count()

Returns the number of log entries.

error()

Returns the last error message of the log instance.