Beispiel #1
0
static inline bool
s_log_init(mccp_log_destination_t dst,
           const char *arg,
           bool multi_process,
           bool emit_date,
           uint64_t debug_level,
           uint64_t trace_flags) {
  bool ret = false;

  if (s_is_fork_initialized == false) {
    (void)pthread_atfork(NULL, NULL, s_child_at_fork);
    s_is_fork_initialized = true;
  }

  if (dst == MCCP_LOG_EMIT_TO_FILE ||
      dst == MCCP_LOG_EMIT_TO_UNKNOWN) {
    int ofd = -INT_MAX;

    s_log_final();

    if (IS_VALID_STRING(arg) == true &&
        dst == MCCP_LOG_EMIT_TO_FILE &&
        s_is_file(arg) == true) {
      ofd = open(arg, O_RDWR | O_CREAT | O_APPEND, 0600);
      if (ofd >= 0) {
        s_log_fd = fdopen(ofd, "a+");
        if (s_log_fd != NULL) {
          ret = true;
        }
      }
    } else {
      s_log_fd = NULL;	/* use stderr. */
      ret = true;
    }
  } else if (dst == MCCP_LOG_EMIT_TO_SYSLOG) {
    if (IS_VALID_STRING(arg) == true) {

      s_log_final();

      openlog(arg, 0, LOG_USER);

      ret = true;
    }
  }

  if (ret == true) {
    s_log_dst = dst;
    free((void *)s_log_arg);
    s_log_arg = (IS_VALID_STRING(arg) == true) ? strdup(arg) : NULL;
    s_do_multi_process = multi_process;
    s_do_date = emit_date;
    s_dbg_level = debug_level;
    s_trace_flags = trace_flags;
  }

  return ret;
}
Beispiel #2
0
static inline bool
s_log_init(lagopus_log_destination_t dst,
           const char *arg,
           bool multi_process,
           bool emit_date,
           uint16_t debug_level) {
  bool ret = false;

  if (s_is_fork_initialized == false) {
    (void)pthread_atfork(NULL, NULL, s_child_at_fork);
    s_is_fork_initialized = true;
  }

  if (dst == LAGOPUS_LOG_EMIT_TO_FILE ||
      dst == LAGOPUS_LOG_EMIT_TO_UNKNOWN) {
    int ofd = -INT_MAX;

    s_log_final();

    if (IS_VALID_STRING(arg) == true &&
        dst == LAGOPUS_LOG_EMIT_TO_FILE &&
        s_is_file(arg) == true) {
      s_set_arg(arg);
      ofd = open(s_log_arg, O_RDWR | O_CREAT | O_APPEND, 0600);
      if (ofd >= 0) {
        s_log_fd = fdopen(ofd, "a+");
        if (s_log_fd != NULL) {
          ret = true;
        }
      }
    } else {
      s_log_fd = NULL;	/* use stderr. */
      ret = true;
    }
  } else if (dst == LAGOPUS_LOG_EMIT_TO_SYSLOG) {
    if (IS_VALID_STRING(arg) == true) {

      s_log_final();

      /*
       * Note that the syslog(3) uses the first argument of openlog(3)
       * STATICALLY. So DON'T pass any heap addresses to openlog(3). I
       * never knew that until this moment, BTW.
       */
      s_set_arg(arg);
      openlog(s_log_arg, 0, LOG_USER);

      ret = true;
    }
  }

  if (ret == true) {
    s_log_dst = dst;
    s_do_multi_process = multi_process;
    s_do_date = emit_date;
    s_dbg_level = debug_level;
  } else {
    s_reset_arg();
  }

  return ret;
}