contrib/mul/mbl/mbl_test.cxx
Go to the documentation of this file.
00001 #include "mbl_test.h"
00002 //:
00003 // \file
00004 // \brief A place for useful things associated with testing.
00005 // \author iscott
00006 // \date  Aug 2005
00007 
00008 #include <vcl_cerrno.h>
00009 #include <vcl_string.h>
00010 #include <vcl_fstream.h>
00011 #include <vcl_ctime.h>
00012 #include <vcl_cstdlib.h>
00013 #include <vcl_iomanip.h>
00014 #include <vul/vul_file.h>
00015 #include <vul/vul_expand_path.h>
00016 #include <mbl/mbl_config.h>
00017 
00018 
00019 //: replace instances of 'from' in 's' with 'to'
00020 static unsigned replace(char from, char to, vcl_string &s)
00021 {
00022   unsigned c = 0;
00023   for (unsigned i=0; i<s.size(); ++i)
00024     if (s[i] == from)
00025     {
00026       c++;
00027       s[i] = to;
00028     }
00029     return c;
00030 }
00031 
00032 
00033 vcl_string timestamp()
00034 {
00035   char tmpbuf[128];
00036   vcl_time_t ltime;
00037   struct vcl_tm *today;
00038 
00039   // Get UNIX-style time and display as number and string.
00040   vcl_time( &ltime );
00041 
00042   // Convert to time structure and adjust for PM if necessary.
00043   today = vcl_localtime( &ltime );
00044 
00045   // Use strftime to build a customized time string.
00046   vcl_strftime( tmpbuf, 128, "%Y-%m-%d %H:%M:%S", today );
00047   return vcl_string(tmpbuf);
00048 }
00049 
00050 
00051 //: A historical measurement recording framework.
00052 // Currently the function will append the measurement to the file specified
00053 // by ${MBL_TEST_SAVE_MEASUREMENT_PATH}/measurement_path, and exports it to Dart.
00054 // In the longer term it may save the value via Dart2.
00055 void mbl_test_save_measurement( const vcl_string &measurement_path, double value)
00056 {
00057   vcl_cout << "<DartMeasurement name=\"" <<
00058     vul_file::strip_directory(measurement_path) <<
00059     "\" type=\"numeric/float\">"<<value<<"</DartMeasurement>" << vcl_endl;
00060 
00061   char * cpath = vcl_getenv("MBL_TEST_SAVE_MEASUREMENT_ROOT");
00062   vcl_string path(cpath?cpath:"");
00063   if (path.empty())
00064     path = MBL_CONFIG_TEST_SAVE_MEASUREMENT_ROOT;
00065   if (path.empty()) // Nobody wants the measurements stored this way.
00066     return;
00067 
00068   vcl_string config = MBL_CONFIG_BUILD_NAME;
00069   if (config.empty()) config="DEFAULT_CONFIG";
00070 
00071   path += '/' + measurement_path + ".txt";
00072   replace('\\', '/', path); // replace Windows-style "\" with Unix-style "/"
00073   path = vul_expand_path(path); // removes trailing or repeated "/"
00074   vul_file::make_directory_path(vul_file::dirname(path));
00075   vcl_ofstream file(path.c_str(), vcl_ios_app | vcl_ios_out);
00076 
00077   if (!file)
00078     vcl_cerr << "ERROR: mbl_test_save_measurement: Unable to open file " << path.c_str()
00079              << "ERRNO: " << errno << '\n';
00080   else
00081     file << timestamp() << ' ' << MBL_CONFIG_BUILD_NAME << ' ' << vcl_setprecision(15) << value << vcl_endl;
00082 }