process_jam_log.cpp 12 KB

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