<commit_before>#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#include "erlusb-log.h"

static FILE *logfile = NULL;

void
log_init()
{
  logfile = fopen("erlusb.log", "a");
  setvbuf(logfile, NULL, _IONBF, 0);
}

void
log_close()
{
  fprintf(logfile, "erlusb.c finished.\n");
  fclose(logfile);
}

void
log_printf(const char *format, ...)
{
  va_list ap, aq;
  va_start(ap, format);
  va_copy(aq, ap);
  vfprintf(logfile, format, aq);
  va_end(aq);
  va_end(ap);
}

static
void
dump_data(FILE *fp, const char *data, const size_t len)
{
   size_t i;
   for (i=0; i<len; i++) {
     switch (i&15) {
     case 0:
       fprintf(fp, "%08x ", i);
       break;
     case 8:
       fprintf(fp, " ");
       break;
     default:
       break;
     }
     fprintf(fp, " %02x", 0xff&data[i]);
     if (((i&15) == 15) || ((i+1) == len)) {
       fprintf(fp, "\n");
     }
   }
}


void
log_data(const char *data, const size_t len)
{
  dump_data(logfile, data, len);
}<commit_msg>Improve hexdump with ASCII characters<commit_after>#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>

#include "erlusb-log.h"

static FILE *logfile = NULL;

void
log_init()
{
  logfile = fopen("erlusb.log", "a");
  setvbuf(logfile, NULL, _IONBF, 0);
}

void
log_close()
{
  fprintf(logfile, "erlusb.c finished.\n");
  fclose(logfile);
}

void
log_printf(const char *format, ...)
{
  va_list ap, aq;
  va_start(ap, format);
  va_copy(aq, ap);
  vfprintf(logfile, format, aq);
  va_end(aq);
  va_end(ap);
}

static
void
dump_data(FILE *fp, const char *data, const size_t len)
{
  size_t row;
  for (row=0; 16*row<len; row++) {
    size_t col;
    fprintf(fp, "%08x ", 16*row);
    for (col=0; col<16; col++) {
      size_t ofs = 16*row+col;
      if (col == 8) {
	fprintf(fp, " ");
      }
      if (ofs < len) {
	fprintf(fp, " %02x", 0xff&data[ofs]);
      } else {
	fprintf(fp, "   ");
      }
    }
    fprintf(fp, "  |");
    for (col=0; col<16; col++) {
      size_t ofs = 16*row+col;
      if (ofs < len) {
	char c = 0xff&data[ofs];
	fprintf(fp, "%c", ((32<=c)&&(c<127))?c:'.');
      } else {
	break;
      }
    }
    fprintf(fp, "|\n");
  }
}


void
log_data(const char *data, const size_t len)
{
  dump_data(logfile, data, len);
}
{'fraction_non_alphanumeric': 0.19201435621354868, 'fraction_numerical': 0.01973979362943024, 'mean_word_length': 2.9539007092198584, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 9, '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': '4354954', 'n_tokens_mistral': 1001, 'n_tokens_neox': 963, 'n_words': 211}