1
0

hmain_test.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "hv.h"
  2. #include "hmain.h"
  3. #include "iniparser.h"
  4. typedef struct conf_ctx_s {
  5. IniParser* parser;
  6. int loglevel;
  7. int worker_processes;
  8. int worker_threads;
  9. int port;
  10. } conf_ctx_t;
  11. conf_ctx_t g_conf_ctx;
  12. inline void conf_ctx_init(conf_ctx_t* ctx) {
  13. ctx->parser = new IniParser;
  14. ctx->loglevel = LOG_LEVEL_DEBUG;
  15. ctx->worker_processes = 0;
  16. ctx->worker_threads = 0;
  17. ctx->port = 0;
  18. }
  19. static void print_version();
  20. static void print_help();
  21. static int parse_confile(const char* confile);
  22. static void worker_fn(void* userdata);
  23. // short options
  24. static const char options[] = "hvc:ts:dp:";
  25. // long options
  26. static const option_t long_options[] = {
  27. {'h', "help", NO_ARGUMENT},
  28. {'v', "version", NO_ARGUMENT},
  29. {'c', "confile", REQUIRED_ARGUMENT},
  30. {'t', "test", NO_ARGUMENT},
  31. {'s', "signal", REQUIRED_ARGUMENT},
  32. {'d', "daemon", NO_ARGUMENT},
  33. {'p', "port", REQUIRED_ARGUMENT}
  34. };
  35. static const char detail_options[] = R"(
  36. -h|--help Print this information
  37. -v|--version Print version
  38. -c|--confile <confile> Set configure file, default etc/{program}.conf
  39. -t|--test Test configure file and exit
  40. -s|--signal <signal> Send <signal> to process,
  41. <signal>=[start,stop,restart,status,reload]
  42. -d|--daemon Daemonize
  43. -p|--port <port> Set listen port
  44. )";
  45. void print_version() {
  46. printf("%s version %s\n", g_main_ctx.program_name, hv_compile_version());
  47. }
  48. void print_help() {
  49. printf("Usage: %s [%s]\n", g_main_ctx.program_name, options);
  50. printf("Options:\n%s\n", detail_options);
  51. }
  52. int parse_confile(const char* confile) {
  53. int ret = g_conf_ctx.parser->LoadFromFile(confile);
  54. if (ret != 0) {
  55. printf("Load confile [%s] failed: %d\n", confile, ret);
  56. exit(-40);
  57. }
  58. // logfile
  59. string str = g_conf_ctx.parser->GetValue("logfile");
  60. if (!str.empty()) {
  61. strncpy(g_main_ctx.logfile, str.c_str(), sizeof(g_main_ctx.logfile));
  62. }
  63. hlog_set_file(g_main_ctx.logfile);
  64. // loglevel
  65. str = g_conf_ctx.parser->GetValue("loglevel");
  66. if (!str.empty()) {
  67. hlog_set_level_by_str(str.c_str());
  68. }
  69. // log_filesize
  70. str = g_conf_ctx.parser->GetValue("log_filesize");
  71. if (!str.empty()) {
  72. hlog_set_max_filesize_by_str(str.c_str());
  73. }
  74. // log_remain_days
  75. str = g_conf_ctx.parser->GetValue("log_remain_days");
  76. if (!str.empty()) {
  77. hlog_set_remain_days(atoi(str.c_str()));
  78. }
  79. // log_fsync
  80. str = g_conf_ctx.parser->GetValue("log_fsync");
  81. if (!str.empty()) {
  82. logger_enable_fsync(hlog, getboolean(str.c_str()));
  83. }
  84. // first log here
  85. hlogi("%s version: %s", g_main_ctx.program_name, hv_compile_version());
  86. hlog_fsync();
  87. // worker_processes
  88. int worker_processes = 0;
  89. str = g_conf_ctx.parser->GetValue("worker_processes");
  90. if (str.size() != 0) {
  91. if (strcmp(str.c_str(), "auto") == 0) {
  92. worker_processes = get_ncpu();
  93. hlogd("worker_processes=ncpu=%d", worker_processes);
  94. }
  95. else {
  96. worker_processes = atoi(str.c_str());
  97. }
  98. }
  99. g_conf_ctx.worker_processes = LIMIT(0, worker_processes, MAXNUM_WORKER_PROCESSES);
  100. // worker_threads
  101. int worker_threads = g_conf_ctx.parser->Get<int>("worker_threads");
  102. g_conf_ctx.worker_threads = LIMIT(0, worker_threads, 16);
  103. // port
  104. int port = 0;
  105. const char* szPort = get_arg("p");
  106. if (szPort) {
  107. port = atoi(szPort);
  108. }
  109. if (port == 0) {
  110. port = g_conf_ctx.parser->Get<int>("port");
  111. }
  112. if (port == 0) {
  113. printf("Please config listen port!\n");
  114. exit(-10);
  115. }
  116. g_conf_ctx.port = port;
  117. hlogi("parse_confile('%s') OK", confile);
  118. return 0;
  119. }
  120. static void on_reload(void* userdata) {
  121. hlogi("reload confile [%s]", g_main_ctx.confile);
  122. parse_confile(g_main_ctx.confile);
  123. }
  124. int main(int argc, char** argv) {
  125. // g_main_ctx
  126. main_ctx_init(argc, argv);
  127. if (argc == 1) {
  128. print_help();
  129. exit(10);
  130. }
  131. // int ret = parse_opt(argc, argv, options);
  132. int ret = parse_opt_long(argc, argv, long_options, ARRAY_SIZE(long_options));
  133. if (ret != 0) {
  134. print_help();
  135. exit(ret);
  136. }
  137. /*
  138. printf("---------------arg------------------------------\n");
  139. printf("%s\n", g_main_ctx.cmdline);
  140. for (auto& pair : g_main_ctx.arg_kv) {
  141. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  142. }
  143. for (auto& item : g_main_ctx.arg_list) {
  144. printf("%s\n", item.c_str());
  145. }
  146. printf("================================================\n");
  147. printf("---------------env------------------------------\n");
  148. for (auto& pair : g_main_ctx.env_kv) {
  149. printf("%s=%s\n", pair.first.c_str(), pair.second.c_str());
  150. }
  151. printf("================================================\n");
  152. */
  153. // help
  154. if (get_arg("h")) {
  155. print_help();
  156. exit(0);
  157. }
  158. // version
  159. if (get_arg("v")) {
  160. print_version();
  161. exit(0);
  162. }
  163. // g_conf_ctx
  164. conf_ctx_init(&g_conf_ctx);
  165. const char* confile = get_arg("c");
  166. if (confile) {
  167. strncpy(g_main_ctx.confile, confile, sizeof(g_main_ctx.confile));
  168. }
  169. parse_confile(g_main_ctx.confile);
  170. // test
  171. if (get_arg("t")) {
  172. printf("Test confile [%s] OK!\n", g_main_ctx.confile);
  173. exit(0);
  174. }
  175. // signal
  176. signal_init(on_reload);
  177. const char* signal = get_arg("s");
  178. if (signal) {
  179. signal_handle(signal);
  180. }
  181. #ifdef OS_UNIX
  182. // daemon
  183. if (get_arg("d")) {
  184. // nochdir, noclose
  185. int ret = daemon(1, 1);
  186. if (ret != 0) {
  187. printf("daemon error: %d\n", ret);
  188. exit(-10);
  189. }
  190. }
  191. #endif
  192. // pidfile
  193. create_pidfile();
  194. master_workers_run(worker_fn, (void*)(intptr_t)100L, g_conf_ctx.worker_processes, g_conf_ctx.worker_threads);
  195. return 0;
  196. }
  197. void worker_fn(void* userdata) {
  198. long num = (long)(intptr_t)(userdata);
  199. while (1) {
  200. printf("num=%ld pid=%ld tid=%ld\n", num, hv_getpid(), hv_gettid());
  201. hv_delay(60000);
  202. }
  203. }