process_jam_log.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. start_pos = first_dir.rfind( '!' );
  103. convert_path_separators( first_dir );
  104. first_dir.insert( first_dir.find( '/', start_pos + 1), "/bin" );
  105. //std::cout << first_dir << std::endl;
  106. start_pos = msg.find( '<', end_pos );
  107. if ( start_pos == string::npos ) return;
  108. ++start_pos;
  109. end_pos = msg.find( '>', start_pos );
  110. second_dir += msg.substr( start_pos, end_pos - start_pos );
  111. start_pos = second_dir.rfind( '!' );
  112. convert_path_separators( second_dir );
  113. second_dir.insert( second_dir.find( '/', start_pos + 1), "/bin" );
  114. //std::cout << second_dir << std::endl;
  115. }
  116. // test_log hides database details -----------------------------------------//
  117. class test_log
  118. : boost::noncopyable
  119. {
  120. const string & m_target_directory;
  121. xml::element_ptr m_root;
  122. public:
  123. test_log( const string & target_directory,
  124. const string & test_name,
  125. const string & toolset )
  126. : m_target_directory( target_directory )
  127. {
  128. fs::path pth( target_directory );
  129. pth /= "test_log.xml";
  130. fs::ifstream file( pth );
  131. if ( !file )
  132. {
  133. string test_program, library_name;
  134. test2prog_map::iterator itr( test2prog.find( test_name ) );
  135. if ( itr != test2prog.end() )
  136. {
  137. test_program = itr->second;
  138. string::size_type start_pos( test_program.find( "libs/" ) );
  139. if ( start_pos != string::npos )
  140. {
  141. start_pos += 5;
  142. library_name = test_program.substr( start_pos,
  143. test_program.find( '/', start_pos )-start_pos );
  144. }
  145. }
  146. m_root.reset( new xml::element( "test-log" ) );
  147. m_root->attributes.push_back(
  148. xml::attribute( "library", library_name ) );
  149. m_root->attributes.push_back(
  150. xml::attribute( "test-name", test_name ) );
  151. m_root->attributes.push_back(
  152. xml::attribute( "test-program", test_program ) );
  153. m_root->attributes.push_back(
  154. xml::attribute( "target-directory", target_directory ) );
  155. m_root->attributes.push_back(
  156. xml::attribute( "toolset", toolset ) );
  157. }
  158. else // existing file
  159. {
  160. m_root = xml::parse( file, pth.string() );
  161. }
  162. }
  163. ~test_log()
  164. {
  165. fs::path pth( m_target_directory );
  166. pth /= "test_log.xml";
  167. fs::ofstream file( pth );
  168. if ( !file )
  169. throw fs::filesystem_error( "process_jam_long.cpp",
  170. pth, "can't open output file" );
  171. xml::write( *m_root, file );
  172. }
  173. const string & target_directory() const { return m_target_directory; }
  174. void remove_action( const string & action_name )
  175. // no effect if action_name not found
  176. {
  177. xml::element_list::iterator itr;
  178. for ( itr = m_root->elements.begin();
  179. itr != m_root->elements.end() && (*itr)->name != action_name;
  180. ++itr ) {}
  181. if ( itr != m_root->elements.end() ) m_root->elements.erase( itr );
  182. }
  183. void add_action( const string & action_name,
  184. const string & result,
  185. const string & timestamp,
  186. const string & content )
  187. {
  188. remove_action( action_name );
  189. xml::element_ptr action( new xml::element(action_name) );
  190. m_root->elements.push_back( action );
  191. action->attributes.push_back( xml::attribute( "result", result ) );
  192. action->attributes.push_back( xml::attribute( "timestamp", timestamp ) );
  193. action->content = content;
  194. }
  195. };
  196. // message_manager maps input messages into test_log actions ---------------//
  197. class message_manager
  198. : boost::noncopyable
  199. {
  200. string m_action_name; // !empty() implies action pending
  201. // IOW, a start_message awaits stop_message
  202. string m_target_directory;
  203. string m_test_name;
  204. string m_toolset;
  205. // data needed to stop further compile action after a compile failure
  206. // detected in the same target directory
  207. string m_previous_target_directory;
  208. bool m_compile_failed;
  209. public:
  210. ~message_manager() { /*assert( m_action_name.empty() );*/ }
  211. void start_message( const string & action_name,
  212. const string & target_directory,
  213. const string & test_name,
  214. const string & toolset,
  215. const string & prior_content )
  216. {
  217. if ( !m_action_name.empty() ) stop_message( prior_content );
  218. m_action_name = action_name;
  219. m_target_directory = target_directory;
  220. m_test_name = test_name;
  221. m_toolset = toolset;
  222. if ( m_previous_target_directory != target_directory )
  223. {
  224. m_previous_target_directory = target_directory;
  225. m_compile_failed = false;
  226. }
  227. }
  228. void stop_message( const string & content )
  229. {
  230. if ( m_action_name.empty() ) return;
  231. stop_message( m_action_name, m_target_directory,
  232. "succeed", timestamp(), content );
  233. }
  234. void stop_message( const string & action_name,
  235. const string & target_directory,
  236. const string & result,
  237. const string & timestamp,
  238. const string & content )
  239. // the only valid action_names are "compile", "link", "run", "lib"
  240. {
  241. // my understanding of the jam output is that there should never be
  242. // a stop_message that was not preceeded by a matching start_message.
  243. assert( m_action_name == action_name );
  244. assert( m_target_directory == target_directory );
  245. assert( result == "succeed" || result == "fail" );
  246. // if test_log.xml entry needed, create it
  247. if ( !m_compile_failed
  248. || action_name != "compile"
  249. || m_previous_target_directory != target_directory )
  250. {
  251. if ( action_name == "compile"
  252. && result == "fail" ) m_compile_failed = true;
  253. test_log tl( target_directory, m_test_name, m_toolset );
  254. // dependency removal
  255. if ( action_name == "lib" )
  256. {
  257. tl.remove_action( "compile" );
  258. tl.remove_action( "link" );
  259. tl.remove_action( "run" );
  260. }
  261. else if ( action_name == "compile" )
  262. {
  263. tl.remove_action( "link" );
  264. tl.remove_action( "run" );
  265. if ( result == "fail" ) m_compile_failed = true;
  266. }
  267. else if ( action_name == "link" ) { tl.remove_action( "run" ); }
  268. // dependency removal won't work right with random names, so assert
  269. else { assert( action_name == "run" ); }
  270. // add the stop_message action
  271. tl.add_action( action_name, result, timestamp, content );
  272. }
  273. m_action_name = ""; // signal no pending action
  274. m_previous_target_directory = target_directory;
  275. }
  276. };
  277. }
  278. // main --------------------------------------------------------------------//
  279. int cpp_main( int argc, char ** argv )
  280. {
  281. string boost_root_relative_initial;
  282. fs::path boost_root( fs::initial_path() );
  283. while ( !boost_root.empty()
  284. && !fs::exists( boost_root / "libs" ) )
  285. {
  286. boost_root = boost_root.branch_path();
  287. boost_root_relative_initial += "../";
  288. }
  289. if ( boost_root.empty() )
  290. {
  291. std::cout << "must be run from within the boost-root directory tree\n";
  292. return 1;
  293. }
  294. message_manager mgr;
  295. string line;
  296. string content;
  297. bool capture_lines = false;
  298. // This loop looks at lines for certain signatures, and accordingly:
  299. // * Calls start_message() to start capturing lines. (start_message() will
  300. // automatically call stop_message() if needed.)
  301. // * Calls stop_message() to stop capturing lines.
  302. // * Capture lines if line capture on.
  303. while ( std::getline( std::cin, line ) )
  304. {
  305. // std::cout << line << "\n";
  306. // create map of test-name to test-file-path
  307. if ( line.find( "(boost-test " ) == 0 )
  308. {
  309. string test_name, test_file_path;
  310. string::size_type colon = line.find( ":" );
  311. if ( colon != string::npos )
  312. {
  313. test_name = line.substr( 13, colon-15 );
  314. test_file_path = line.substr( colon+3,
  315. line.find( "\"", colon+3 )-colon-3 );
  316. convert_path_separators( test_file_path );
  317. test2prog.insert( std::make_pair( test_name, test_file_path ) );
  318. // std::cout << test_name << ", " << test_file_path << "\n";
  319. continue;
  320. }
  321. }
  322. // these actions represent both the start of a new action
  323. // and the end of a failed action
  324. else if ( line.find( "C++-action " ) != string::npos
  325. || line.find( "vc-C++ " ) != string::npos
  326. || line.find( "C-action " ) != string::npos
  327. || line.find( "Cc-action " ) != string::npos
  328. || line.find( "Link-action " ) != string::npos
  329. || line.find( "vc-Link " ) != string::npos )
  330. {
  331. string action( ( line.find( "Link-action " ) != string::npos
  332. || line.find( "vc-Link " ) != string::npos )
  333. ? "link" : "compile" );
  334. if ( line.find( "...failed " ) != string::npos )
  335. mgr.stop_message( action, target_directory( line ),
  336. "fail", timestamp(), content );
  337. else
  338. {
  339. string target_dir( target_directory( line ) );
  340. mgr.start_message( action, target_dir,
  341. test_name( target_dir ), toolset( target_dir ), content );
  342. }
  343. content = "\n";
  344. capture_lines = true;
  345. }
  346. // these actions are only used to stop the previous action
  347. else if ( line.find( "-Archive" ) != string::npos
  348. || line.find( "MkDir" ) == 0 )
  349. {
  350. mgr.stop_message( content );
  351. content.clear();
  352. capture_lines = false;
  353. }
  354. else if ( line.find( "execute-test" ) != string::npos )
  355. {
  356. if ( line.find( "...failed " ) != string::npos )
  357. {
  358. mgr.stop_message( "run", target_directory( line ),
  359. "fail", timestamp(), content );
  360. content = "\n";
  361. capture_lines = true;
  362. }
  363. else
  364. {
  365. string target_dir( target_directory( line ) );
  366. mgr.start_message( "run", target_dir,
  367. test_name( target_dir ), toolset( target_dir ), content );
  368. // contents of .output file for content
  369. capture_lines = false;
  370. content = "\n";
  371. fs::ifstream file( fs::path(target_dir)
  372. / (test_name(target_dir) + ".output") );
  373. if ( file )
  374. {
  375. string ln;
  376. while ( std::getline( file, ln ) )
  377. {
  378. append_html( ln, content );;
  379. content += "\n";
  380. }
  381. }
  382. }
  383. }
  384. // bjam indicates some prior dependency failed by a "...skipped" message
  385. else if ( line.find( "...skipped <" ) != string::npos )
  386. {
  387. mgr.stop_message( content );
  388. content.clear();
  389. capture_lines = false;
  390. if ( line.find( ".exe for lack of " ) != string::npos )
  391. {
  392. capture_lines = true;
  393. string target_dir;
  394. string lib_dir;
  395. parse_skipped_msg( line, target_dir, lib_dir );
  396. if ( target_dir != lib_dir ) // it's a lib problem
  397. {
  398. target_dir.insert( 0, boost_root_relative_initial );
  399. mgr.start_message( "lib", target_dir,
  400. test_name( target_dir ), toolset( target_dir ), content );
  401. content = lib_dir;
  402. mgr.stop_message( "lib", target_dir, "fail", timestamp(), content );
  403. content = "\n";
  404. }
  405. }
  406. }
  407. else if ( line.find( "**passed**" ) != string::npos
  408. || line.find( "failed-test-file " ) != string::npos
  409. || line.find( "command-file-dump" ) != string::npos )
  410. {
  411. mgr.stop_message( content );
  412. content = "\n";
  413. capture_lines = true;
  414. }
  415. else if ( capture_lines ) // hang onto lines for possible later use
  416. {
  417. append_html( line, content );;
  418. content += "\n";
  419. }
  420. }
  421. mgr.stop_message( content );
  422. return 0;
  423. }
粤ICP备19079148号