IpJournalist.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // Copyright (C) 2004, 2009 International Business Machines and others.
  2. // All Rights Reserved.
  3. // This code is published under the Eclipse Public License.
  4. //
  5. // $Id: IpJournalist.hpp 2204 2013-04-13 13:49:26Z stefan $
  6. //
  7. // Authors: Carl Laird, Andreas Waechter IBM 2004-08-13
  8. #ifndef __IPJOURNALIST_HPP__
  9. #define __IPJOURNALIST_HPP__
  10. #include "IpoptConfig.h"
  11. #include "IpTypes.hpp"
  12. #include "IpReferenced.hpp"
  13. #include "IpSmartPtr.hpp"
  14. #ifdef HAVE_CSTDARG
  15. # include <cstdarg>
  16. #else
  17. # ifdef HAVE_STDARG_H
  18. # include <stdarg.h>
  19. # else
  20. # include <cstdarg> // if this header is included by someone who does not define HAVE_CSTDARG or HAVE_STDARG, let's hope that cstdarg is available
  21. # endif
  22. #endif
  23. #ifdef HAVE_CSTDIO
  24. # include <cstdio>
  25. #else
  26. # ifdef HAVE_STDIO_H
  27. # include <stdio.h>
  28. # else
  29. # include <cstdio> // if this header is included by someone who does not define HAVE_CSTDIO or HAVE_STDIO, let's hope that cstdio is available
  30. # endif
  31. #endif
  32. #include <string>
  33. #include <vector>
  34. #include <ostream>
  35. namespace Ipopt
  36. {
  37. // forward declarations
  38. class Journal;
  39. class FileJournal;
  40. /**@name Journalist Enumerations. */
  41. //@{
  42. /** Print Level Enum. */
  43. enum EJournalLevel {
  44. J_INSUPPRESSIBLE=-1,
  45. J_NONE=0,
  46. J_ERROR,
  47. J_STRONGWARNING,
  48. J_SUMMARY,
  49. J_WARNING,
  50. J_ITERSUMMARY,
  51. J_DETAILED,
  52. J_MOREDETAILED,
  53. J_VECTOR,
  54. J_MOREVECTOR,
  55. J_MATRIX,
  56. J_MOREMATRIX,
  57. J_ALL,
  58. J_LAST_LEVEL
  59. };
  60. /** Category Selection Enum. */
  61. enum EJournalCategory {
  62. J_DBG=0,
  63. J_STATISTICS,
  64. J_MAIN,
  65. J_INITIALIZATION,
  66. J_BARRIER_UPDATE,
  67. J_SOLVE_PD_SYSTEM,
  68. J_FRAC_TO_BOUND,
  69. J_LINEAR_ALGEBRA,
  70. J_LINE_SEARCH,
  71. J_HESSIAN_APPROXIMATION,
  72. J_SOLUTION,
  73. J_DOCUMENTATION,
  74. J_NLP,
  75. J_TIMING_STATISTICS,
  76. J_USER_APPLICATION /** This can be used by the user's application*/ ,
  77. J_USER1 /** This can be used by the user's application*/ ,
  78. J_USER2 /** This can be used by the user's application*/ ,
  79. J_USER3 /** This can be used by the user's application*/ ,
  80. J_USER4 /** This can be used by the user's application*/ ,
  81. J_USER5 /** This can be used by the user's application*/ ,
  82. J_USER6 /** This can be used by the user's application*/ ,
  83. J_USER7 /** This can be used by the user's application*/ ,
  84. J_USER8 /** This can be used by the user's application*/ ,
  85. J_USER9 /** This can be used by the user's application*/ ,
  86. J_USER10 /** This can be used by the user's application*/ ,
  87. J_USER11 /** This can be used by the user's application*/ ,
  88. J_USER12 /** This can be used by the user's application*/ ,
  89. J_USER13 /** This can be used by the user's application*/ ,
  90. J_USER14 /** This can be used by the user's application*/ ,
  91. J_USER15 /** This can be used by the user's application*/ ,
  92. J_USER16 /** This can be used by the user's application*/ ,
  93. J_USER17 /** This can be used by the user's application*/ ,
  94. J_LAST_CATEGORY
  95. };
  96. //@}
  97. /** Class responsible for all message output.
  98. * This class is responsible for all messaging and output.
  99. * The "printing" code or "author" should send ALL messages to the
  100. * Journalist, indicating an appropriate category and print level.
  101. * The journalist then decides, based on reader specified
  102. * acceptance criteria, which message is actually printed in which
  103. * journals.
  104. * This allows the printing code to send everything, while the
  105. * "reader" can decide what they really want to see.
  106. *
  107. * Authors:
  108. * Authors use the
  109. * Journals: You can add as many Journals as you like to the
  110. * Journalist with the AddJournal or the AddFileJournal methods.
  111. * Each one represents a different printing location (or file).
  112. * Then, you can call the "print" methods of the Journalist to output
  113. * information to each of the journals.
  114. *
  115. * Acceptance Criteria: Each print message should be flagged
  116. * appropriately with an EJournalCategory and EJournalLevel.
  117. *
  118. * The AddFileJournal
  119. * method returns a pointer to the newly created Journal object
  120. * (if successful) so you can set Acceptance criteria for that
  121. * particular location.
  122. *
  123. */
  124. class Journalist : public ReferencedObject
  125. {
  126. public:
  127. /**@name Constructor / Desructor. */
  128. //@{
  129. /** Constructor. */
  130. Journalist();
  131. /** Destructor... */
  132. virtual ~Journalist();
  133. //@}
  134. /**@name Author Methods.
  135. * These methods are used by authoring code, or code that wants
  136. * to report some information.
  137. */
  138. //@{
  139. /** Method to print a formatted string */
  140. virtual void Printf(EJournalLevel level, EJournalCategory category,
  141. const char* format, ...) const;
  142. /** Method to print a long string including indentation. The
  143. * string is printed starting at the current position. If the
  144. * position (counting started at the current position) exceeds
  145. * max_length, a new line is inserted, and indent_spaces many
  146. * spaces are printed before the string is continued. This is
  147. * for example used during the printing of the option
  148. * documentation. */
  149. virtual void PrintStringOverLines(EJournalLevel level, EJournalCategory category,
  150. Index indent_spaces, Index max_length,
  151. const std::string& line) const;
  152. /** Method to print a formatted string with indentation */
  153. virtual void PrintfIndented(EJournalLevel level,
  154. EJournalCategory category,
  155. Index indent_level,
  156. const char* format, ...) const;
  157. /** Method to print a formatted string
  158. * using the va_list argument. */
  159. virtual void VPrintf(EJournalLevel level,
  160. EJournalCategory category,
  161. const char* pformat,
  162. va_list ap) const;
  163. /** Method to print a formatted string with indentation,
  164. * using the va_list argument. */
  165. virtual void VPrintfIndented(EJournalLevel level,
  166. EJournalCategory category,
  167. Index indent_level,
  168. const char* pformat,
  169. va_list ap) const;
  170. /** Method that returns true if there is a Journal that would
  171. * write output for the given JournalLevel and JournalCategory.
  172. * This is useful if expensive computation would be required for
  173. * a particular output. The author code can check with this
  174. * method if the computations are indeed required.
  175. */
  176. virtual bool ProduceOutput(EJournalLevel level,
  177. EJournalCategory category) const;
  178. /** Method that flushes the current buffer for all Journalists.
  179. Calling this method after one optimization run helps to avoid
  180. cluttering output with that produced by other parts of the
  181. program (e.g. written in Fortran) */
  182. virtual void FlushBuffer() const;
  183. //@}
  184. /**@name Reader Methods.
  185. * These methods are used by the reader. The reader will setup the
  186. * journalist with each output file and the acceptance
  187. * criteria for that file.
  188. *
  189. * Use these methods to setup the journals (files or other output).
  190. * These are the internal objects that keep track of the print levels
  191. * for each category. Then use the internal Journal objects to
  192. * set specific print levels for each category (or keep defaults).
  193. *
  194. */
  195. //@{
  196. /** Add a new journal. The location_name is a string identifier,
  197. * which can be used to obtain the pointer to the new Journal at
  198. * a later point using the GetJournal method.
  199. * The default_level is
  200. * used to initialize the * printing level for all categories.
  201. */
  202. virtual bool AddJournal(const SmartPtr<Journal> jrnl);
  203. /** Add a new FileJournal. fname is the name
  204. * of the * file to which this Journal corresponds. Use
  205. * fname="stdout" * for stdout, and use fname="stderr" for
  206. * stderr. This method * returns the Journal pointer so you can
  207. * set specific acceptance criteria. It returns NULL if there
  208. * was a problem creating a new Journal.
  209. */
  210. virtual SmartPtr<Journal> AddFileJournal(
  211. const std::string& location_name, /**< journal identifier */
  212. const std::string& fname, /**< file name */
  213. EJournalLevel default_level = J_WARNING /**< default journal level */
  214. );
  215. /** Get an existing journal. You can use this method to change
  216. * the acceptance criteria at runtime.
  217. */
  218. virtual SmartPtr<Journal> GetJournal(const std::string& location_name);
  219. /** Delete all journals curently known by the journalist. */
  220. virtual void DeleteAllJournals();
  221. //@}
  222. private:
  223. /**@name Default Compiler Generated Methods
  224. * (Hidden to avoid implicit creation/calling).
  225. * These methods are not implemented and
  226. * we do not want the compiler to implement
  227. * them for us, so we declare them private
  228. * and do not define them. This ensures that
  229. * they will not be implicitly created/called. */
  230. //@{
  231. /** Copy Constructor */
  232. Journalist(const Journalist&);
  233. /** Overloaded Equals Operator */
  234. void operator=(const Journalist&);
  235. //@}
  236. //** Private Data Members. */
  237. //@{
  238. std::vector< SmartPtr<Journal> > journals_;
  239. //@}
  240. };
  241. /** Journal class (part of the Journalist implementation.). This
  242. * class is the base class for all Journals. It controls the
  243. * acceptance criteria for print statements etc. Derived classes
  244. * like the FileJournal - output those messages to specific locations
  245. */
  246. class Journal : public ReferencedObject
  247. {
  248. public:
  249. /** Constructor. */
  250. Journal(const std::string& name, EJournalLevel default_level);
  251. /** Destructor. */
  252. virtual ~Journal();
  253. /** Get the name of the Journal */
  254. virtual std::string Name();
  255. /** Set the print level for a particular category. */
  256. virtual void SetPrintLevel(
  257. EJournalCategory category, EJournalLevel level
  258. );
  259. /** Set the print level for all category. */
  260. virtual void SetAllPrintLevels(
  261. EJournalLevel level
  262. );
  263. /**@name Journal Output Methods. These methods are called by the
  264. * Journalist who first checks if the output print level and category
  265. * are acceptable.
  266. * Calling the Print methods explicitly (instead of through the
  267. * Journalist will output the message regardless of print level
  268. * and category. You should use the Journalist to print & flush instead
  269. */
  270. //@{
  271. /** Ask if a particular print level/category is accepted by the
  272. * journal.
  273. */
  274. virtual bool IsAccepted(
  275. EJournalCategory category, EJournalLevel level
  276. ) const;
  277. /** Print to the designated output location */
  278. virtual void Print(EJournalCategory category, EJournalLevel level,
  279. const char* str)
  280. {
  281. PrintImpl(category, level, str);
  282. }
  283. /** Printf to the designated output location */
  284. virtual void Printf(EJournalCategory category, EJournalLevel level,
  285. const char* pformat, va_list ap)
  286. {
  287. PrintfImpl(category, level, pformat, ap);
  288. }
  289. /** Flush output buffer.*/
  290. virtual void FlushBuffer()
  291. {
  292. FlushBufferImpl();
  293. }
  294. //@}
  295. protected:
  296. /**@name Implementation version of Print methods. Derived classes
  297. * should overload the Impl methods.
  298. */
  299. //@{
  300. /** Print to the designated output location */
  301. virtual void PrintImpl(EJournalCategory category, EJournalLevel level,
  302. const char* str)=0;
  303. /** Printf to the designated output location */
  304. virtual void PrintfImpl(EJournalCategory category, EJournalLevel level,
  305. const char* pformat, va_list ap)=0;
  306. /** Flush output buffer.*/
  307. virtual void FlushBufferImpl()=0;
  308. //@}
  309. private:
  310. /**@name Default Compiler Generated Methods
  311. * (Hidden to avoid implicit creation/calling).
  312. * These methods are not implemented and
  313. * we do not want the compiler to implement
  314. * them for us, so we declare them private
  315. * and do not define them. This ensures that
  316. * they will not be implicitly created/called. */
  317. //@{
  318. /** Default Constructor */
  319. Journal();
  320. /** Copy Constructor */
  321. Journal(const Journal&);
  322. /** Overloaded Equals Operator */
  323. void operator=(const Journal&);
  324. //@}
  325. /** Name of the output location */
  326. std::string name_;
  327. /** vector of integers indicating the level for each category */
  328. Index print_levels_[J_LAST_CATEGORY];
  329. };
  330. /** FileJournal class. This is a particular Journal implementation that
  331. * writes to a file for output. It can write to (stdout, stderr, or disk)
  332. * by using "stdout" and "stderr" as filenames.
  333. */
  334. class FileJournal : public Journal
  335. {
  336. public:
  337. /** Constructor. */
  338. FileJournal(const std::string& name, EJournalLevel default_level);
  339. /** Destructor. */
  340. virtual ~FileJournal();
  341. /** Open a new file for the output location.
  342. * Special Names: stdout means stdout,
  343. * : stderr means stderr.
  344. *
  345. * Return code is false only if the file with the given name
  346. * could not be opened.
  347. */
  348. virtual bool Open(const char* fname);
  349. protected:
  350. /**@name Implementation version of Print methods - Overloaded from
  351. * Journal base class.
  352. */
  353. //@{
  354. /** Print to the designated output location */
  355. virtual void PrintImpl(EJournalCategory category, EJournalLevel level,
  356. const char* str);
  357. /** Printf to the designated output location */
  358. virtual void PrintfImpl(EJournalCategory category, EJournalLevel level,
  359. const char* pformat, va_list ap);
  360. /** Flush output buffer.*/
  361. virtual void FlushBufferImpl();
  362. //@}
  363. private:
  364. /**@name Default Compiler Generated Methods
  365. * (Hidden to avoid implicit creation/calling).
  366. * These methods are not implemented and
  367. * we do not want the compiler to implement
  368. * them for us, so we declare them private
  369. * and do not define them. This ensures that
  370. * they will not be implicitly created/called. */
  371. //@{
  372. /** Default Constructor */
  373. FileJournal();
  374. /** Copy Constructor */
  375. FileJournal(const FileJournal&);
  376. /** Overloaded Equals Operator */
  377. void operator=(const FileJournal&);
  378. //@}
  379. /** FILE pointer for the output destination */
  380. FILE* file_;
  381. };
  382. /** StreamJournal class. This is a particular Journal implementation that
  383. * writes to a stream for output.
  384. */
  385. class StreamJournal : public Journal
  386. {
  387. public:
  388. /** Constructor. */
  389. StreamJournal(const std::string& name, EJournalLevel default_level);
  390. /** Destructor. */
  391. virtual ~StreamJournal()
  392. {}
  393. /** Setting the output stream pointer */
  394. void SetOutputStream(std::ostream* os);
  395. protected:
  396. /**@name Implementation version of Print methods - Overloaded from
  397. * Journal base class.
  398. */
  399. //@{
  400. /** Print to the designated output location */
  401. virtual void PrintImpl(EJournalCategory category, EJournalLevel level,
  402. const char* str);
  403. /** Printf to the designated output location */
  404. virtual void PrintfImpl(EJournalCategory category, EJournalLevel level,
  405. const char* pformat, va_list ap);
  406. /** Flush output buffer.*/
  407. virtual void FlushBufferImpl();
  408. //@}
  409. private:
  410. /**@name Default Compiler Generated Methods
  411. * (Hidden to avoid implicit creation/calling).
  412. * These methods are not implemented and
  413. * we do not want the compiler to implement
  414. * them for us, so we declare them private
  415. * and do not define them. This ensures that
  416. * they will not be implicitly created/called. */
  417. //@{
  418. /** Default Constructor */
  419. StreamJournal();
  420. /** Copy Constructor */
  421. StreamJournal(const StreamJournal&);
  422. /** Overloaded Equals Operator */
  423. void operator=(const StreamJournal&);
  424. //@}
  425. /** pointer to output stream for the output destination */
  426. std::ostream* os_;
  427. /** buffer for sprintf. Being generous in size here... */
  428. char buffer_[32768];
  429. };
  430. }
  431. #endif