Studio:Log.pm: Difference between revisions
m (Text replace - 'Category: Libraries' to 'Category:Studio:Libraries') |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 57: | Line 57: | ||
my $log = new Log; | my $log = new S2S::Log; | ||
$log->addStudio(); # accept defaults | $log->addStudio(); # accept defaults | ||
Line 63: | Line 63: | ||
$log->addFile(); # accept defaults | $log->addFile(); # accept defaults | ||
$log->addBCMessage( | $log->addBCMessage(messageOwner => $logMsg); | ||
); | |||
$log->addTestCase( | $log->addTestCase(testCase => $tc); | ||
); | |||
$log->addAnnotation( | $log->addAnnotation(testSuite => $main::testSuite); | ||
); | |||
$log->debug('this is a debug test message'); | $log->debug('this is a debug test message'); | ||
$log->info('this is an info test message'); | |||
$log->warning('this is a warning test message'); | $log->warning('this is a warning test message'); | ||
$log->error('this is an error test message'); | $log->error('this is an error test message'); | ||
Line 89: | Line 78: | ||
$log->debug('this is another debug test message: %x and %e ', $number, $number); | $log->debug('this is another debug test message: %x and %e ', $number, $number); | ||
$log->info('this is another info test message'); | |||
$log->warning('this is another warning test message'); | $log->warning('this is another warning test message'); | ||
$log->error('this is another error test message'); | $log->error('this is another error test message'); | ||
Line 134: | Line 124: | ||
*A test suite annotation | *A test suite annotation | ||
*A broadcast message | *A broadcast message | ||
*The console window (when script is running from the command line) | |||
== Message Levels == | == Message Levels == | ||
Line 147: | Line 138: | ||
From lowest to highest, the module defines the following levels: | From lowest to highest, the module defines the following levels: | ||
* DEBUG | * DEBUG | ||
* INFO | |||
* WARNING | * WARNING | ||
* ERROR | * ERROR | ||
Line 163: | Line 154: | ||
This module derives from from '''Log::Handler'''. If you need advanced functionality, | This module derives from from '''Log::Handler'''. If you need advanced functionality, | ||
please see [[http://search.cpan.org/~bloonix/Log-Handler-0.49/lib/Log/Handler.pm]] | please see [[http://search.cpan.org/~bloonix/Log-Handler-0.49/lib/Log/Handler.pm]] | ||
= Functions = | = Functions = | ||
Line 183: | Line 172: | ||
<source lang="perl"> | <source lang="perl"> | ||
$log->addStudio( | $log->addStudio(level => 'error'); | ||
</source> | </source> | ||
Line 191: | Line 180: | ||
;<code>level</code> key | ;<code>level</code> key | ||
:Determines minimum logging level for this output. Valid values are 'debug', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | :Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | ||
== addFile() == | == addFile() == | ||
Line 199: | Line 188: | ||
<source lang="perl"> | <source lang="perl"> | ||
$log->addFile( | $log->addFile(level => 'error', filename => "C:\\myLog\\mylog.txt"); | ||
</source> | </source> | ||
Line 207: | Line 196: | ||
;<code>level</code> key | ;<code>level</code> key | ||
:Determines minimum logging level for this output. Valid values are 'debug', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | :Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | ||
;<code>filename</code> key | ;<code>filename</code> key | ||
Line 219: | Line 208: | ||
# a broadcast message has been captured with the name 'logMSG' | # a broadcast message has been captured with the name 'logMSG' | ||
my $logMsg = $main::ascript->Messages->Item("logMSG")->Owner; | my $logMsg = $main::ascript->Messages->Item("logMSG")->Owner; | ||
$log->addBCMessage( | $log->addBCMessage(level => 'error', messageOwner => $logMSG); | ||
</source> | </source> | ||
Line 227: | Line 216: | ||
;<code>level</code> key | ;<code>level</code> key | ||
:Determines minimum logging level for this output. Valid values are 'debug', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | :Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | ||
;<code>messageOwner</code> key | ;<code>messageOwner</code> key | ||
Line 264: | Line 253: | ||
<source lang="perl"> | <source lang="perl"> | ||
# $tc is the test case to be logged to | # $tc is the test case to be logged to | ||
$log->addTestCast( | $log->addTestCast(level => 'error', testCase => $tc);> | ||
</source> | </source> | ||
Line 272: | Line 261: | ||
;<code>level</code> key | ;<code>level</code> key | ||
:Determines minimum logging level for this output. Valid values are 'debug', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | :Determines minimum logging level for this output. Valid values are 'debug', info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | ||
;<code>testCase</code> key | ;<code>testCase</code> key | ||
Line 283: | Line 272: | ||
<source lang="perl"> | <source lang="perl"> | ||
# $ts is the test suite to be logged to | # $ts is the test suite to be logged to | ||
$log->addAnnotation( | $log->addAnnotation(level => 'error', testSuite => $ts);> | ||
</source> | </source> | ||
Line 291: | Line 280: | ||
;<code>level</code> key | ;<code>level</code> key | ||
:Determines minimum logging level for this output. Valid values are 'debug', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | :Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | ||
;<code>testSuite</code> key | ;<code>testSuite</code> key | ||
Line 297: | Line 286: | ||
If the annotation does not exist, then it is created on first write. Upon creation, the comment label is set to: "[LEVEL] Log Message" (where LEVEL is one of: DEBUG, WARNING, ERROR, FATAL) and the annotation level is set to correspond to the level indicated in the label (note that WARNING is mapped to the ''warn'' level.). | If the annotation does not exist, then it is created on first write. Upon creation, the comment label is set to: "[LEVEL] Log Message" (where LEVEL is one of: DEBUG, WARNING, ERROR, FATAL) and the annotation level is set to correspond to the level indicated in the label (note that WARNING is mapped to the ''warn'' level.). | ||
== addScreen() == | |||
Adds the console window as an output of this logger. The output will be seen only if the script using the logger was invoked from the command line. | |||
<source lang="perl"> | |||
$log->addScreen(level => 'error', logTo => 'STDERR');> | |||
</source> | |||
'''arg1: <code>$params</code> ''(Optional)''''' | |||
An anonymous hash; string values at keys are as follows: | |||
;<code>level</code> key | |||
:Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged). | |||
;<code>logTo</code> key | |||
:Sets the output stream to be logged to. Valid values are 'STDOUT' and 'STDERR'. Defaults to 'STDOUT' if omitted. | |||
== debug() == | == debug() == | ||
Line 303: | Line 313: | ||
<source lang="perl"> | <source lang="perl"> | ||
# $tc is the test case to be logged to | # $tc is the test case to be logged to | ||
$log- | $log->debug("this is a debug message"); | ||
my $number = 32767; | |||
$log->debug("you can also use sprintf: %x %e ", $number, $number); | |||
</source> | |||
'''arg1: <code>$msg</code>''' | |||
String to be logged. | |||
Note that the argument(s) you supply here are passed to sprintf, so you can supply | |||
arguments as FORMAT, LIST if desired. (Useful for number formatting, changing radix, | |||
etc.) See [[http://perldoc.perl.org/functions/sprintf.html]] | |||
== info() == | |||
Writes a message with the INFO level to all added outputs. | |||
<source lang="perl"> | |||
$log->info("this is an info message"); | |||
my $number = 32767; | my $number = 32767; | ||
$log- | $log->info("you can also use sprintf: %x %e ", $number, $number); | ||
</source> | </source> | ||
Line 375: | Line 402: | ||
[[Category: Libraries]] | [[Category:Studio:Libraries]] |
Latest revision as of 23:26, 20 August 2009
This package provides standard multi-output logging functionality for all STRIDE perl scripts.
Example
use strict;
use warnings;
use Carp;
use Win32::TieRegistry(Delimiter=>"/");
use File::Spec;
use Time::HiRes qw (usleep gettimeofday tv_interval sleep);
use Win32::OLE;
Win32::OLE->Option(Warn => 3);
# setup the lib path
use vars qw(
$studio
$testSuite
$ascript
$StrideDir
$StrideLibDir
);
BEGIN {
$StrideDir =
$Registry->{"LMachine/SOFTWARE/S2 Technologies/STRIDE/InstallDir"} ||
$ENV{'STRIDE_DIR'} ||
'C:\\STRIDE';
$StrideLibDir = File::Spec->catdir($StrideDir, 'lib', 'perl');
}
###############################
# Imported Libs
###############################
use lib $StrideLibDir; # Use the above calculated path to finf the lib's
use S2S::Log;
# create a test case to which comments will be added
$main::testSuite->Tests->RemoveAll();
my $tcName = "Sample Test Case";
my $tc = $main::testSuite->Tests->Add($tcName);
my $logMsg = $main::ascript->Messages->Item("logMSG")->Owner;
# set up a trace view to capture broadcast log messages
my $tvName = 'Log Viewer';
my $tv = $main::studio->Workspace->TraceViews->Item($tvName);
if (!$tv) {
$tv = $main::studio->Workspace->TraceViews->Add("HostTraceView");
$tv->{Name} = $tvName;
}
$tv->Open();
my $tvFilter =$tv->Filters->InterfacesByUniqueId->Item($logMsg->Name);
$tvFilter->{Data} = 1;
$tvFilter->{On} = 1;
$tv->Filters->InterfacesByUniqueId->Apply();
my $log = new S2S::Log;
$log->addStudio(); # accept defaults
$log->addFile(); # accept defaults
$log->addBCMessage(messageOwner => $logMsg);
$log->addTestCase(testCase => $tc);
$log->addAnnotation(testSuite => $main::testSuite);
$log->debug('this is a debug test message');
$log->info('this is an info test message');
$log->warning('this is a warning test message');
$log->error('this is an error test message');
$log->fatal('this is a fatal test message');
my $number = 12345;
$log->debug('this is another debug test message: %x and %e ', $number, $number);
$log->info('this is another info test message');
$log->warning('this is another warning test message');
$log->error('this is another error test message');
$log->fatal('this is another fatal test message');
$tc->{Status} = "PASS";
Example .h File to Define Broadcast Message Output
#include <sr.h>
#ifdef _UNICODE
typedef short TCHAR;
#else
typedef char TCHAR ;
#endif
#define logMSG 11758 | srMT_BRD
#define MAX_LOGMSG 256
typedef struct
{
TCHAR* pMsg;
}LOGMSG_t;
#ifdef _SCL
#pragma scl_msg(logMSG, LOGMSG_t)
#pragma scl_string(LOGMSG_t.pMsg, MAX_LOGMSG)
#endif /* _SCL */
Description
This module is used for all logging from STRIDE test scripts.
The module allows logging to one or many output locations at once. The supported output locations are as follows:
- The STRIDE Studio message window
- A disk file
- A test case comment
- A test suite annotation
- A broadcast message
- The console window (when script is running from the command line)
Message Levels
You assign a level to each message you log. This provides a simple filtering mechanism as each output can optionally be configured to log only those messages that exceed a specified level.
For example, you might want to log only ERROR and FATAL messages to the STRIDE Studio message window, thus ignoring DEBUG and WARNING messages.
From lowest to highest, the module defines the following levels:
- DEBUG
- INFO
- WARNING
- ERROR
- FATAL
To use the module, you do the following:
- Create an instance of the S2S::Log object
- Add the outputs that you want; you may supply operational parameters to customize the outputs as well
- Write log messages to the log object
Note
This module derives from from Log::Handler. If you need advanced functionality, please see [[1]]
Functions
new()
Creates and returns an instance of the logger.
my $log = new S2S::Log;
addStudio()
Adds the STRIDE Studio message window as an output of this logger.
$log->addStudio(level => 'error');
arg1: $params
(optional)
An anonymous hash; string values at keys are as follows:
level
key- Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged).
addFile()
Adds a disk file as an output of this logger.
$log->addFile(level => 'error', filename => "C:\\myLog\\mylog.txt");
arg1: $params
(optional)
An anonymous hash; string values at keys are as follows:
level
key- Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged).
filename
key- Sets the output filename for this output. A complete path is required. If the specified folder does not exist, it is created. If the output file already exists, subsequent output is appended. Defaults to
%WORKSPACE_DIR%\output.log
if omitted.
addBCMessage()
Adds a STRIDE broadcast message as an output of this logger.
# a broadcast message has been captured with the name 'logMSG'
my $logMsg = $main::ascript->Messages->Item("logMSG")->Owner;
$log->addBCMessage(level => 'error', messageOwner => $logMSG);
arg1: $params
(NOT optional; the messageOwner must be specified)
An anonymous hash; string values at keys are as follows:
level
key- Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged).
messageOwner
key- Sets the broadcast message owner for this output. An instance of a message owner object is required. The broadcast message must have a payload member named
pMsg
to which the message string is written.
For example:
#include <sr.h>
#ifdef _UNICODE
typedef short TCHAR;
#else
typedef char TCHAR ;
#endif
#define logMSG 11758 | srMT_BRD
#define MAX_LOGMSG 256
typedef struct
{
TCHAR* pMsg;
}LOGMSG_t;
#ifdef _SCL
#pragma scl_msg(logMSG, LOGMSG_t)
#pragma scl_string(LOGMSG_t.pMsg, MAX_LOGMSG)
#endif /* _SCL */
addTestCase()
Adds a STRIDE test case as an output of this logger.
# $tc is the test case to be logged to
$log->addTestCast(level => 'error', testCase => $tc);>
arg1: $params
(NOT optional; testCase must be specified)
An anonymous hash; string values at keys are as follows:
level
key- Determines minimum logging level for this output. Valid values are 'debug', info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged).
testCase
key- Sets the output test case for this output. Logged messages are added as comments to the specified test case. The comment label is set to: "[LEVEL] Log Message" where LEVEL is one of: DEBUG, WARNING, ERROR, FATAL.
addAnnotation()
Adds a STRIDE test suite annotation as an output of this logger.
# $ts is the test suite to be logged to
$log->addAnnotation(level => 'error', testSuite => $ts);>
arg1: $params
(NOT optional; testSuite must be specified)
An anonymous hash; string values at keys are as follows:
level
key- Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged).
testSuite
key- Sets the output test suite for this output. Logged messages are added as comments to the suite's annotation with the name "[LEVEL] Log Message", where LEVEL is one of DEBUG, WARNING, ERROR, FATAL.
If the annotation does not exist, then it is created on first write. Upon creation, the comment label is set to: "[LEVEL] Log Message" (where LEVEL is one of: DEBUG, WARNING, ERROR, FATAL) and the annotation level is set to correspond to the level indicated in the label (note that WARNING is mapped to the warn level.).
addScreen()
Adds the console window as an output of this logger. The output will be seen only if the script using the logger was invoked from the command line.
$log->addScreen(level => 'error', logTo => 'STDERR');>
arg1: $params
(Optional)
An anonymous hash; string values at keys are as follows:
level
key- Determines minimum logging level for this output. Valid values are 'debug', 'info', 'warning', 'error', and 'fatal'. Defaults to 'debug' if omitted (all messages logged).
logTo
key- Sets the output stream to be logged to. Valid values are 'STDOUT' and 'STDERR'. Defaults to 'STDOUT' if omitted.
debug()
Writes a message with the DEBUG level to all added outputs.
# $tc is the test case to be logged to
$log->debug("this is a debug message");
my $number = 32767;
$log->debug("you can also use sprintf: %x %e ", $number, $number);
arg1: $msg
String to be logged.
Note that the argument(s) you supply here are passed to sprintf, so you can supply arguments as FORMAT, LIST if desired. (Useful for number formatting, changing radix, etc.) See [[2]]
info()
Writes a message with the INFO level to all added outputs.
$log->info("this is an info message");
my $number = 32767;
$log->info("you can also use sprintf: %x %e ", $number, $number);
arg1: $msg
String to be logged.
Note that the argument(s) you supply here are passed to sprintf, so you can supply arguments as FORMAT, LIST if desired. (Useful for number formatting, changing radix, etc.) See [[3]]
warning()
Writes a log message with the WARNING level to all added outputs..
# $tc is the test case to be logged to
$log->warning("this is a debug message");
my $number = 32767;
$log->warning("you can also use sprintf: %x %e ", $number, $number);
arg1: $msg
String to be logged.
Note that the argument(s) you supply here are passed to sprintf, so you can supply arguments as FORMAT, LIST if desired. (Useful for number formatting, changing radix, etc.) See [[4]]
error()
Writes a log message with the ERROR level to all added outputs..
# $tc is the test case to be logged to
$log->error("this is a debug message");
my $number = 32767;
$log->error("you can also use sprintf: %x %e ", $number, $number);
arg1: $msg
String to be logged.
Note that the argument(s) you supply here are passed to sprintf, so you can supply arguments as FORMAT, LIST if desired. (Useful for number formatting, changing radix, etc.) See [[5]]
fatal()
Writes a log message with the FATAL level to all added outputs..
# $tc is the test case to be logged to
$log->fatal("this is a debug message");
my $number = 32767;
$log->fatal("you can also use sprintf: %x %e ", $number, $number);
arg1: $msg
String to be logged.
Note that the argument(s) you supply here are passed to sprintf, so you can supply arguments as FORMAT, LIST if desired. (Useful for number formatting, changing radix, etc.) See [[6]]