main.cpp.tmpl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "h.h"
  5. #ifdef _WIN32
  6. #include "win32_getopt.h"
  7. #endif
  8. void pexit(int code) {
  9. printf("Exit with code %d.\n", code);
  10. exit(code);
  11. }
  12. typedef struct arg_s {
  13. string cmd;
  14. string confile; // -c
  15. string logfile;
  16. int port; // -p
  17. int daemon; // -d
  18. } arg_t;
  19. arg_t g_arg;
  20. const char* options = "hvc:p:d";
  21. const char* options_descr = "\
  22. Options:\n\
  23. -h Print help\n\
  24. -v Print version\n\
  25. -c confile Configure file\n\
  26. -p port Listen port\n\
  27. -d Daemon\n\
  28. ";
  29. string version() {
  30. return asprintf("%s version %s", g_arg.cmd.c_str(), get_compile_version());
  31. }
  32. string usage() {
  33. return asprintf("Usage: %s [%s]", g_arg.cmd.c_str(), options);
  34. }
  35. void print_help() {
  36. puts(usage().c_str());
  37. puts(options_descr);
  38. puts(version().c_str());
  39. }
  40. bool parse_command_line(int argc, char** argv) {
  41. int opt = -1;
  42. while ((opt = getopt(argc, argv, options)) != -1) {
  43. switch (opt) {
  44. case 'h': {
  45. print_help();
  46. pexit(0);
  47. }
  48. break;
  49. case 'v': {
  50. puts(version().c_str());
  51. pexit(0);
  52. }
  53. break;
  54. case 'c': {
  55. g_arg.confile = optarg;
  56. }
  57. break;
  58. case 'p': {
  59. g_arg.port = atoi(optarg);
  60. }
  61. break;
  62. case 'd': {
  63. g_arg.daemon = 1;
  64. }
  65. break;
  66. default: {
  67. puts("Unrecognized option!");
  68. pexit(-10);
  69. }
  70. break;
  71. }
  72. }
  73. return true;
  74. }
  75. int main(int argc, char** argv) {
  76. string strCmd((const char*)argv[0]);
  77. g_arg.cmd = filename(strCmd);
  78. //g_arg.confile = g_arg.cmd + ".conf";
  79. g_arg.logfile = g_arg.cmd + ".log";
  80. g_arg.port = 0;
  81. g_arg.daemon = 0;
  82. hlog_set_file(g_arg.logfile.c_str());
  83. hlog_set_level(LOG_LEVEL_INFO);
  84. hlogi("%s", version().c_str());
  85. if (!parse_command_line(argc, argv)) {
  86. printf("Parse command line error, please use -h to get help.\n");
  87. pexit(-10);
  88. }
  89. IniParser ini;
  90. if (!g_arg.confile.empty()) {
  91. int iRet = ini.LoadFromFile(g_arg.confile.c_str());
  92. if (iRet != 0) {
  93. printf("Load confile [%s] failed: %d\n", g_arg.confile.c_str(), iRet);
  94. pexit(-20);
  95. }
  96. }
  97. if (g_arg.port == 0) {
  98. g_arg.port = atoi(ini.GetValue("port").c_str());
  99. }
  100. if (g_arg.port == 0) {
  101. puts("Please config listen port!");
  102. pexit(-30);
  103. }
  104. #ifdef __unix__
  105. if (g_arg.daemon) {
  106. daemon(1, 1);
  107. }
  108. #endif
  109. pexit(0);
  110. }