diff --git a/daemon/configure.ac b/daemon/configure.ac index a06fe449c80fd9334c5be2658879084583e8642b..f14d3faba8bca7058ea67562ad241e7c8938ad80 100644 --- a/daemon/configure.ac +++ b/daemon/configure.ac @@ -50,7 +50,8 @@ AC_FUNC_ALLOCA AC_HEADER_STDC AC_CHECK_HEADERS([arpa/inet.h fcntl.h libintl.h limits.h malloc.h memory.h \ netdb.h netinet/in.h stdlib.h string.h strings.h \ - sys/ioctl.h sys/socket.h sys/time.h unistd.h utime.h ostream]) + sys/ioctl.h sys/socket.h sys/time.h unistd.h utime.h \ + ostream getopt.h]) dnl Check for typedefs, structures, and compiler characteristics AC_HEADER_STAT diff --git a/daemon/src/dbus/dbusmanager.cpp b/daemon/src/dbus/dbusmanager.cpp index becd522679892b2a071910dc9c30c3e3c23223f7..b7e55ba6d2022f4bf772122763a32883cf5d97b8 100644 --- a/daemon/src/dbus/dbusmanager.cpp +++ b/daemon/src/dbus/dbusmanager.cpp @@ -86,7 +86,7 @@ void DBusManager::exec() ERROR("%s: %s, exiting\n", err.name(), err.what()); ::exit(EXIT_FAILURE); } catch (const std::exception &err) { - ERROR("%s: %s, exiting\n", err.what()); + ERROR("%s: exiting\n", err.what()); ::exit(EXIT_FAILURE); } } @@ -100,7 +100,7 @@ DBusManager::exit() ERROR("%s: %s, exiting\n", err.name(), err.what()); ::exit(EXIT_FAILURE); } catch (const std::exception &err) { - ERROR("%s: %s, exiting\n", err.what()); + ERROR("%s: exiting\n", err.what()); ::exit(EXIT_FAILURE); } } diff --git a/daemon/src/main.cpp b/daemon/src/main.cpp index 05af64d9b04eb90390d5414ec6cb03e2f4715edd..236421926909536574cefaa37535dd60bfe8f14a 100644 --- a/daemon/src/main.cpp +++ b/daemon/src/main.cpp @@ -35,50 +35,98 @@ #endif #include <iostream> -#include <memory> // for auto_ptr -#include <string> -// #include <commoncpp/common.h> +#include <getopt.h> #include "fileutils.h" - #include "dbus/dbusmanager.h" #include "manager.h" -/* -ost::CommandOptionNoArg console( - "console", "c", "Log in console (instead of syslog)" -); - -ost::CommandOptionNoArg debug( - "debug", "d", "Debug mode (more verbose)" -); - -ost::CommandOptionNoArg help( - "help", "h", "Print help" -); -*/ -int main(int /*argc*/, char **argv) + +namespace { + void print_title() + { + std::cout << "SFLphone Daemon " << VERSION << + ", by Savoir-Faire Linux 2004-2012" << std::endl << + "http://www.sflphone.org/" << std::endl; + } + + void print_usage() + { + std::cout << std::endl << + "-c, --console \t- Log in console (instead of syslog)" << std::endl << + "-d, --debug \t- Debug mode (more verbose)" << std::endl << + "-h, --help \t- Print help" << std::endl; + } + + // Parse command line arguments, setting debug options or printing a help + // message accordingly. + // returns true if we should quit (i.e. help was printed), false otherwise + bool parse_args(int argc, char *argv[]) + { + int consoleFlag = false; + int debugFlag = false; + int helpFlag = false; + int versionFlag = false; + static const struct option long_options[] = { + /* These options set a flag. */ + {"debug", no_argument, NULL, 'd'}, + {"console", no_argument, NULL, 'c'}, + {"help", no_argument, NULL, 'h'}, + {"version", no_argument, NULL, 'v'}, + {0, 0, 0, 0} /* Sentinel */ + }; + + while (true) { + /* getopt_long stores the option index here. */ + int option_index = 0; + int c = getopt_long(argc, argv, "dchv", long_options, &option_index); + + /* Detect the end of the options. */ + if (c == -1) + break; + + switch (c) { + case 'd': + debugFlag = true; + break; + + case 'c': + consoleFlag = true; + break; + + case 'h': + case '?': + helpFlag = true; + break; + + case 'v': + versionFlag = true; + break; + + default: + break; + } + } + + bool quit = false; + if (helpFlag) { + print_usage(); + quit = true; + } else if (versionFlag) { + // We've always print the title/version, so we can just exit + quit = true; + } else { + Logger::setConsoleLog(consoleFlag); + Logger::setDebugMode(debugFlag); + } + return quit; + } +} + +int main(int argc, char *argv []) { fileutils::set_program_dir(argv[0]); - // makeCommandOptionParse allocates the object with operator new, so - // auto_ptr is fine in this context. - // TODO: This should eventually be replaced with std::unique_ptr for C++0x - // std::auto_ptr<ost::CommandOptionParse> args(ost::makeCommandOptionParse(argc, argv, "")); - - printf("SFLphone Daemon " VERSION ", by Savoir-Faire Linux 2004-2012\n" \ - "http://www.sflphone.org/\n"); -/* - if (help.numSet) { - std::cerr << args->printUsage(); + print_title(); + if (parse_args(argc, argv)) return 0; - } else if (args->argsHaveError()) { - std::cerr << args->printErrors(); - std::cerr << args->printUsage(); - return 1; - } -*/ - // Logger::setConsoleLog(console.numSet); - // Logger::setDebugMode(debug.numSet); - Logger::setConsoleLog(1); - Logger::setDebugMode(1); if (!fileutils::create_pidfile()) return 1; @@ -89,7 +137,8 @@ int main(int /*argc*/, char **argv) std::cerr << e.what() << std::endl; return 1; } catch (...) { - std::cerr << "An exception occured when initializing the system." << std::endl; + std::cerr << "An exception occured when initializing " PACKAGE << + std::endl; return 1; }