Vugen: Writing To An External Log File

Writing information to the Vugen execution log file is pretty basic stuff, but what happens when you need to write data to an external log file on your the local drive? I can think of a few reasons you might want to do this, but there are a couple of ways I have found using some basic ANSI C code, and I keep this handy in case I ever need it.

This first example prototypes the memset function at the beginning of the init section.
That would look like this:

extern int memset (char *, int, int);

Declare some variables needed at the top of the Action section of the script where the code will be used later:

long stream;
char logdata[80];

Let’s say I wanted to grab the date being shown on a web page (the news page), and I grabbed this as a parameter programatically using the web_reg_save_param function:

web_reg_save_param("pLatestDate",
 "LB=Verdana size=5>2003-",
 "RB=</b>",
 "Ord=1",
 "NotFound=EMPTY",
 LAST);

web_url("news.php",
 "URL=http://www.loadtester.com/news-observations",
 "Resource=0",
 "RecContentType=text/html",
 "Referer=",
 "Snapshot=t1.inf",
 "Mode=HTTP",
 LAST);

Right after that step is where I would use the code to write out this information to a text file called “lrtest.txt”.

if((stream = fopen("c:\\lrtest.txt","a"))== NULL){
 return(-1);
 }
 else {
 sprintf(logdata,"\n%s The latest date for the news page is %s",
     lr_eval_string("{pDate}"), lr_eval_string("{pLatestDate}"));
 fwrite(logdata, sizeof(logdata), 1, stream);
 fclose(stream);
 memset (logdata, 0x00, sizeof (logdata));
 };

Note that I am APPENDING to the file. The \n as the first part of the string I am writing means I am starting at the second line. It looks better.

Here is another way. Create a function called WriteToFile():

WriteToFile(char *message, char *filename )
 
{ long file; //Different from standard C.
 
if (strlen(lr_eval_string(filename)) == 0) {
 filename = "c:\\test\\default.txt";
 }
 if ((file = fopen(lr_eval_string(filename), "a+" )) == NULL) {
 lr_output_message("Unable to create %s", lr_eval_string(filename));
 return -1;
 }
 fprintf(file, "%s\n", lr_eval_string(message));
 fclose(file);
 lr_eval_string_ext_free(message);
 return 0;
 }

 

What's Next?

Did you enjoy this article? Help spread the word by sharing:

Join the Northway Navigator Club today and get access to restricted content including our best tips and tricks. Membership is free! You will also receive free email updates by registering.

Engage in the conversation and leave a comment:

Scott Moore

About Scott Moore (153 articles)

With over 20 years of IT experience with various platforms and technologies, Scott has tested some of the largest applications and infrastructures in the world. He is a Certified Instructor and Certified Product Consultant in HP’s LoadRunner and Performance Center products. He currently holds HP certifications for ASE, ASC, and CI. A thought leader in the APM space, he speaks regularly at IT conferences and events