00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <sbuild/sbuild-i18n.h>
00023 #include <sbuild/sbuild-log.h>
00024 #include <sbuild/sbuild-types.h>
00025
00026 #include "schroot-base-main.h"
00027
00028 #include <cstdlib>
00029 #include <ctime>
00030 #include <iostream>
00031
00032 #include <syslog.h>
00033
00034 #include <boost/format.hpp>
00035
00036 using std::endl;
00037 using boost::format;
00038 using sbuild::_;
00039 using namespace schroot_base;
00040
00041 main::main (std::string const& program_name,
00042 std::string const& program_usage,
00043 options::ptr const& program_options,
00044 bool use_syslog):
00045 program_name(program_name),
00046 program_usage(program_usage),
00047 program_options(program_options),
00048 use_syslog(use_syslog)
00049 {
00050 }
00051
00052 main::~main ()
00053 {
00054 }
00055
00056 void
00057 main::action_version (std::ostream& stream)
00058 {
00059
00060
00061
00062 format fmt(_("%1% (Debian sbuild) %2% (%3%)\n"));
00063 fmt % this->program_name % VERSION % sbuild::gmdate(RELEASE_DATE);
00064
00065 stream << fmt
00066 << _("Written by Roger Leigh") << '\n' << '\n'
00067
00068 << _("Copyright (C) 2004-2007 Roger Leigh") << '\n'
00069 << _("This is free software; see the source for copying conditions. There is NO\n"
00070 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n")
00071 << std::flush;
00072 }
00073
00074 void
00075 main::action_help (std::ostream& stream)
00076 {
00077 stream
00078 << _("Usage:") << '\n'
00079 << " " << this->program_name << " "
00080 << this->program_usage << std::endl;
00081
00082 stream << this->program_options->get_visible_options() << std::flush;
00083 }
00084
00085 int
00086 main::run (int argc,
00087 char *argv[])
00088 {
00089 try
00090 {
00091 this->program_options->parse(argc, argv);
00092
00093 #ifdef SBUILD_DEBUG
00094 sbuild::debug_level = sbuild::DEBUG_CRITICAL;
00095 #endif
00096
00097 if (this->use_syslog)
00098 openlog(this->program_name.c_str(), LOG_PID|LOG_NDELAY, LOG_AUTHPRIV);
00099
00100 int status = run_impl();
00101
00102 closelog();
00103
00104 return status;
00105 }
00106 catch (std::exception const& e)
00107 {
00108 sbuild::log_exception_error(e);
00109
00110 try
00111 {
00112 dynamic_cast<boost::program_options::error const&>(e);
00113 sbuild::log_info()
00114
00115 << format(_("Run \"%1% --help\" to list usage example and all available options"))
00116 % argv[0]
00117 << endl;
00118 }
00119 catch (std::bad_cast const& discard)
00120 {
00121 }
00122
00123 if (this->use_syslog)
00124 closelog();
00125
00126 return EXIT_FAILURE;
00127 }
00128 }