<commit_before>/*
 * $Id$
 *
 * Log tailer for Varnish
 */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "compat/vis.h"

#include "vsb.h"

#include "libvarnish.h"
#include "shmlog.h"
#include "varnishapi.h"

static int	bflag, cflag;

/* Ordering-----------------------------------------------------------*/

static struct vsb	*ob[65536];
static unsigned char	invcl[65536];

static void
clean_order(void)
{
	unsigned u;

printf("Clean\n");
	for (u = 0; u < 65536; u++) {
		if (ob[u] == NULL)
			continue;
		vsb_finish(ob[u]);
		if (vsb_len(ob[u]))
			printf("%s\n", vsb_data(ob[u]));
		vsb_clear(ob[u]);
	}
}

static int 
h_order(void *priv, unsigned tag, unsigned fd, unsigned len, unsigned spec, const char *ptr)
{

	(void)priv;

	if (!(spec & (VSL_S_CLIENT|VSL_S_BACKEND))) {
		if (!bflag && !cflag)
			VSL_H_Print(stdout, tag, fd, len, spec, ptr);
		return (0);
	}
	if (ob[fd] == NULL) {
		ob[fd] = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
		assert(ob[fd] != NULL);
	}
	switch (tag) {
	case SLT_VCL_call:
		invcl[fd] = 1;
		vsb_printf(ob[fd], "%5d %-12s %c %.*s",
		    fd, VSL_tags[tag],
		    ((spec & VSL_S_CLIENT) ? 'c' : \
		    (spec & VSL_S_BACKEND) ? 'b' : ' '),
		    len, ptr);
		return (0);
	case SLT_VCL_trace:
	case SLT_VCL_return:
		if (invcl[fd]) {
			vsb_cat(ob[fd], " ");
			vsb_bcat(ob[fd], ptr, len);
			return (0);
		}
		break;
	default:
		if (invcl[fd])
			vsb_cat(ob[fd], "\n");
		invcl[fd] = 0;
		break;
	}
	if (invcl[fd]) {
		vsb_cat(ob[fd], "\n");
		invcl[fd] = 0;
	}
	vsb_printf(ob[fd], "%5d %-12s %c %.*s\n",
	    fd, VSL_tags[tag],
	    ((spec & VSL_S_CLIENT) ? 'c' : (spec & VSL_S_BACKEND) ? 'b' : ' '),
	    len, ptr);
	switch (tag) {
	case SLT_ReqEnd:
	case SLT_BackendClose:
	case SLT_BackendReuse:
	case SLT_StatSess:
		vsb_finish(ob[fd]);
		if (vsb_len(ob[fd]) > 1)
			printf("%s\n", vsb_data(ob[fd]));
		vsb_clear(ob[fd]);
		break;
	default:
		break;
	}
	return (0);
}

static void
do_order(struct VSL_data *vd)
{
	int i;

	if (!bflag) {
		VSL_Select(vd, SLT_SessionOpen);
		VSL_Select(vd, SLT_SessionClose);<commit_msg>Turn the "invcl" array into a flag array so we can put more stuff there.<commit_after>/*
 * $Id$
 *
 * Log tailer for Varnish
 */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "compat/vis.h"

#include "vsb.h"

#include "libvarnish.h"
#include "shmlog.h"
#include "varnishapi.h"

static int	bflag, cflag;

/* Ordering-----------------------------------------------------------*/

static struct vsb	*ob[65536];
static unsigned char	flg[65536];
#define F_INVCL		(1 << 0)

static void
clean_order(void)
{
	unsigned u;

printf("Clean\n");
	for (u = 0; u < 65536; u++) {
		if (ob[u] == NULL)
			continue;
		vsb_finish(ob[u]);
		if (vsb_len(ob[u]))
			printf("%s\n", vsb_data(ob[u]));
		vsb_clear(ob[u]);
	}
}

static int 
h_order(void *priv, unsigned tag, unsigned fd, unsigned len, unsigned spec, const char *ptr)
{

	(void)priv;

	if (!(spec & (VSL_S_CLIENT|VSL_S_BACKEND))) {
		if (!bflag && !cflag)
			VSL_H_Print(stdout, tag, fd, len, spec, ptr);
		return (0);
	}
	if (ob[fd] == NULL) {
		ob[fd] = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
		assert(ob[fd] != NULL);
	}
	switch (tag) {
	case SLT_VCL_call:
		flg[fd] |= F_INVCL;
		vsb_printf(ob[fd], "%5d %-12s %c %.*s",
		    fd, VSL_tags[tag],
		    ((spec & VSL_S_CLIENT) ? 'c' : \
		    (spec & VSL_S_BACKEND) ? 'b' : ' '),
		    len, ptr);
		return (0);
	case SLT_VCL_trace:
	case SLT_VCL_return:
		if (flg[fd] & F_INVCL) {
			vsb_cat(ob[fd], " ");
			vsb_bcat(ob[fd], ptr, len);
			return (0);
		}
		break;
	default:
		break;
	}
	if (flg[fd] & F_INVCL) {
		vsb_cat(ob[fd], "\n");
		flg[fd] &= ~F_INVCL;
	}
	vsb_printf(ob[fd], "%5d %-12s %c %.*s\n",
	    fd, VSL_tags[tag],
	    ((spec & VSL_S_CLIENT) ? 'c' : (spec & VSL_S_BACKEND) ? 'b' : ' '),
	    len, ptr);
	switch (tag) {
	case SLT_ReqEnd:
	case SLT_BackendClose:
	case SLT_BackendReuse:
	case SLT_StatSess:
		vsb_finish(ob[fd]);
		if (vsb_len(ob[fd]) > 1)
			printf("%s\n", vsb_data(ob[fd]));
		vsb_clear(ob[fd]);
		break;
	default:
		break;
	}
	return (0);
}

static void
do_order(struct VSL_data *vd)
{
	int i;

	if (!bflag) {
		VSL_Select(vd, SLT_SessionOpen);
		VSL_Select(vd, SLT_SessionClose);
{'fraction_non_alphanumeric': 0.21631701631701633, 'fraction_numerical': 0.01421911421911422, 'mean_word_length': 3.2275862068965515, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, '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': '5419684', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1966, 'n_words': 415}