<commit_before>#include <errno.h>
#include <string.h>

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <pthread.h>

#include <syslog.h>

#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <inttypes.h>

#include "transfused.h"

void log_timestamp(int fd) {
  char timestamp[26];
  int msec;
  struct tm* tm_info;
  struct timeval tv;

  gettimeofday(&tv, NULL);

  msec = lrint(tv.tv_usec/1000.0);
  if (msec >= 1000) {
    msec -= 1000;
    tv.tv_sec++;
  }

  tm_info = localtime(&tv.tv_sec);

  strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  dprintf(fd, "%s.%03d ", timestamp, msec);
}

void die
(int exit_code, parameters * params, const char * parg, const char * fmt, ...);

void vlog_sock_locked(int fd, const char * fmt, va_list args) {
  uint16_t log_err_type = 1;
  int rc, len;
  va_list targs;
  char * fill;

  va_copy(targs, args);
  len = vsnprintf(NULL, 0, fmt, targs);
  if (len < 0) die(1, NULL, NULL, "Couldn't log due to vsnprintf failure");
  va_end(targs);

  rc = len + 4 + 2; // 4 for length itself and 2 for message type
  write_exactly("vlog_sock_locked", fd, (uint32_t *) &rc, sizeof(uint32_t));
  write_exactly("vlog_sock_locked", fd, &log_err_type, sizeof(uint16_t));

  va_copy(targs, args);
  rc = vdprintf(fd, fmt, targs);
  if (rc < 0) die(1, NULL, "Couldn't send log message with vdprintf", "");
  va_end(targs);

  if (rc < len) { // we didn't write the whole message :-(
    rc = len - rc;
    fill = (char *) calloc(rc, 1);
    if (fill == NULL) die(1, NULL, "vlog_sock_locked fill", "");
    write_exactly("vlog_sock_locked fill", fd, fill, rc);
  }
}

void log_sock_locked(int fd, const char * fmt, ...) {
  va_list args;

  va_start(args, fmt);

  vlog_sock_locked(fd, fmt, args);

  va_end(args);
}

void die
(int exit_code, parameters * params, const char * parg, const char * fmt, ...)
{
  va_list argp, targs;
  int in_errno = errno;
  int fd = 0;

  if (params != NULL) {
    fd = params->ctl_sock;
    lock("die ctl_lock", &params->ctl_lock);
  }

  va_start(argp, fmt);
  va_copy(targs, argp);
  vsyslog(LOG_CRIT, fmt, targs);
  va_end(targs);

  if (fd != 0) vlog_sock_locked(fd, fmt, argp);
  va_end(argp);

  if (parg != NULL) {
    if (*parg != 0) {
      syslog(LOG_CRIT, "%s: %s", parg, strerror(in_errno));
      if (fd != 0) log_sock_locked(fd, "%s: %s", parg, strerror(in_errno));
    } else {
      syslog(LOG_CRIT, "%s", strerror(in_errno));
      if (fd != 0) log_sock_locked(fd, "%s", strerror(in_errno));
    }
  }

  exit(exit_code);
  // Nobody else should die before we terminate everything
  unlock("die ctl_lock", &params->ctl_lock);
}

void vlog_locked(parameters * params, const char * fmt, va_list args) {
  int rc;
  int fd = params->ctl_sock;
  va_list targs;

  if (fd != 0) vlog_sock_locked(fd, fmt, args);
  else {
    va_copy(targs, args);
    vsyslog(LOG_INFO, fmt, targs);
    va_end(targs);

    fd = params->logfile_fd;<commit_msg>transfused: make sure to flush the logging socket when we die<commit_after>#include <errno.h>
#include <string.h>

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#include <syslog.h>

#include <sys/time.h>
#include <time.h>
#include <math.h>
#include <inttypes.h>

#include "transfused.h"

void log_timestamp(int fd) {
  char timestamp[26];
  int msec;
  struct tm* tm_info;
  struct timeval tv;

  gettimeofday(&tv, NULL);

  msec = lrint(tv.tv_usec/1000.0);
  if (msec >= 1000) {
    msec -= 1000;
    tv.tv_sec++;
  }

  tm_info = localtime(&tv.tv_sec);

  strftime(timestamp, 26, "%Y-%m-%d %H:%M:%S", tm_info);
  dprintf(fd, "%s.%03d ", timestamp, msec);
}

void die
(int exit_code, parameters * params, const char * parg, const char * fmt, ...);

void vlog_sock_locked(int fd, const char * fmt, va_list args) {
  uint16_t log_err_type = 1;
  int rc, len;
  va_list targs;
  char * fill;

  va_copy(targs, args);
  len = vsnprintf(NULL, 0, fmt, targs);
  if (len < 0) die(1, NULL, NULL, "Couldn't log due to vsnprintf failure");
  va_end(targs);

  rc = len + 4 + 2; // 4 for length itself and 2 for message type
  write_exactly("vlog_sock_locked", fd, (uint32_t *) &rc, sizeof(uint32_t));
  write_exactly("vlog_sock_locked", fd, &log_err_type, sizeof(uint16_t));

  va_copy(targs, args);
  rc = vdprintf(fd, fmt, targs);
  if (rc < 0) die(1, NULL, "Couldn't send log message with vdprintf", "");
  va_end(targs);

  if (rc < len) { // we didn't write the whole message :-(
    rc = len - rc;
    fill = (char *) calloc(rc, 1);
    if (fill == NULL) die(1, NULL, "vlog_sock_locked fill", "");
    write_exactly("vlog_sock_locked fill", fd, fill, rc);
  }
}

void log_sock_locked(int fd, const char * fmt, ...) {
  va_list args;

  va_start(args, fmt);

  vlog_sock_locked(fd, fmt, args);

  va_end(args);
}

void die
(int exit_code, parameters * params, const char * parg, const char * fmt, ...)
{
  va_list argp, targs;
  int in_errno = errno;
  int fd = 0;

  if (params != NULL) {
    fd = params->ctl_sock;
    lock("die ctl_lock", &params->ctl_lock);
  }

  va_start(argp, fmt);
  va_copy(targs, argp);
  vsyslog(LOG_CRIT, fmt, targs);
  va_end(targs);

  if (fd != 0) vlog_sock_locked(fd, fmt, argp);
  va_end(argp);

  if (parg != NULL) {
    if (*parg != 0) {
      syslog(LOG_CRIT, "%s: %s", parg, strerror(in_errno));
      if (fd != 0) log_sock_locked(fd, "%s: %s", parg, strerror(in_errno));
    } else {
      syslog(LOG_CRIT, "%s", strerror(in_errno));
      if (fd != 0) log_sock_locked(fd, "%s", strerror(in_errno));
    }
  }

  if (fd != 0) close(fd); // flush
  exit(exit_code);
  // Nobody else should die before we terminate everything
  unlock("die ctl_lock", &params->ctl_lock);
}

void vlog_locked(parameters * params, const char * fmt, va_list args) {
  int rc;
  int fd = params->ctl_sock;
  va_list targs;

  if (fd != 0) vlog_sock_locked(fd, fmt, args);
  else {
    va_copy(targs, args);
    vsyslog(LOG_INFO, fmt, targs);
    va_end(targs);

    fd = params->logfile_fd;
{'fraction_non_alphanumeric': 0.1653635002495425, 'fraction_numerical': 0.015138911994676426, 'mean_word_length': 3.606896551724138, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7648726', 'n_tokens_mistral': 2541, 'n_tokens_neox': 2422, 'n_words': 626}