Back to Blog

LoadRunner Utility Code: Lance’s Log

Several years back, I found this posting and thought it was interesting. I am just now getting around to sharing it. Try this code if you want. I use this LoadRunner utility code to assist in analyzing test data, or to write desired dynamic data from scripts to .csv files outside of LoadRunner scenario results folders.

This code requires some script parameters to be defined, and you have to pre-allocate your folder where you want to write your data. The code also checks to see if you are running from Vugen or the Controller. If running out of Vugen, it writes the logs to the script’s results folder using a nice name that will sort to the top of the results folder. If you run out of the controller, it writes to the pre-allocated folder, getting the path via the parameter file I call MyLog.dat:

  1. Make sure you have created the folder path
  2. Make sure you can write to the folder from the generator via mapped drive letter.
  3. Create the following script parameters:
  • parmname=”time” format: “%m%d%y,%H%M$S”  This is a custom date/time format with a comma embedded in it. This is used when writing timestamped rows of data to your log. be sure to set “Update each Occurrence”. I assume you already know how to create a custom Date/Time format.
  • parmname=”fname” format: “%m%d%y_%H%M$S” used to create your log file name in the folder you pre-created. Be sure to set “Update Once”
  • parmname=”iter” type iteration parm format “%02d”

Create parameter file (ie .dat) – “MyLog.dat” (or something like it). The value of the parameter in file will be the full path to the folder create in step 1. Use a parameter name like “LogLoc”.

For example:


NOTE: The Q drive is a mapped network drive on the generator.

Create the script parameter parmname=”LogLoc” pointing to MyLog.dat to pick up the path

See the whole example below. You can stick the WriteToLog function in your scripts.  I use an include because I feel it is a cleaner method.

#include "web_api.h"
 int WriteToLanceLog(void)

extern char* strtok(char *token, const char *delimiter);
extern char *strcat ( char *to, const char *from );
extern void lr_whoami (int *vuser_id, char **sgroup, int *scid);

char *scriptpath, *last, *scriptname ;

long logHndl;
int id;
char *vuser_group;
char id_string[3];
static char filepath[1024];

// first time stuff - create a new log file
 if ( atoi(lr_eval_string("{iter}")) == 1) {

// build file name
 lr_whoami(&id, &vuser_group, NULL); // only good in controller mode
 lr_log_message( "*****vuser_group: %s, vuser id: %d, ",
 vuser_group, id );

 // if a VUgen run, leave the log in VUgen directory
 if ( strcmp(vuser_group,"None") == 0) {
 scriptpath = lr_get_attrib_string("usr");
 lr_output_message("%s", scriptpath);
 last = (char*)strrchr(scriptpath, '\\');
 lr_output_message("%s", last);
 scriptname = (char *)strtok(last+1, ".");
 lr_output_message("%s", scriptname);
 strcat(filepath, "@");
 strcat(filepath, scriptname);
 lr_log_message("filepath=%s", filepath);

strcat (filepath, lr_eval_string("_{fname}") );

strcat (filepath, ".csv" ); // make it a csv file
lr_log_message("******filepath=%s", filepath);

lr_save_var( filepath, strlen(filepath), 0, "MyLog");
lr_log_message("%s", lr_eval_string("{MyLog}") );

} // if ( strcmp(vuser_group,"None") == 0)

else { // its running out of controller

// strcat (filepath, "@");
// get MH log folder location via parameter
strcat (filepath, lr_eval_string("{LogLoc}") );
lr_log_message("filepath=%s", filepath);
strcat (filepath, vuser_group);
itoa(id, id_string, 10);
strcat (filepath, "_");
lr_output_message("%s", id_string);
strcat (filepath, id_string);
strcat (filepath, lr_eval_string("_{fname}") );
strcat (filepath, ".csv" ); // make it a csv file
lr_output_message("filepath=%s", filepath);

// else
// end build file name

logHndl = fopen(filepath, "a"); // write header record in log

lr_message("logHndl=%d", logHndl);
if (logHndl == NULL) {
lr_error_message("Iteration: %s Unable to create/access %s",
     lr_eval_string("{iter}"), filepath);
return -1;
} //if (logHndl == NULL)

fprintf(logHndl, "Date,Time,Iter,MyData\n");
} // if ( atoi(lr_eval_string("{iter}")) == 1)

// This is the normal write routine, after the initial setup above
logHndl = fopen(filepath, "a"); // open log

//lr_message("logHndl=%d", logHndl);

if (logHndl == NULL) {
 lr_error_message("Iteration: %s Unable to create/access %s",
     lr_eval_string("{iter}"), filepath);
 return -1;
 } //if (logHndl == NULL)

// now write the data
fprintf(logHndl, "%s,%s,%s\n",
"WhateverDataNeedsToBeWrittenGoesHere" );


return 0;

// end WriteToLog

Now here is what the Action section would look like:

return 0;


Back to Blog