process_jam_log.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. #include <cctype> // for tolower
  17. using std::string;
  18. namespace xml = boost::tiny_xml;
  19. namespace fs = boost::filesystem;
  20. #define BOOST_NO_CPP_MAIN_SUCCESS_MESSAGE
  21. #include <boost/test/included/prg_exec_monitor.hpp>
  22. namespace
  23. {
  24. struct test_info
  25. {
  26. string file_path;
  27. string type;
  28. bool always_show_run_output;
  29. };
  30. typedef std::map< string, test_info > test2info_map; // key is test-name
  31. test2info_map test2info;
  32. // append_html -------------------------------------------------------------//
  33. void append_html( const string & src, string & target )
  34. {
  35. // there are a few lines we want to ignore
  36. if ( src.find( "th target..." ) != string::npos
  37. || src.find( "cc1plus.exe: warning: changing search order for system directory" ) != string::npos
  38. || src.find( "cc1plus.exe: warning: as it has already been specified as a non-system directory" ) != string::npos
  39. ) return;
  40. for ( string::size_type pos = 0; pos < src.size(); ++pos )
  41. {
  42. if ( src[pos] == '<' ) target += "&lt;";
  43. else if ( src[pos] == '>' ) target += "&gt;";
  44. else if ( src[pos] == '&' ) target += "&amp;";
  45. else target += src[pos];
  46. }
  47. }
  48. // timestamp ---------------------------------------------------------------//
  49. string timestamp()
  50. {
  51. char run_date[128];
  52. std::time_t tod;
  53. std::time( &tod );
  54. std::strftime( run_date, sizeof(run_date),
  55. "%Y-%m-%d %X UTC", std::gmtime( &tod ) );
  56. return string( run_date );
  57. }
  58. // convert path separators to forward slashes ------------------------------//
  59. void convert_path_separators( string & s )
  60. {
  61. for ( string::iterator itr = s.begin(); itr != s.end(); ++itr )
  62. if ( *itr == '\\' || *itr == '!' ) *itr = '/';
  63. }
  64. // extract a directory from a jam target string ----------------------------//
  65. string target_directory( const string & s )
  66. {
  67. string temp( s );
  68. convert_path_separators( temp );
  69. temp.erase( temp.find_last_of( "/" ) );
  70. string::size_type pos = temp.find_last_of( " " );
  71. if ( pos != string::npos ) temp.erase( 0, pos+1 );
  72. return temp;
  73. }
  74. string toolset( const string & s )
  75. {
  76. string t( s );
  77. string::size_type pos = t.find( "/bin/" );
  78. if ( pos != string::npos ) pos += 5;
  79. else return "";
  80. pos = t.find( "/", pos );
  81. if ( pos != string::npos ) pos += 1;
  82. else return "";
  83. return t.substr( pos, t.find( "/", pos ) - pos );
  84. }
  85. string test_name( const string & s )
  86. {
  87. string t( s );
  88. string::size_type pos = t.find( "/bin/" );
  89. if ( pos != string::npos ) pos += 5;
  90. else return "";
  91. return t.substr( pos, t.find( ".", pos ) - pos );
  92. }
  93. // the format of paths is really kinky, so convert to normal form
  94. // first path is missing the leading "..\".
  95. // first path is missing "\bin" after "status".
  96. // second path is missing the leading "..\".
  97. // second path is missing "\bin" after "build".
  98. // second path uses "!" for some separators.
  99. void parse_skipped_msg( const string & msg,
  100. string & first_dir, string & second_dir )
  101. {
  102. first_dir.clear();
  103. second_dir.clear();
  104. string::size_type start_pos( msg.find( '<' ) );
  105. if ( start_pos == string::npos ) return;
  106. ++start_pos;
  107. string::size_type end_pos( msg.find( '>', start_pos ) );
  108. first_dir += msg.substr( start_pos, end_pos - start_pos );
  109. start_pos = first_dir.rfind( '!' );
  110. convert_path_separators( first_dir );
  111. first_dir.insert( first_dir.find( '/', start_pos + 1), "/bin" );
  112. //std::cout << first_dir << std::endl;
  113. start_pos = msg.find( '<', end_pos );
  114. if ( start_pos == string::npos ) return;
  115. ++start_pos;
  116. end_pos = msg.find( '>', start_pos );
  117. second_dir += msg.substr( start_pos, end_pos - start_pos );
  118. start_pos = second_dir.rfind( '!' );
  119. convert_path_separators( second_dir );
  120. second_dir.insert( second_dir.find( '/', start_pos + 1), "/bin" );
  121. //std::cout << second_dir << std::endl;
  122. }
  123. // test_log hides database details -----------------------------------------//
  124. class test_log
  125. : boost::noncopyable
  126. {
  127. const string & m_target_directory;
  128. xml::element_ptr m_root;
  129. public:
  130. test_log( const string & target_directory,
  131. const string & test_name,
  132. const string & toolset )
  133. : m_target_directory( target_directory )
  134. {
  135. fs::path pth( target_directory );
  136. pth /= "test_log.xml";
  137. fs::ifstream file( pth );
  138. if ( !file )
  139. {
  140. test_info info;
  141. string library_name;
  142. test2info_map::iterator itr( test2info.find( test_name ) );
  143. if ( itr != test2info.end() )
  144. {
  145. info = itr->second;
  146. string::size_type start_pos( info.file_path.find( "libs/" ) );
  147. if ( start_pos != string::npos )
  148. {
  149. start_pos += 5;
  150. library_name = info.file_path.substr( start_pos,
  151. info.file_path.find( '/', start_pos )-start_pos );
  152. }
  153. }
  154. m_root.reset( new xml::element( "test-log" ) );
  155. m_root->attributes.push_back(
  156. xml::attribute( "library", library_name ) );
  157. m_root->attributes.push_back(
  158. xml::attribute( "test-name", test_name ) );
  159. m_root->attributes.push_back(
  160. xml::attribute( "test-type", info.type ) );
  161. m_root->attributes.push_back(
  162. xml::attribute( "test-program", info.file_path ) );
  163. m_root->attributes.push_back(
  164. xml::attribute( "target-directory", target_directory ) );
  165. m_root->attributes.push_back(
  166. xml::attribute( "toolset", toolset ) );
  167. m_root->attributes.push_back(
  168. xml::attribute( "show-run-output",
  169. info.always_show_run_output ? "true" : "false" ) );
  170. }
  171. else // existing file
  172. {
  173. m_root = xml::parse( file, pth.string() );
  174. }
  175. }
  176. ~test_log()
  177. {
  178. fs::path pth( m_target_directory );
  179. pth /= "test_log.xml";
  180. fs::ofstream file( pth );
  181. if ( !file )
  182. throw fs::filesystem_error( "process_jam_long.cpp",
  183. pth, "can't open output file" );
  184. xml::write( *m_root, file );
  185. }
  186. const string & target_directory() const { return m_target_directory; }
  187. void remove_action( const string & action_name )
  188. // no effect if action_name not found
  189. {
  190. xml::element_list::iterator itr;
  191. for ( itr = m_root->elements.begin();
  192. itr != m_root->elements.end() && (*itr)->name != action_name;
  193. ++itr ) {}
  194. if ( itr != m_root->elements.end() ) m_root->elements.erase( itr );
  195. }
  196. void add_action( const string & action_name,
  197. const string & result,
  198. const string & timestamp,
  199. const string & content )
  200. {
  201. remove_action( action_name );
  202. xml::element_ptr action( new xml::element(action_name) );
  203. m_root->elements.push_back( action );
  204. action->attributes.push_back( xml::attribute( "result", result ) );
  205. action->attributes.push_back( xml::attribute( "timestamp", timestamp ) );
  206. action->content = content;
  207. }
  208. };
  209. // message_manager maps input messages into test_log actions ---------------//
  210. class message_manager
  211. : boost::noncopyable
  212. {
  213. string m_action_name; // !empty() implies action pending
  214. // IOW, a start_message awaits stop_message
  215. string m_target_directory;
  216. string m_test_name;
  217. string m_toolset;
  218. // data needed to stop further compile action after a compile failure
  219. // detected in the same target directory
  220. string m_previous_target_directory;
  221. bool m_compile_failed;
  222. public:
  223. ~message_manager() { /*assert( m_action_name.empty() );*/ }
  224. void start_message( const string & action_name,
  225. const string & target_directory,
  226. const string & test_name,
  227. const string & toolset,
  228. const string & prior_content )
  229. {
  230. if ( !m_action_name.empty() ) stop_message( prior_content );
  231. m_action_name = action_name;
  232. m_target_directory = target_directory;
  233. m_test_name = test_name;
  234. m_toolset = toolset;
  235. if ( m_previous_target_directory != target_directory )
  236. {
  237. m_previous_target_directory = target_directory;
  238. m_compile_failed = false;
  239. }
  240. }
  241. void stop_message( const string & content )
  242. {
  243. if ( m_action_name.empty() ) return;
  244. stop_message( m_action_name, m_target_directory,
  245. "succeed", timestamp(), content );
  246. }
  247. void stop_message( const string & action_name,
  248. const string & target_directory,
  249. const string & result,
  250. const string & timestamp,
  251. const string & content )
  252. // the only valid action_names are "compile", "link", "run", "lib"
  253. {
  254. // my understanding of the jam output is that there should never be
  255. // a stop_message that was not preceeded by a matching start_message.
  256. assert( m_action_name == action_name );
  257. assert( m_target_directory == target_directory );
  258. assert( result == "succeed" || result == "fail" );
  259. // if test_log.xml entry needed, create it
  260. if ( !m_compile_failed
  261. || action_name != "compile"
  262. || m_previous_target_directory != target_directory )
  263. {
  264. if ( action_name == "compile"
  265. && result == "fail" ) m_compile_failed = true;
  266. test_log tl( target_directory, m_test_name, m_toolset );
  267. // dependency removal
  268. if ( action_name == "lib" )
  269. {
  270. tl.remove_action( "compile" );
  271. tl.remove_action( "link" );
  272. tl.remove_action( "run" );
  273. }
  274. else if ( action_name == "compile" )
  275. {
  276. tl.remove_action( "link" );
  277. tl.remove_action( "run" );
  278. if ( result == "fail" ) m_compile_failed = true;
  279. }
  280. else if ( action_name == "link" ) { tl.remove_action( "run" ); }
  281. // dependency removal won't work right with random names, so assert
  282. else { assert( action_name == "run" ); }
  283. // add the stop_message action
  284. tl.add_action( action_name, result, timestamp, content );
  285. }
  286. m_action_name = ""; // signal no pending action
  287. m_previous_target_directory = target_directory;
  288. }
  289. };
  290. }
  291. // main --------------------------------------------------------------------//
  292. int cpp_main( int argc, char ** argv )
  293. {
  294. string boost_root_relative_initial;
  295. fs::path boost_root( fs::initial_path() );
  296. while ( !boost_root.empty()
  297. && !fs::exists( boost_root / "libs" ) )
  298. {
  299. boost_root = boost_root.branch_path();
  300. boost_root_relative_initial += "../";
  301. }
  302. if ( boost_root.empty() )
  303. {
  304. std::cout << "must be run from within the boost-root directory tree\n";
  305. return 1;
  306. }
  307. message_manager mgr;
  308. string line;
  309. string content;
  310. bool capture_lines = false;
  311. // This loop looks at lines for certain signatures, and accordingly:
  312. // * Calls start_message() to start capturing lines. (start_message() will
  313. // automatically call stop_message() if needed.)
  314. // * Calls stop_message() to stop capturing lines.
  315. // * Capture lines if line capture on.
  316. while ( std::getline( std::cin, line ) )
  317. {
  318. // std::cout << line << "\n";
  319. // create map of test-name to test-info
  320. if ( line.find( "boost-test(" ) == 0 )
  321. {
  322. string::size_type pos = line.find( '"' );
  323. string test_name( line.substr( pos+1, line.find( '"', pos+1)-pos-1 ) );
  324. test_info info;
  325. info.always_show_run_output
  326. = line.find( "\"always_show_run_output\"" ) != string::npos;
  327. info.type = line.substr( 11, line.find( ')' )-11 );
  328. for (int i = 0; i!=info.type.size(); ++i )
  329. { info.type[i] = std::tolower( info.type[i] ); }
  330. pos = line.find( ':' );
  331. info.file_path = line.substr( pos+3,
  332. line.find( "\"", pos+3 )-pos-3 );
  333. convert_path_separators( info.file_path );
  334. test2info.insert( std::make_pair( test_name, info ) );
  335. // std::cout << test_name << ", " << info.type << ", " << info.file_path << "\n";
  336. continue;
  337. }
  338. // these actions represent both the start of a new action
  339. // and the end of a failed action
  340. else if ( line.find( "C++-action " ) != string::npos
  341. || line.find( "vc-C++ " ) != string::npos
  342. || line.find( "C-action " ) != string::npos
  343. || line.find( "Cc-action " ) != string::npos
  344. || line.find( "Link-action " ) != string::npos
  345. || line.find( "vc-Link " ) != string::npos )
  346. {
  347. string action( ( line.find( "Link-action " ) != string::npos
  348. || line.find( "vc-Link " ) != string::npos )
  349. ? "link" : "compile" );
  350. if ( line.find( "...failed " ) != string::npos )
  351. mgr.stop_message( action, target_directory( line ),
  352. "fail", timestamp(), content );
  353. else
  354. {
  355. string target_dir( target_directory( line ) );
  356. mgr.start_message( action, target_dir,
  357. test_name( target_dir ), toolset( target_dir ), content );
  358. }
  359. content = "\n";
  360. capture_lines = true;
  361. }
  362. // these actions are only used to stop the previous action
  363. else if ( line.find( "-Archive" ) != string::npos
  364. || line.find( "MkDir" ) == 0 )
  365. {
  366. mgr.stop_message( content );
  367. content.clear();
  368. capture_lines = false;
  369. }
  370. else if ( line.find( "execute-test" ) != string::npos )
  371. {
  372. if ( line.find( "...failed " ) != string::npos )
  373. {
  374. mgr.stop_message( "run", target_directory( line ),
  375. "fail", timestamp(), content );
  376. content = "\n";
  377. capture_lines = true;
  378. }
  379. else
  380. {
  381. string target_dir( target_directory( line ) );
  382. mgr.start_message( "run", target_dir,
  383. test_name( target_dir ), toolset( target_dir ), content );
  384. // contents of .output file for content
  385. capture_lines = false;
  386. content = "\n";
  387. fs::ifstream file( fs::path(target_dir)
  388. / (test_name(target_dir) + ".output") );
  389. if ( file )
  390. {
  391. string ln;
  392. while ( std::getline( file, ln ) )
  393. {
  394. append_html( ln, content );;
  395. content += "\n";
  396. }
  397. }
  398. }
  399. }
  400. // bjam indicates some prior dependency failed by a "...skipped" message
  401. else if ( line.find( "...skipped <" ) != string::npos )
  402. {
  403. mgr.stop_message( content );
  404. content.clear();
  405. capture_lines = false;
  406. if ( line.find( ".exe for lack of " ) != string::npos )
  407. {
  408. capture_lines = true;
  409. string target_dir;
  410. string lib_dir;
  411. parse_skipped_msg( line, target_dir, lib_dir );
  412. if ( target_dir != lib_dir ) // it's a lib problem
  413. {
  414. target_dir.insert( 0, boost_root_relative_initial );
  415. mgr.start_message( "lib", target_dir,
  416. test_name( target_dir ), toolset( target_dir ), content );
  417. content = lib_dir;
  418. mgr.stop_message( "lib", target_dir, "fail", timestamp(), content );
  419. content = "\n";
  420. }
  421. }
  422. }
  423. else if ( line.find( "**passed**" ) != string::npos
  424. || line.find( "failed-test-file " ) != string::npos
  425. || line.find( "command-file-dump" ) != string::npos )
  426. {
  427. mgr.stop_message( content );
  428. content = "\n";
  429. capture_lines = true;
  430. }
  431. else if ( capture_lines ) // hang onto lines for possible later use
  432. {
  433. append_html( line, content );;
  434. content += "\n";
  435. }
  436. }
  437. mgr.stop_message( content );
  438. return 0;
  439. }
粤ICP备19079148号