SEvMgr Logo  0.2.0
C++ Simulation-Oriented Discrete Event Management Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Defines
sevmgr_demo.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 #include <fstream>
00008 #include <vector>
00009 #include <list>
00010 #include <string>
00011 //  //// Boost (Extended STL) ////
00012 // Boost Program Options
00013 #include <boost/program_options.hpp>
00014 // StdAir
00015 #include <stdair/stdair_basic_types.hpp>
00016 #include <stdair/basic/ProgressStatusSet.hpp>
00017 #include <stdair/bom/EventStruct.hpp>
00018 #include <stdair/bom/EventQueue.hpp>
00019 #include <stdair/bom/BomDisplay.hpp>
00020 #include <stdair/service/Logger.hpp>
00021 // SEvMgr
00022 #include <sevmgr/SEVMGR_Service.hpp>
00023 #include <sevmgr/config/sevmgr-paths.hpp>
00024 
00025 // //////// Constants //////
00027 const stdair::Filename_T K_SEVMGR_DEFAULT_LOG_FILENAME ("sevmgr_demo.log");
00028 
00030 const int K_SEVMGR_EARLY_RETURN_STATUS = 99;
00031 
00032 
00033 // ///////// Parsing of Options & Configuration /////////
00035 int readConfiguration (int argc, char* argv[],
00036                        stdair::Filename_T& ioLogFilename) {
00037 
00038   // Declare a group of options that will be allowed only on command line
00039   boost::program_options::options_description generic ("Generic options");
00040   generic.add_options()
00041     ("prefix", "print installation prefix")
00042     ("version,v", "print version string")
00043     ("help,h", "produce help message");
00044     
00045   // Declare a group of options that will be allowed both on command
00046   // line and in config file
00047   boost::program_options::options_description config ("Configuration");
00048   config.add_options()
00049     ("log,l",
00050      boost::program_options::value< std::string >(&ioLogFilename)->default_value(K_SEVMGR_DEFAULT_LOG_FILENAME),
00051      "Filepath for the logs")
00052     ;
00053 
00054   // Hidden options, will be allowed both on command line and
00055   // in config file, but will not be shown to the user.
00056   boost::program_options::options_description hidden ("Hidden options");
00057   hidden.add_options()
00058     ("copyright",
00059      boost::program_options::value< std::vector<std::string> >(),
00060      "Show the copyright (license)");
00061         
00062   boost::program_options::options_description cmdline_options;
00063   cmdline_options.add(generic).add(config).add(hidden);
00064 
00065   boost::program_options::options_description config_file_options;
00066   config_file_options.add(config).add(hidden);
00067 
00068   boost::program_options::options_description visible ("Allowed options");
00069   visible.add(generic).add(config);
00070         
00071   boost::program_options::positional_options_description p;
00072   p.add ("copyright", -1);
00073         
00074   boost::program_options::variables_map vm;
00075   boost::program_options::
00076     store (boost::program_options::command_line_parser (argc, argv).
00077            options (cmdline_options).positional(p).run(), vm);
00078 
00079   std::ifstream ifs ("sevmgr.cfg");
00080   boost::program_options::store (parse_config_file (ifs, config_file_options),
00081                                  vm);
00082   boost::program_options::notify (vm);
00083     
00084   if (vm.count ("help")) {
00085     std::cout << visible << std::endl;
00086     return K_SEVMGR_EARLY_RETURN_STATUS;
00087   }
00088 
00089   if (vm.count ("version")) {
00090     std::cout << PACKAGE_NAME << ", version " << PACKAGE_VERSION << std::endl;
00091     return K_SEVMGR_EARLY_RETURN_STATUS;
00092   }
00093 
00094   if (vm.count ("prefix")) {
00095     std::cout << "Installation prefix: " << PREFIXDIR << std::endl;
00096     return K_SEVMGR_EARLY_RETURN_STATUS;
00097   }
00098 
00099   if (vm.count ("log")) {
00100     ioLogFilename = vm["log"].as< std::string >();
00101     std::cout << "Log filename is: " << ioLogFilename << std::endl;
00102   }
00103   
00104   return 0;
00105 }
00106 
00107 
00108 // /////////////// M A I N /////////////////
00109 int main (int argc, char* argv[]) {
00110 
00111   // Output log File
00112   stdair::Filename_T lLogFilename;
00113 
00114   // Call the command-line option parser
00115   const int lOptionParserStatus = readConfiguration (argc, argv, lLogFilename);
00116 
00117   if (lOptionParserStatus == K_SEVMGR_EARLY_RETURN_STATUS) {
00118     return 0;
00119   }
00120 
00121   // Set the log parameters
00122   std::ofstream logOutputFile;
00123   // Open and clean the log outputfile
00124   logOutputFile.open (lLogFilename.c_str());
00125   logOutputFile.clear();
00126 
00127   // Set up the log parameters
00128   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00129 
00133   SEVMGR::SEVMGR_Service sevmgrService (lLogParams);
00134 
00135   // Build a sample BOM tree (empty for now)
00136   sevmgrService.buildSampleBom();
00137 
00138   // Close the Log outputFile
00139   logOutputFile.close();
00140 
00141   /*
00142     Note: as that program is not intended to be run on a server in
00143     production, it is better not to catch the exceptions. When it
00144     happens (that an exception is throwned), that way we get the
00145     call stack.
00146   */
00147 
00148   return 0;
00149 }