1
/*
2
 * Copyright (c) 2002  Dustin Sallings <dustin@spy.net>
3
 */
4
5
#include <iostream>
6
7
#ifdef USE_ASSERT
8
# include <assert.h>
9
#else
10
# undef assert
11
# define assert(a)
12
#endif
13
14
#include "logfiles.h"
15
16
#define STDOUT_BUF_SIZE 1024*1024
17
18
namespace logmerge {
19
	static void initLogfiles(log_queue&, int, char **);
20
	static void outputLogfiles(log_queue&);
21
	static void initLogfile(log_queue&, const char*);
22
}
23
24
static void logmerge::initLogfile(log_queue& queue, const char *filename) {
25
	struct logfile *lf=createLogfile(filename);
26
	if(lf!=NULL) {
27
		queue.push(lf);
28
	} else {
29
		std::cerr << "Error opening logfile ``" << filename
30
			<< "''" << std::endl;
31
	}
32
}
33
34
/* Initialize all of the logfiles */
35
static void logmerge::initLogfiles(log_queue& queue, int argc, char **argv)
36
{
37
	if(argc>1) {
38
		for(int i=1; i<argc; i++) {
39
			initLogfile(queue, argv[i]);
40
		}
41
	} else {
42
		char buf[8192];
43
		std::cerr << "No logfiles given, accepting list from stdin"
44
			<< std::endl;
45
		while(fgets((char*)&buf, sizeof(buf)-1, stdin)) {
46
			buf[strlen(buf)-1]=0x00;
47
			initLogfile(queue, buf);
48
		}
49
	}
50
}
51
52
static void logmerge::outputLogfiles(log_queue& queue)
53
{
54
	int entries=0;
55
	struct logfile *lf=NULL;
56
57
	while(!queue.empty()) {
58
		entries++;
59
60
		lf=queue.top();
61
		assert(lf!=NULL);
62
		if(! lf->isOpen) {
63
			openLogfile(lf);
64
		}
65
66
		lf->outputLine(lf);
67
		skipRecord(queue);
68
	}
69
70
	std::cerr << "Read " << entries << " entries" << std::endl;
71
}
72
73
#ifdef USE_ASSERT
74
static void testMonthParsing() {
75
	char *months[] = {
76
		"Jan/", "Feb/", "Mar/", "Apr/", "May/", "Jun/",
77
		"Jul/", "Aug/", "Sep/", "Oct/", "Nov/", "Dec/"
78
	};
79
	for(int i=0; i<12; i++) {
80
		for(int j=0; j<10; j++) {
81
			int rv=parseMonth(months[i]);
82
			if(i != rv) {
83
				std::cerr << "Expected " << i << " for "
84
					<< months[i] << " got " << rv << std::endl;
85
				abort();
86
			}
87
		}
88
	}
89
	for(int j=0; j<10; j++) {
90
		for(int i=0; i<12; i++) {
91
			int rv=parseMonth(months[i]);
92
			if(i != rv) {
93
				std::cerr << "Expected " << i << " for "
94
					<< months[i] << " got " << rv << std::endl;
95
				abort();
96
			}
97
		}
98
	}
99
}
100
#else
101
static void testMonthParsing() {
102
}
103
#endif
104
105
/**
106
 * The main.
107
 */
108
int main(int argc, char **argv)
109
{
110
	log_queue queue;
111
112
	testMonthParsing();
113
114
	setvbuf(stdout, NULL, _IOFBF, STDOUT_BUF_SIZE);
115
116
	logmerge::initLogfiles(queue, argc, argv);
117
	logmerge::outputLogfiles(queue);
118
119
	return(0);
120
}