process_jam_log.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // process jam regression test output into XML -----------------------------//
  2. // (C) Copyright Beman Dawes 2002. Permission to copy,
  3. // use, modify, sell and distribute this software is granted provided this
  4. // copyright notice appears in all copies. This software is provided "as is"
  5. // without express or implied warranty, and with no claim as to its
  6. // suitability for any purpose.
  7. #include "detail/tiny_xml.hpp"
  8. #include "boost/filesystem/operations.hpp"
  9. #include "boost/filesystem/fstream.hpp"
  10. #include "boost/filesystem/exception.hpp"
  11. #include <iostream>
  12. #include <string>
  13. #include <map>
  14. #include <utility> // for make_pair
  15. #include <ctime>
  16. using std::string;
  17. namespace xml = boost::tiny_xml;
  18. namespace fs = boost::filesystem;
  19. #define BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE
  20. #include <boost/test/included/prg_exec_monitor.hpp>
  21. namespace
  22. {
  23. typedef std::map< string, string > test2prog_map; // test_name, test_file_path
  24. test2prog_map test2prog;
  25. // append_html -------------------------------------------------------------//
  26. void append_html( const string & src, string & target )
  27. {
  28. // there are a few lines we want to ignore
  29. if ( src.find( "th target..." ) != string::npos
  30. || src.find( "cc1plus.exe: warning: changing search order for system directory" ) != string::npos
  31. || src.find( "cc1plus.exe: warning: as it has already been specified as a non-system directory" ) != string::npos
  32. ) return;
  33. for ( string::size_type pos = 0; pos < src.size(); ++pos )
  34. {
  35. if ( src[pos] == '<' ) target += "&lt;";
  36. else if ( src[pos] == '>' ) target += "&gt;";
  37. else if ( src[pos] == '&' ) target += "&amp;";
  38. else target += src[pos];
  39. }
  40. }
  41. // timestamp ---------------------------------------------------------------//
  42. string timestamp()
  43. {
  44. char run_date[128];
  45. std::time_t tod;
  46. std::time( &tod );
  47. std::strftime( run_date, sizeof(run_date),
  48. "%Y-%m-%d %X UTC", std::gmtime( &tod ) );
  49. return string( run_date );
  50. }
  51. // convert path separators to forward slashes ------------------------------//
  52. void convert_path_separators( string & s )
  53. {
  54. for ( string::iterator itr = s.begin(); itr != s.end(); ++itr )
  55. if ( *itr == '\\' || *itr == '!' ) *itr = '/';
  56. }
  57. // extract a directory from a jam target string ----------------------------//
  58. string target_directory( const string & s )
  59. {
  60. string temp( s );
  61. convert_path_separators( temp );
  62. temp.erase( temp.find_last_of( "/" ) );
  63. string::size_type pos = temp.find_last_of( " " );
  64. if ( pos != string::npos ) temp.erase( 0, pos+1 );
  65. return temp;
  66. }
  67. string toolset( const string & s )
  68. {
  69. string t( s );
  70. string::size_type pos = t.find( "/bin/" );
  71. if ( pos != string::npos ) pos += 5;
  72. else return "";
  73. pos = t.find( "/", pos );
  74. if ( pos != string::npos ) pos += 1;
  75. else return "";
  76. return t.substr( pos, t.find( "/", pos ) - pos );
  77. }
  78. string test_name( const string & s )
  79. {
  80. string t( s );
  81. string::size_type pos = t.find( "/bin/" );
  82. if ( pos != string::npos ) pos += 5;
  83. else return "";
  84. return t.substr( pos, t.find( ".", pos ) - pos );
  85. }
  86. // the format of paths is really kinky, so convert to normal form
  87. // first path is missing the leading "..\".
  88. // first path is missing "\bin" after "status".
  89. // second path is missing the leading "..\".
  90. // second path is missing "\bin" after "build".
  91. // second path uses "!" for some separators.
  92. void parse_skipped_msg( const string & msg,
  93. string & first_dir, string & second_dir )
  94. {
  95. first_dir.clear();
  96. second_dir.clear();
  97. string::size_type start_pos( msg.find( '<' ) );
  98. if ( start_pos == string::npos ) return;
  99. ++start_pos;
  100. string::size_type end_pos( msg.find( '>', start_pos ) );
  101. first_dir += msg.substr( start_pos, end_pos - start_pos );
  102. convert_path_separators( first_dir );
  103. first_dir.insert( 6, "/bin" );
  104. start_pos = msg.find( '<', end_pos );
  105. if ( start_pos == string::npos ) return;
  106. ++start_pos;
  107. end_pos = msg.find( '>', start_pos );
  108. second_dir += msg.substr( start_pos, end_pos - start_pos );
  109. convert_path_separators( second_dir );
  110. second_dir.insert( second_dir.find( "/build/" )+6, "/bin" );
  111. }
  112. // test_log hides database details -----------------------------------------//
  113. class test_log
  114. : boost::noncopyable
  115. {
  116. const string & m_target_directory;
  117. xml::element_ptr m_root;
  118. public:
  119. test_log( const string & target_directory,
  120. const string & test_name,
  121. const string & toolset )
  122. : m_target_directory( target_directory )
  123. {
  124. fs::path pth( target_directory );
  125. pth /= "test_log.xml";
  126. fs::ifstream file( pth );
  127. if ( !file )
  128. {
  129. string test_program, library_name;
  130. test2prog_map::iterator itr( test2prog.find( test_name ) );
  131. if ( itr != test2prog.end() )
  132. {
  133. test_program = itr->second;
  134. string::size_type start_pos( test_program.find( "libs/" ) );
  135. if ( start_pos != string::npos )
  136. {
  137. start_pos += 5;
  138. library_name = test_program.substr( start_pos,
  139. test_program.find( '/', start_pos )-start_pos );
  140. }
  141. }
  142. m_root.reset( new xml::element( "test-log" ) );
  143. m_root->attributes.push_back(
  144. xml::attribute( "library", library_name ) );
  145. m_root->attributes.push_back(
  146. xml::attribute( "test-name", test_name ) );
  147. m_root->attributes.push_back(
  148. xml::attribute( "test-program", test_program ) );
  149. m_root->attributes.push_back(
  150. xml::attribute( "target-directory", target_directory ) );
  151. m_root->attributes.push_back(
  152. xml::attribute( "toolset", toolset ) );
  153. }
  154. else // existing file
  155. {
  156. m_root = xml::parse( file, pth.string() );
  157. }
  158. }
  159. ~test_log()
  160. {
  161. fs::path pth( m_target_directory );
  162. pth /= "test_log.xml";
  163. fs::ofstream file( pth );
  164. if ( !file )
  165. throw fs::filesystem_error( "Can't open output file: "
  166. + pth.string(), fs::other_error );
  167. xml::write( *m_root, file );
  168. }
  169. const string & target_directory() const { return m_target_directory; }
  170. void remove_action( const string & action_name )
  171. // no effect if action_name not found
  172. {
  173. xml::element_list::iterator itr;
  174. for ( itr = m_root->elements.begin();
  175. itr != m_root->elements.end() && (*itr)->name != action_name;
  176. ++itr ) {}
  177. if ( itr != m_root->elements.end() ) m_root->elements.erase( itr );
  178. }
  179. void add_action( const string & action_name,
  180. const string & result,
  181. const string & timestamp,
  182. const string & content )
  183. {
  184. remove_action( action_name );
  185. xml::element_ptr action( new xml::element(action_name) );
  186. m_root->elements.push_back( action );
  187. action->attributes.push_back( xml::attribute( "result", result ) );
  188. action->attributes.push_back( xml::attribute( "timestamp", timestamp ) );
  189. action->content = content;
  190. }
  191. };
  192. // message_manager maps input messages into test_log actions ---------------//
  193. class message_manager
  194. : boost::noncopyable
  195. {
  196. string m_action_name; // !empty() implies action pending
  197. // IOW, a start_message awaits stop_message
  198. string m_target_directory;
  199. string m_test_name;
  200. string m_toolset;
  201. public:
  202. ~message_manager() { /*assert( m_action_name.empty() );*/ }
  203. void start_message( const string & action_name,
  204. const string & target_directory,
  205. const string & test_name,
  206. const string & toolset,
  207. const string & prior_content )
  208. {
  209. if ( !m_action_name.empty() ) stop_message( prior_content );
  210. m_action_name = action_name;
  211. m_target_directory = target_directory;
  212. m_test_name = test_name;
  213. m_toolset = toolset;
  214. }
  215. void stop_message( const string & content )
  216. {
  217. if ( m_action_name.empty() ) return;
  218. stop_message( m_action_name, m_target_directory,
  219. "succeed", timestamp(), content );
  220. }
  221. void stop_message( const string & action_name,
  222. const string & target_directory,
  223. const string & result,
  224. const string & timestamp,
  225. const string & content )
  226. // the only valid action_names are "compile", "link", "run", "lib"
  227. {
  228. // my understanding of the jam output is that there should never be
  229. // a stop_message that was not preceeded by a matching start_message.
  230. assert( m_action_name == action_name );
  231. assert( m_target_directory == target_directory );
  232. test_log tl( target_directory, m_test_name, m_toolset );
  233. // dependency removal
  234. if ( action_name == "lib" )
  235. {
  236. tl.remove_action( "compile" );
  237. tl.remove_action( "link" );
  238. tl.remove_action( "run" );
  239. }
  240. else if ( action_name == "compile" )
  241. {
  242. tl.remove_action( "link" );
  243. tl.remove_action( "run" );
  244. }
  245. else if ( action_name == "link" ) { tl.remove_action( "run" ); }
  246. // dependency removal won't work right with random names, so assert
  247. else { assert( action_name == "run" ); }
  248. // add the stop_message action
  249. tl.add_action( action_name, result, timestamp, content );
  250. m_action_name = ""; // signal no pending action
  251. }
  252. };
  253. }
  254. // main --------------------------------------------------------------------//
  255. int cpp_main( int argc, char ** argv )
  256. {
  257. message_manager mgr;
  258. string line;
  259. string content;
  260. bool capture_lines = false;
  261. // This loop looks at lines for certain signatures, and accordingly:
  262. // * Calls start_message() to start capturing lines. (start_message() will
  263. // automatically call stop_message() if needed.)
  264. // * Calls stop_message() to stop capturing lines.
  265. // * Capture lines if line capture on.
  266. while ( std::getline( std::cin, line ) )
  267. {
  268. // std::cout << line << "\n";
  269. if ( line.find( "(boost-test " ) == 0 )
  270. {
  271. string test_name, test_file_path;
  272. string::size_type colon = line.find( ":" );
  273. if ( colon != string::npos )
  274. {
  275. test_name = line.substr( 13, colon-15 );
  276. test_file_path = line.substr( colon+3,
  277. line.find( "\"", colon+3 )-colon-3 );
  278. convert_path_separators( test_file_path );
  279. test2prog.insert( std::make_pair( test_name, test_file_path ) );
  280. // std::cout << test_name << ", " << test_file_path << "\n";
  281. continue;
  282. }
  283. }
  284. else if ( line.find( "C++-action " ) != string::npos
  285. || line.find( "vc-C++ " ) != string::npos
  286. || line.find( "C-action " ) != string::npos
  287. || line.find( "Cc-action " ) != string::npos
  288. || line.find( "Link-action " ) != string::npos
  289. || line.find( "vc-Link " ) != string::npos )
  290. {
  291. string action( ( line.find( "Link-action " ) != string::npos
  292. || line.find( "vc-Link " ) != string::npos )
  293. ? "link" : "compile" );
  294. if ( line.find( "...failed " ) != string::npos )
  295. mgr.stop_message( action, target_directory( line ),
  296. "fail", timestamp(), content );
  297. else
  298. {
  299. string target_dir( target_directory( line ) );
  300. mgr.start_message( action, target_dir,
  301. test_name( target_dir ), toolset( target_dir ), content );
  302. }
  303. content = "\n";
  304. capture_lines = true;
  305. }
  306. else if ( line.find( "execute-test" ) != string::npos )
  307. {
  308. if ( line.find( "...failed " ) != string::npos )
  309. {
  310. mgr.stop_message( "run", target_directory( line ),
  311. "fail", timestamp(), content );
  312. content = "\n";
  313. capture_lines = true;
  314. }
  315. else
  316. {
  317. string target_dir( target_directory( line ) );
  318. mgr.start_message( "run", target_dir,
  319. test_name( target_dir ), toolset( target_dir ), content );
  320. // contents of .output file for content
  321. capture_lines = false;
  322. content = "\n";
  323. fs::ifstream file( fs::path(target_dir)
  324. / (test_name(target_dir) + ".output") );
  325. if ( file )
  326. {
  327. string ln;
  328. while ( std::getline( file, ln ) )
  329. {
  330. append_html( ln, content );;
  331. content += "\n";
  332. }
  333. }
  334. }
  335. }
  336. else if ( line.find( "...skipped <" ) != string::npos
  337. && line.find( ".test for lack of " ) != string::npos )
  338. {
  339. mgr.stop_message( content );
  340. content.clear();
  341. capture_lines = true;
  342. string target_dir;
  343. string lib_dir;
  344. parse_skipped_msg( line, target_dir, lib_dir );
  345. if ( target_dir != lib_dir ) // it's a lib problem
  346. {
  347. target_dir.insert( 0, "../" );
  348. mgr.start_message( "lib", target_dir,
  349. test_name( target_dir ), toolset( target_dir ), content );
  350. content = lib_dir;
  351. mgr.stop_message( "lib", target_dir, "fail", timestamp(), content );
  352. content = "\n";
  353. }
  354. }
  355. else if ( line.find( "**passed**" ) != string::npos
  356. || line.find( "failed-test-file " ) != string::npos
  357. || line.find( "command-file-dump" ) != string::npos )
  358. {
  359. mgr.stop_message( content );
  360. content = "\n";
  361. capture_lines = true;
  362. }
  363. else if ( capture_lines ) // hang onto lines for possible later use
  364. {
  365. append_html( line, content );;
  366. content += "\n";
  367. }
  368. }
  369. mgr.stop_message( content );
  370. return 0;
  371. }
粤ICP备19079148号