<commit_before>#include "genericLoggerRuntime.h"

#ifdef C_VA_COPY
/* Used when passing a va_list on the stack to another function taking a va_list */
#define REAL_AP ap2
#else
#define REAL_AP ap
#endif

#include "genericLogger.h"

#define DATEBUILDER_MAX_SIZE sizeof(char) * (1024+1)

struct genericLogger {
  genericLoggerCallback_t  logCallbackp;
  void                    *userDatavp;
  genericLoggerLevel_t     genericLoggerLeveli;
};

static const char *_dateBuilder_internalErrors  = "Internal error when building date";
static const char *_messageBuilder_internalErrors = "Internal error when building message";

static C_INLINE void  _genericLogger_defaultCallbackp(void *userDatavp, genericLoggerLevel_t logLeveli, const char *msgs);
static C_INLINE char *_dateBuilders(const char *fmts);
static C_INLINE char *_messageBuilders(const char *fmts, ...);
static C_INLINE char *_messageBuilder_aps(const char *fmts, va_list ap);

/**********************/
/* genericLogger_newp */
/**********************/
genericLogger_t *genericLogger_newp(genericLoggerCallback_t logCallbackp, void *userDatavp, genericLoggerLevel_t genericLoggerLeveli) {
  genericLogger_t *genericLoggerp = malloc(sizeof(genericLogger_t));

  if (genericLoggerp == NULL) {
    /* Well, shall we log about this - a priori no: the caller wanted to set up a particular */
    /* logging system, and not use our default */
    return NULL;
  }

  genericLoggerp->logCallbackp        = logCallbackp;
  genericLoggerp->userDatavp          = userDatavp;
  genericLoggerp->genericLoggerLeveli = genericLoggerLeveli;

  return genericLoggerp;
}

/************************/
/* genericLogger_clonep */
/************************/
genericLogger_t *genericLogger_clonep(genericLogger_t *genericLoggerp) {
  if (genericLoggerp == NULL) {
    return NULL;
  }

  return genericLogger_newp(genericLoggerp->logCallbackp, genericLoggerp->userDatavp, genericLoggerp->genericLoggerLeveli);
}

/*******************************/
/* genericLogger_logLevel_seti */
/*******************************/
genericLoggerLevel_t genericLogger_logLevel_seti(genericLogger_t *genericLoggerp, genericLoggerLevel_t logLeveli) {
  genericLoggerp->genericLoggerLeveli = logLeveli;
  return genericLoggerp->genericLoggerLeveli;
}

/*******************************/
/* genericLogger_logLevel_geti */
/*******************************/
genericLoggerLevel_t genericLogger_logLevel_geti(genericLogger_t *genericLoggerp) {
  return genericLoggerp->genericLoggerLeveli;
}

/*************************************/
/* genericLogger_defaultLogCallbackp */
/*************************************/
genericLoggerCallback_t genericLogger_defaultLogCallbackp(void) {
  return &_genericLogger_defaultCallbackp;
}

/***********************/
/* genericLogger_freev */
/***********************/
void genericLogger_freev(genericLogger_t **genericLoggerpp) {

  if (genericLoggerpp != NULL) {
    if (*genericLoggerpp != NULL) {
      free(*genericLoggerpp);
      *genericLoggerpp = NULL;
    }
  }
}

/*************************************/
/* _genericLogger_defaultCallbackp */
/*************************************/
static C_INLINE void _genericLogger_defaultCallbackp(void *userDatavp, genericLoggerLevel_t logLeveli, const char *msgs) {
  /* We are NOT going to do a general log4c mechanism (this can come later), using genericLogger in fact */
  /* I.e. we are fixing the default output to be: DD/MM/YYYY hh::mm::ss PREFIX MESSAGE */
  const char   *prefixs =
    (logLeveli == GENERICLOGGER_LOGLEVEL_TRACE    ) ? "TRACE"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_DEBUG    ) ? "DEBUG"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_INFO     ) ? "INFO"      :
    (logLeveli == GENERICLOGGER_LOGLEVEL_NOTICE   ) ? "NOTICE"    :
    (logLeveli == GENERICLOGGER_LOGLEVEL_WARNING  ) ? "WARN"      :
    (logLeveli == GENERICLOGGER_LOGLEVEL_ERROR    ) ? "ERROR"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_CRITICAL ) ? "CRITICAL"  :
    (logLeveli == GENERICLOGGER_LOGLEVEL_ALERT    ) ? "ALERT"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_EMERGENCY) ? "EMERGENCY" :
    "UNKOWN";
  char   *dates         = _dateBuilders("%d/%m/%Y %H:%M:%S");
  char   *localMsgs     = _messageBuilders("%s %9s %s\n", dates, prefixs, (msgs != NULL) ? msgs : "");
  int     filenoStderri = C_FILENO(stderr);
  size_t  bytesWritenl  = 0;
  char   *s             = localMsgs;
  size_t  countl        = strlen(s);

  while (bytesWritenl < countl) {
    bytesWritenl += C_WRITE(filenoStderri, s+bytesWritenl, countl-bytesWritenl);
  }

  if (dates != _dateBuilder_internalErrors) {
    free(dates);
  }
  if (localMsgs != _messageBuilder_internalErrors) {
    free(localMsgs);
  }
}

/**********************/
/* genericLogger_logv */
/**********************/
void genericLogger_logv(genericLogger_t *genericLoggerp, genericLoggerLevel_t genericLoggerLeveli, const char *fmts, ...) {
  va_list ap;

  va_start(ap, fmts);
  genericLogger_logapv(genericLoggerp, genericLoggerLeveli, fmts, ap);
  va_end(ap);
}

/************************/
/* genericLogger_logapv */
/************************/
void genericLogger_logapv(genericLogger_t *genericLoggerp, genericLoggerLevel_t genericLoggerLeveli, const char *fmts, va_list ap) {
#ifdef C_VA_COPY<commit_msg>Export stuff<commit_after>#include "genericLoggerRuntime.h"

#ifdef C_VA_COPY
/* Used when passing a va_list on the stack to another function taking a va_list */
#define REAL_AP ap2
#else
#define REAL_AP ap
#endif

#include "genericLogger.h"

#define DATEBUILDER_MAX_SIZE sizeof(char) * (1024+1)

struct genericLogger {
  genericLoggerCallback_t  logCallbackp;
  void                    *userDatavp;
  genericLoggerLevel_t     genericLoggerLeveli;
};

static const char *_dateBuilder_internalErrors  = "Internal error when building date";
static const char *_messageBuilder_internalErrors = "Internal error when building message";

static C_INLINE void  _genericLogger_defaultCallbackp(void *userDatavp, genericLoggerLevel_t logLeveli, const char *msgs);
static C_INLINE char *_dateBuilders(const char *fmts);
static C_INLINE char *_messageBuilders(const char *fmts, ...);
static C_INLINE char *_messageBuilder_aps(const char *fmts, va_list ap);

/**********************/
/* genericLogger_newp */
/**********************/
GENERICLOGGER_EXPORT genericLogger_t *genericLogger_newp(genericLoggerCallback_t logCallbackp, void *userDatavp, genericLoggerLevel_t genericLoggerLeveli) {
  genericLogger_t *genericLoggerp = malloc(sizeof(genericLogger_t));

  if (genericLoggerp == NULL) {
    /* Well, shall we log about this - a priori no: the caller wanted to set up a particular */
    /* logging system, and not use our default */
    return NULL;
  }

  genericLoggerp->logCallbackp        = logCallbackp;
  genericLoggerp->userDatavp          = userDatavp;
  genericLoggerp->genericLoggerLeveli = genericLoggerLeveli;

  return genericLoggerp;
}

/************************/
/* genericLogger_clonep */
/************************/
GENERICLOGGER_EXPORT genericLogger_t *genericLogger_clonep(genericLogger_t *genericLoggerp) {
  if (genericLoggerp == NULL) {
    return NULL;
  }

  return genericLogger_newp(genericLoggerp->logCallbackp, genericLoggerp->userDatavp, genericLoggerp->genericLoggerLeveli);
}

/*******************************/
/* genericLogger_logLevel_seti */
/*******************************/
GENERICLOGGER_EXPORT genericLoggerLevel_t genericLogger_logLevel_seti(genericLogger_t *genericLoggerp, genericLoggerLevel_t logLeveli) {
  genericLoggerp->genericLoggerLeveli = logLeveli;
  return genericLoggerp->genericLoggerLeveli;
}

/*******************************/
/* genericLogger_logLevel_geti */
/*******************************/
GENERICLOGGER_EXPORT genericLoggerLevel_t genericLogger_logLevel_geti(genericLogger_t *genericLoggerp) {
  return genericLoggerp->genericLoggerLeveli;
}

/*************************************/
/* genericLogger_defaultLogCallbackp */
/*************************************/
GENERICLOGGER_EXPORT genericLoggerCallback_t genericLogger_defaultLogCallbackp(void) {
  return &_genericLogger_defaultCallbackp;
}

/***********************/
/* genericLogger_freev */
/***********************/
GENERICLOGGER_EXPORT void genericLogger_freev(genericLogger_t **genericLoggerpp) {

  if (genericLoggerpp != NULL) {
    if (*genericLoggerpp != NULL) {
      free(*genericLoggerpp);
      *genericLoggerpp = NULL;
    }
  }
}

/*************************************/
/* _genericLogger_defaultCallbackp */
/*************************************/
static C_INLINE void _genericLogger_defaultCallbackp(void *userDatavp, genericLoggerLevel_t logLeveli, const char *msgs) {
  /* We are NOT going to do a general log4c mechanism (this can come later), using genericLogger in fact */
  /* I.e. we are fixing the default output to be: DD/MM/YYYY hh::mm::ss PREFIX MESSAGE */
  const char   *prefixs =
    (logLeveli == GENERICLOGGER_LOGLEVEL_TRACE    ) ? "TRACE"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_DEBUG    ) ? "DEBUG"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_INFO     ) ? "INFO"      :
    (logLeveli == GENERICLOGGER_LOGLEVEL_NOTICE   ) ? "NOTICE"    :
    (logLeveli == GENERICLOGGER_LOGLEVEL_WARNING  ) ? "WARN"      :
    (logLeveli == GENERICLOGGER_LOGLEVEL_ERROR    ) ? "ERROR"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_CRITICAL ) ? "CRITICAL"  :
    (logLeveli == GENERICLOGGER_LOGLEVEL_ALERT    ) ? "ALERT"     :
    (logLeveli == GENERICLOGGER_LOGLEVEL_EMERGENCY) ? "EMERGENCY" :
    "UNKOWN";
  char   *dates         = _dateBuilders("%d/%m/%Y %H:%M:%S");
  char   *localMsgs     = _messageBuilders("%s %9s %s\n", dates, prefixs, (msgs != NULL) ? msgs : "");
  int     filenoStderri = C_FILENO(stderr);
  size_t  bytesWritenl  = 0;
  char   *s             = localMsgs;
  size_t  countl        = strlen(s);

  while (bytesWritenl < countl) {
    bytesWritenl += C_WRITE(filenoStderri, s+bytesWritenl, countl-bytesWritenl);
  }

  if (dates != _dateBuilder_internalErrors) {
    free(dates);
  }
  if (localMsgs != _messageBuilder_internalErrors) {
    free(localMsgs);
  }
}

/**********************/
/* genericLogger_logv */
/**********************/
GENERICLOGGER_EXPORT void genericLogger_logv(genericLogger_t *genericLoggerp, genericLoggerLevel_t genericLoggerLeveli, const char *fmts, ...) {
  va_list ap;

  va_start(ap, fmts);
  genericLogger_logapv(genericLoggerp, genericLoggerLeveli, fmts, ap);
  va_end(ap);
}

/************************/
/* genericLogger_logapv */
/************************/
GENERICLOGGER_EXPORT void genericLogger_logapv(genericLogger_t *genericLoggerp, genericLoggerLevel_t genericLoggerLeveli, const char *fmts, va_list ap) {
#ifdef C_VA_COPY
{'fraction_non_alphanumeric': 0.18600730542287158, 'fraction_numerical': 0.0016858668165214948, 'mean_word_length': 4.992143658810326, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '4968963', 'n_tokens_mistral': 3352, 'n_tokens_neox': 3281, 'n_words': 664}