예제 #1
0
파일: logger.c 프로젝트: 1514louluo/lagopus
static void
s_once_proc(void) {
  char *dbg_lvl_str = getenv("LAGOPUS_LOG_DEBUGLEVEL");
  char *logfile = getenv("LAGOPUS_LOG_FILE");
  uint16_t d = 0;
  lagopus_log_destination_t log_dst = LAGOPUS_LOG_EMIT_TO_UNKNOWN;

  if (IS_VALID_STRING(dbg_lvl_str) == true) {
    uint16_t tmp = 0;
    if (lagopus_str_parse_uint16(dbg_lvl_str, &tmp) == LAGOPUS_RESULT_OK) {
      d = tmp;
    }
  }
  if ((logfile = s_validate_path(logfile, s_is_valid_path)) != NULL) {
    log_dst = LAGOPUS_LOG_EMIT_TO_FILE;
  }

  if (lagopus_log_initialize(log_dst, logfile, false, true, d) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_error(1, "logger initialization error.\n");
  }

  if (d > 0) {
    lagopus_msg_debug(d, "Logger debug level is set to: %d.\n", d);
  }

#ifdef HAVE_PROCFS_SELF_EXE
  if (readlink("/proc/self/exe", s_exefile, PATH_MAX) != -1) {
    (void)lagopus_set_command_name(s_exefile);
    lagopus_msg_debug(10, "set the command name '%s'.\n",
                      lagopus_get_command_name());
  }
#endif /* HAVE_PROCFS_SELF_EXE */
}
예제 #2
0
파일: logger.c 프로젝트: D-TOKIOKA/lagopus
static void
s_once_proc(void) {
  char *dbg_lvl_str = getenv("LAGOPUS_LOG_DEBUGLEVEL");
  char *logfile = getenv("LAGOPUS_LOG_FILE");
  uint16_t d = 0;
  lagopus_log_destination_t log_dst = LAGOPUS_LOG_EMIT_TO_UNKNOWN;

  if (IS_VALID_STRING(dbg_lvl_str) == true) {
    uint16_t tmp = 0;
    if (lagopus_str_parse_uint16(dbg_lvl_str, &tmp) == LAGOPUS_RESULT_OK) {
      d = tmp;
    }
  }
  if ((logfile = s_validate_path(logfile, s_is_valid_path)) != NULL) {
    log_dst = LAGOPUS_LOG_EMIT_TO_FILE;
  }

  if (lagopus_log_initialize(log_dst, logfile, false, true, d) !=
      LAGOPUS_RESULT_OK) {
    lagopus_exit_error(1, "logger initialization error.\n");
  }

  if (d > 0) {
    lagopus_msg_debug(d, "Logger debug level is set to: %d.\n", d);
  }
}
예제 #3
0
int
main(int argc, const char *const argv[]) {
  lagopus_log_destination_t dst = LAGOPUS_LOG_EMIT_TO_UNKNOWN;
  const char *logarg;
  char *p;
  uint16_t cur_debug_level = lagopus_log_get_debug_level();
  int ipcfds[2];

  ipcfds[0] = -1;
  ipcfds[1] = -1;

  s_progname = ((p = strrchr(argv[0], '/')) != NULL) ? ++p : argv[0];

  parse_args(argc, argv);

  if (IS_VALID_STRING(s_logfile) == true) {
    dst = LAGOPUS_LOG_EMIT_TO_FILE;
    logarg = s_logfile;
  } else {
    dst = LAGOPUS_LOG_EMIT_TO_SYSLOG;
    logarg = s_progname;
  }
  /*
   * If the cur_debug_level > 0 the debug level is set by the lagopus
   * runtime so use it as is.
   */
  (void)lagopus_log_initialize(dst, logarg, false, true,
                               (cur_debug_level > 0) ?
                               cur_debug_level : s_debug_level);

  (void)lagopus_signal(SIGHUP, s_hup_handler, NULL);
  (void)lagopus_signal(SIGINT, s_term_handler, NULL);
  (void)lagopus_signal(SIGTERM, s_term_handler, NULL);
  (void)lagopus_signal(SIGQUIT, s_term_handler, NULL);

  if (s_debug_level == 0) {
    pid_t pid = (pid_t)-1;

    if (pipe(ipcfds) != 0) {
      lagopus_perror(LAGOPUS_RESULT_POSIX_API_ERROR);
      return 1;
    }

    pid = fork();
    if (pid > 0) {
      int exit_code;
      ssize_t st;

      (void)close(ipcfds[1]);
      if ((st = read(ipcfds[0], (void *)&exit_code, sizeof(int))) !=
          sizeof(int)) {
        lagopus_perror(LAGOPUS_RESULT_POSIX_API_ERROR);
        return 1;
      }

      return exit_code;

    } else if (pid == 0) {
      s_daemonize(ipcfds[1]);
      if (lagopus_log_get_destination() == LAGOPUS_LOG_EMIT_TO_FILE) {
        (void)lagopus_log_reinitialize();
      }
    } else {
      lagopus_perror(LAGOPUS_RESULT_POSIX_API_ERROR);
      lagopus_msg_error("can' be a daemon.\n");
      return 1;
    }
  }

  return s_do_main(argc, argv, ipcfds[1]);
}
예제 #4
0
파일: check0.c 프로젝트: D-TOKIOKA/lagopus
int
main(int argc, const char *const argv[]) {
  const char *nm = myname(argv[0]);
  (void)argc;

  lagopus_log_set_trace_flags(TRACE_OFPT_HELLO |
                              TRACE_OFPT_ERROR |
                              TRACE_OFPT_METER_MOD);

  lagopus_msg("this should emitted to stderr.\n");

  lagopus_msg_trace(TRACE_OFPT_HELLO, false, "hello test.\n");
  lagopus_msg_trace(TRACE_OFPT_ERROR, false, "error test.\n");
  lagopus_msg_trace(TRACE_OFPT_HELLO |
                    TRACE_OFPT_ERROR,
                    false, "hello|error test.\n");

  /*
   * log to stderr.
   */
  if (IS_LAGOPUS_RESULT_OK(
        lagopus_log_initialize(LAGOPUS_LOG_EMIT_TO_UNKNOWN, NULL,
                               false, true, 1)) == false) {
    lagopus_msg_fatal("what's wrong??\n");
    /* not reached. */
  }
  lagopus_dprint("debug to stderr.\n");

  /*
   * log to file.
   */
  if (IS_LAGOPUS_RESULT_OK(
        lagopus_log_initialize(LAGOPUS_LOG_EMIT_TO_FILE, "./testlog.txt",
                               false, true, 10)) == false) {
    lagopus_msg_fatal("what's wrong??\n");
    /* not reached. */
  }
  lagopus_dprint("debug to file.\n");
  lagopus_msg_debug(5, "debug to file, again.\n");
  lagopus_msg_trace(TRACE_OFPT_HELLO, false, "hello file test.\n");
  lagopus_msg_trace(TRACE_OFPT_ERROR, false, "error file test.\n");
  lagopus_msg_trace(TRACE_OFPT_HELLO |
                    TRACE_OFPT_ERROR,
                    false, "hello|error file test.\n");

  if (IS_LAGOPUS_RESULT_OK(
        lagopus_log_initialize(LAGOPUS_LOG_EMIT_TO_SYSLOG, nm,
                               false, false, 10)) == false) {
    lagopus_msg_fatal("what's wrong??\n");
    /* not reached. */
  }
  lagopus_msg_debug(5, "debug to syslog.\n");
  lagopus_msg_trace(TRACE_OFPT_HELLO, false, "hello syslog test.\n");
  lagopus_msg_trace(TRACE_OFPT_ERROR, false, "error syslog test.\n");
  lagopus_msg_trace(TRACE_OFPT_HELLO |
                    TRACE_OFPT_ERROR,
                    false, "hello|error syslog test.\n");

  /*
   * log to stderr, again.
   */
  if (IS_LAGOPUS_RESULT_OK(
        lagopus_log_initialize(LAGOPUS_LOG_EMIT_TO_UNKNOWN, NULL,
                               false, true, 1)) == false) {
    lagopus_msg_fatal("what's wrong??\n");
    /* not reached. */
  }
  lagopus_dprint("will exit 1 ...\n");
  lagopus_exit_error(1, "exit 1 on purpose.\n");

  return 0;
}