message.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <cctype>
  15. #include <cstddef>
  16. #include <fstream>
  17. #include <iostream>
  18. #include <string>
  19. #include <vector>
  20. #include <errno.h>
  21. #include <getopt.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. #include <util/filename.h>
  26. #include <util/strutil.h>
  27. #include <log/log_messages.h>
  28. #include <log/message_dictionary.h>
  29. #include <log/message_exception.h>
  30. #include <log/message_reader.h>
  31. #include <log/logger.h>
  32. #include <boost/foreach.hpp>
  33. using namespace std;
  34. using namespace isc::log;
  35. using namespace isc::util;
  36. static const char* VERSION = "1.0-0";
  37. /// \file log/compiler/message.cc
  38. /// \brief Message Compiler
  39. ///
  40. /// \b Overview<BR>
  41. /// This is the program that takes as input a message file and produces:
  42. ///
  43. /// \li A .h file containing message definition
  44. /// \li A .cc file containing code that adds the messages to the program's
  45. /// message dictionary at start-up time.
  46. ///
  47. /// \b Invocation<BR>
  48. /// The program is invoked with the command:
  49. ///
  50. /// <tt>message [-v | -h | -p | -d <dir> | <message-file>]</tt>
  51. ///
  52. /// It reads the message file and writes out two files of the same
  53. /// name in the current working directory (unless -d is used) but
  54. /// with extensions of .h and .cc, or .py if -p is used.
  55. ///
  56. /// -v causes it to print the version number and exit. -h prints a help
  57. /// message (and exits). -p sets the output to python. -d <dir> will make
  58. /// it write the output file(s) to dir instead of current working
  59. /// directory
  60. /// \brief Print Version
  61. ///
  62. /// Prints the program's version number.
  63. void
  64. version() {
  65. cout << VERSION << "\n";
  66. }
  67. /// \brief Print Usage
  68. ///
  69. /// Prints program usage to stdout.
  70. void
  71. usage() {
  72. cout <<
  73. "Usage: message [-h] [-v] [-p] [-d dir] <message-file>\n" <<
  74. "\n" <<
  75. "-h Print this message and exit\n" <<
  76. "-v Print the program version and exit\n" <<
  77. "-p Output python source instead of C++ ones\n" <<
  78. "-d <dir> Place output files in given directory\n" <<
  79. "\n" <<
  80. "<message-file> is the name of the input message file.\n";
  81. }
  82. /// \brief Create Time
  83. ///
  84. /// Returns the current time as a suitably-formatted string.
  85. ///
  86. /// \return Current time
  87. string
  88. currentTime() {
  89. // Get a text representation of the current time.
  90. time_t curtime;
  91. time(&curtime);
  92. char* buffer = ctime(&curtime);
  93. // Convert to string and strip out the trailing newline
  94. string current_time = buffer;
  95. return (isc::util::str::trim(current_time));
  96. }
  97. /// \brief Create Header Sentinel
  98. ///
  99. /// Given the name of a file, create an #ifdef sentinel name. The name is
  100. /// __<name>_<ext>, where <name> is the name of the file, and <ext> is the
  101. /// extension less the leading period. The sentinel will be upper-case.
  102. ///
  103. /// \param file Filename object representing the file.
  104. ///
  105. /// \return Sentinel name
  106. string
  107. sentinel(Filename& file) {
  108. string name = file.name();
  109. string ext = file.extension();
  110. string sentinel_text = "__" + name + "_" + ext.substr(1);
  111. isc::util::str::uppercase(sentinel_text);
  112. return (sentinel_text);
  113. }
  114. /// \brief Quote String
  115. ///
  116. /// Inserts an escape character (a backslash) prior to any double quote
  117. /// characters. This is used to handle the fact that the input file does not
  118. /// contain quotes, yet the string will be included in a C++ literal string.
  119. string
  120. quoteString(const string& instring) {
  121. // Create the output string and reserve the space needed to hold the input
  122. // string. (Most input strings will not contain quotes, so this single
  123. // reservation should be all that is needed.)
  124. string outstring;
  125. outstring.reserve(instring.size());
  126. // Iterate through the input string, preceding quotes with a slash.
  127. for (size_t i = 0; i < instring.size(); ++i) {
  128. if (instring[i] == '"') {
  129. outstring += '\\';
  130. }
  131. outstring += instring[i];
  132. }
  133. return (outstring);
  134. }
  135. /// \brief Sorted Identifiers
  136. ///
  137. /// Given a dictionary, return a vector holding the message IDs in sorted
  138. /// order.
  139. ///
  140. /// \param dictionary Dictionary to examine
  141. ///
  142. /// \return Sorted list of message IDs
  143. vector<string>
  144. sortedIdentifiers(MessageDictionary& dictionary) {
  145. vector<string> ident;
  146. for (MessageDictionary::const_iterator i = dictionary.begin();
  147. i != dictionary.end(); ++i) {
  148. ident.push_back(i->first);
  149. }
  150. sort(ident.begin(), ident.end());
  151. return (ident);
  152. }
  153. /// \brief Split Namespace
  154. ///
  155. /// The $NAMESPACE directive may well specify a namespace in the form a::b.
  156. /// Unfortunately, the C++ "namespace" statement can only accept a single
  157. /// string - to set up the namespace of "a::b" requires two statements, one
  158. /// for "namspace a" and the other for "namespace b".
  159. ///
  160. /// This function returns the set of namespace components as a vector of
  161. /// strings. A vector of one element, containing the empty string, is returned
  162. /// if the anonymous namespace is specified.
  163. ///
  164. /// \param ns Argument to $NAMESPACE (passed by value, as we will be modifying
  165. /// it.)
  166. vector<string>
  167. splitNamespace(string ns) {
  168. // Namespaces components are separated by double colon characters -
  169. // convert to single colons.
  170. size_t dcolon;
  171. while ((dcolon = ns.find("::")) != string::npos) {
  172. ns.replace(dcolon, 2, ":");
  173. }
  174. // ... and return the vector of namespace components split on the single
  175. // colon.
  176. return (isc::util::str::tokens(ns, ":"));
  177. }
  178. /// \brief Write Opening Namespace(s)
  179. ///
  180. /// Writes the lines listing the namespaces in use.
  181. void
  182. writeOpeningNamespace(ostream& output, const vector<string>& ns) {
  183. if (!ns.empty()) {
  184. // Output namespaces in correct order
  185. for (vector<string>::size_type i = 0; i < ns.size(); ++i) {
  186. output << "namespace " << ns[i] << " {\n";
  187. }
  188. output << "\n";
  189. }
  190. }
  191. /// \brief Write Closing Namespace(s)
  192. ///
  193. /// Writes the lines listing the namespaces in use.
  194. void
  195. writeClosingNamespace(ostream& output, const vector<string>& ns) {
  196. if (!ns.empty()) {
  197. for (int i = ns.size() - 1; i >= 0; --i) {
  198. output << "} // namespace " << ns[i] << "\n";
  199. }
  200. output << "\n";
  201. }
  202. }
  203. /// \brief Write python file
  204. ///
  205. /// Writes the python file containing the symbol definitions as module level
  206. /// constants. These are objects which register themself at creation time,
  207. /// so they can be replaced by dictionary later.
  208. ///
  209. /// \param file Name of the message file. The source code is written to a file
  210. /// file of the same name but with a .py suffix.
  211. /// \param dictionary The dictionary holding the message definitions.
  212. /// \param output_directory if not null NULL, output files are written
  213. /// to the given directory. If NULL, they are written to the current
  214. /// working directory.
  215. ///
  216. /// \note We don't use the namespace as in C++. We don't need it, because
  217. /// python file/module works as implicit namespace as well.
  218. void
  219. writePythonFile(const string& file, MessageDictionary& dictionary,
  220. const char* output_directory)
  221. {
  222. Filename message_file(file);
  223. Filename python_file(Filename(message_file.name()).useAsDefault(".py"));
  224. if (output_directory != NULL) {
  225. python_file.setDirectory(output_directory);
  226. }
  227. // Open the file for writing
  228. ofstream pyfile(python_file.fullName().c_str());
  229. // Write the comment and imports
  230. pyfile <<
  231. "# File created from " << message_file.fullName() << " on " <<
  232. currentTime() << "\n" <<
  233. "\n" <<
  234. "import isc.log\n" <<
  235. "\n";
  236. vector<string> idents(sortedIdentifiers(dictionary));
  237. BOOST_FOREACH(const string& ident, idents) {
  238. pyfile << ident << " = isc.log.create_message(\"" <<
  239. ident << "\", \"" << quoteString(dictionary.getText(ident)) <<
  240. "\")\n";
  241. }
  242. pyfile.close();
  243. }
  244. /// \brief Write Header File
  245. ///
  246. /// Writes the C++ header file containing the symbol definitions. These are
  247. /// "extern" references to definitions in the .cc file. As such, they should
  248. /// take up no space in the module in which they are included, and redundant
  249. /// references should be removed by the compiler.
  250. ///
  251. /// \param file Name of the message file. The header file is written to a
  252. /// file of the same name but with a .h suffix.
  253. /// \param ns_components Namespace in which the definitions are to be placed.
  254. /// An empty string indicates no namespace.
  255. /// \param dictionary Dictionary holding the message definitions.
  256. /// \param output_directory if not null NULL, output files are written
  257. /// to the given directory. If NULL, they are written to the current
  258. /// working directory.
  259. void
  260. writeHeaderFile(const string& file, const vector<string>& ns_components,
  261. MessageDictionary& dictionary, const char* output_directory)
  262. {
  263. Filename message_file(file);
  264. Filename header_file(Filename(message_file.name()).useAsDefault(".h"));
  265. if (output_directory != NULL) {
  266. header_file.setDirectory(output_directory);
  267. }
  268. // Text to use as the sentinels.
  269. string sentinel_text = sentinel(header_file);
  270. // Open the output file for writing
  271. ofstream hfile(header_file.fullName().c_str());
  272. if (hfile.fail()) {
  273. throw MessageException(LOG_OPEN_OUTPUT_FAIL, header_file.fullName(),
  274. strerror(errno));
  275. }
  276. // Write the header preamble. If there is an error, we'll pick it up
  277. // after the last write.
  278. hfile <<
  279. "// File created from " << message_file.fullName() << " on " <<
  280. currentTime() << "\n" <<
  281. "\n" <<
  282. "#ifndef " << sentinel_text << "\n" <<
  283. "#define " << sentinel_text << "\n" <<
  284. "\n" <<
  285. "#include <log/message_types.h>\n" <<
  286. "\n";
  287. // Write the message identifiers, bounded by a namespace declaration
  288. writeOpeningNamespace(hfile, ns_components);
  289. vector<string> idents = sortedIdentifiers(dictionary);
  290. for (vector<string>::const_iterator j = idents.begin();
  291. j != idents.end(); ++j) {
  292. hfile << "extern const isc::log::MessageID " << *j << ";\n";
  293. }
  294. hfile << "\n";
  295. writeClosingNamespace(hfile, ns_components);
  296. // ... and finally the postamble
  297. hfile << "#endif // " << sentinel_text << "\n";
  298. // Report errors (if any) and exit
  299. if (hfile.fail()) {
  300. throw MessageException(LOG_WRITE_ERROR, header_file.fullName(),
  301. strerror(errno));
  302. }
  303. hfile.close();
  304. }
  305. /// \brief Convert Non Alpha-Numeric Characters to Underscores
  306. ///
  307. /// Simple function for use in a call to transform
  308. char
  309. replaceNonAlphaNum(char c) {
  310. return (isalnum(c) ? c : '_');
  311. }
  312. /// \brief Write Program File
  313. ///
  314. /// Writes the C++ source code file. This defines the text of the message
  315. /// symbols, as well as the initializer object that sets the entries in the
  316. /// global dictionary.
  317. ///
  318. /// The construction of the initializer object loads the dictionary with the
  319. /// message text. However, nothing actually references it. If the initializer
  320. /// were in a file by itself, the lack of things referencing it would cause the
  321. /// linker to ignore it when pulling modules out of the logging library in a
  322. /// static link. By including it in the file with the symbol definitions, the
  323. /// module will get included in the link process to resolve the symbol
  324. /// definitions, and so the initializer object will be included in the final
  325. /// image. (Note that there are no such problems when the logging library is
  326. /// built as a dynamically-linked library: the whole library - including the
  327. /// initializer module - gets mapped into address space when the library is
  328. /// loaded, after which all the initializing code (including the constructors
  329. /// of objects declared outside functions) gets run.)
  330. ///
  331. /// There _may_ be a problem when we come to port this to Windows. Microsoft
  332. /// Visual Studio contains a "Whole Program Optimisation" option, where the
  333. /// optimisation is done at link-time, not compiler-time. In this it _may_
  334. /// decide to remove the initializer object because of a lack of references
  335. /// to it. But until BIND-10 is ported to Windows, we won't know.
  336. ///
  337. /// \param file Name of the message file. The header file is written to a
  338. /// file of the same name but with a .h suffix.
  339. /// \param ns_components Namespace in which the definitions are to be placed.
  340. /// An empty string indicates no namespace.
  341. /// \param dictionary Dictionary holding the message definitions.
  342. /// \param output_directory if not null NULL, output files are written
  343. /// to the given directory. If NULL, they are written to the current
  344. /// working directory.
  345. void
  346. writeProgramFile(const string& file, const vector<string>& ns_components,
  347. MessageDictionary& dictionary,
  348. const char* output_directory)
  349. {
  350. Filename message_file(file);
  351. Filename program_file(Filename(message_file.name()).useAsDefault(".cc"));
  352. if (output_directory) {
  353. program_file.setDirectory(output_directory);
  354. }
  355. // Open the output file for writing
  356. ofstream ccfile(program_file.fullName().c_str());
  357. if (ccfile.fail()) {
  358. throw MessageException(LOG_OPEN_OUTPUT_FAIL, program_file.fullName(),
  359. strerror(errno));
  360. }
  361. // Write the preamble. If there is an error, we'll pick it up after
  362. // the last write.
  363. ccfile <<
  364. "// File created from " << message_file.fullName() << " on " <<
  365. currentTime() << "\n" <<
  366. "\n" <<
  367. "#include <cstddef>\n" <<
  368. "#include <log/message_types.h>\n" <<
  369. "#include <log/message_initializer.h>\n" <<
  370. "\n";
  371. // Declare the message symbols themselves.
  372. writeOpeningNamespace(ccfile, ns_components);
  373. vector<string> idents = sortedIdentifiers(dictionary);
  374. for (vector<string>::const_iterator j = idents.begin();
  375. j != idents.end(); ++j) {
  376. ccfile << "extern const isc::log::MessageID " << *j <<
  377. " = \"" << *j << "\";\n";
  378. }
  379. ccfile << "\n";
  380. writeClosingNamespace(ccfile, ns_components);
  381. // Now the code for the message initialization.
  382. ccfile <<
  383. "namespace {\n" <<
  384. "\n" <<
  385. "const char* values[] = {\n";
  386. // Output the identifiers and the associated text.
  387. idents = sortedIdentifiers(dictionary);
  388. for (vector<string>::const_iterator i = idents.begin();
  389. i != idents.end(); ++i) {
  390. ccfile << " \"" << *i << "\", \"" <<
  391. quoteString(dictionary.getText(*i)) << "\",\n";
  392. }
  393. // ... and the postamble
  394. ccfile <<
  395. " NULL\n" <<
  396. "};\n" <<
  397. "\n" <<
  398. "const isc::log::MessageInitializer initializer(values);\n" <<
  399. "\n" <<
  400. "} // Anonymous namespace\n" <<
  401. "\n";
  402. // Report errors (if any) and exit
  403. if (ccfile.fail()) {
  404. throw MessageException(LOG_WRITE_ERROR, program_file.fullName(),
  405. strerror(errno));
  406. }
  407. ccfile.close();
  408. }
  409. /// \brief Warn of Duplicate Entries
  410. ///
  411. /// If the input file contained duplicate message IDs, only the first will be
  412. /// processed. However, we should warn about it.
  413. ///
  414. /// \param reader Message Reader used to read the file
  415. void
  416. warnDuplicates(MessageReader& reader) {
  417. // Get the duplicates (the overflow) and, if present, sort them into some
  418. // order and remove those which occur more than once (which mean that they
  419. // occur more than twice in the input file).
  420. MessageReader::MessageIDCollection duplicates = reader.getNotAdded();
  421. if (duplicates.size() > 0) {
  422. cout << "Warning: the following duplicate IDs were found:\n";
  423. sort(duplicates.begin(), duplicates.end());
  424. MessageReader::MessageIDCollection::iterator new_end =
  425. unique(duplicates.begin(), duplicates.end());
  426. for (MessageReader::MessageIDCollection::iterator i = duplicates.begin();
  427. i != new_end; ++i) {
  428. cout << " " << *i << "\n";
  429. }
  430. }
  431. }
  432. /// \brief Main Program
  433. ///
  434. /// Parses the options then dispatches to the appropriate function. See the
  435. /// main file header for the invocation.
  436. int
  437. main(int argc, char* argv[]) {
  438. const char* soptions = "hvpd:"; // Short options
  439. optind = 1; // Ensure we start a new scan
  440. int opt; // Value of the option
  441. bool doPython = false;
  442. const char *output_directory = NULL;
  443. while ((opt = getopt(argc, argv, soptions)) != -1) {
  444. switch (opt) {
  445. case 'd':
  446. output_directory = optarg;
  447. break;
  448. case 'p':
  449. doPython = true;
  450. break;
  451. case 'h':
  452. usage();
  453. return (0);
  454. case 'v':
  455. version();
  456. return (0);
  457. default:
  458. // A message will have already been output about the error.
  459. return (1);
  460. }
  461. }
  462. // Do we have the message file?
  463. if (optind < (argc - 1)) {
  464. cout << "Error: excess arguments in command line\n";
  465. usage();
  466. return (1);
  467. } else if (optind >= argc) {
  468. cout << "Error: missing message file\n";
  469. usage();
  470. return (1);
  471. }
  472. string message_file = argv[optind];
  473. try {
  474. // Have identified the file, so process it. First create a local
  475. // dictionary into which the data will be put.
  476. MessageDictionary dictionary;
  477. // Read the data into it.
  478. MessageReader reader(&dictionary);
  479. reader.readFile(message_file);
  480. if (doPython) {
  481. // Warn in case of ignored directives
  482. if (!reader.getNamespace().empty()) {
  483. cerr << "Python mode, ignoring the $NAMESPACE directive" <<
  484. endl;
  485. }
  486. // Write the whole python file
  487. writePythonFile(message_file, dictionary, output_directory);
  488. } else {
  489. // Get the namespace into which the message definitions will be put and
  490. // split it into components.
  491. vector<string> ns_components =
  492. splitNamespace(reader.getNamespace());
  493. // Write the header file.
  494. writeHeaderFile(message_file, ns_components, dictionary,
  495. output_directory);
  496. // Write the file that defines the message symbols and text
  497. writeProgramFile(message_file, ns_components, dictionary,
  498. output_directory);
  499. }
  500. // Finally, warn of any duplicates encountered.
  501. warnDuplicates(reader);
  502. }
  503. catch (const MessageException& e) {
  504. // Create an error message from the ID and the text
  505. MessageDictionary& global = MessageDictionary::globalDictionary();
  506. string text = e.id();
  507. text += ", ";
  508. text += global.getText(e.id());
  509. // Format with arguments
  510. vector<string> args(e.arguments());
  511. for (size_t i(0); i < args.size(); ++ i) {
  512. replacePlaceholder(&text, args[i], i + 1);
  513. }
  514. cerr << text << "\n";
  515. return (1);
  516. }
  517. return (0);
  518. }