forked from zhugegy/LogClusterC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.c
581 lines (517 loc) · 15.6 KB
/
output.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
* Copyright (C) 2016 Zhuge Chen, Risto Vaarandi and Mauno Pihelgas
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* File: output.c
*
* Content: Functions related to outputting messages (and logs).
*
* Created on November 29, 2016, 10:57 PM
*/
#include "common_header.h"
#include "output.h"
#include <string.h> /* for strcmp(), strcpy(), etc. */
#include "utility.h"
static void print_clusters_default_config(struct Parameters *pParam);
static void print_clusters_constant_config(struct Parameters *pParam);
static void print_clusters_if_join_cluster_default_0(
struct Parameters *pParam);
static void print_clusters_default_0(struct Parameters *pParam);
static void print_clusters_if_join_cluster_constant_1(
struct Parameters *pParam);
static void print_clusters_constant_1(struct Parameters *pParam);
static void print_cluster(struct Cluster* pCluster);
static void print_cluster_with_token(struct ClusterWithToken *pClusterWithToken,
struct Parameters *pParam);
/* Log message operator. It refines a message into timestamped format, and
forwards it to user terminal. It also forwards the message to Syslog. */
void log_msg(char *message, int logLv, struct Parameters* pParam)
{
time_t t;
char *timestamp;
t = time(0);
timestamp = ctime(&t);
timestamp[strlen(timestamp) - 1] = 0;
fprintf(stderr, "%s: %s\n", timestamp, message);
if (pParam->bSyslogFlag == 1)
{
syslog(logLv, "%s", message);
}
}
void print_usage()
{
fprintf(stderr, "\n");
fprintf(stderr, VERSIONINFO);
fprintf(stderr, "\n");
fprintf(stderr, USAGEINFO);
}
void print_cluster_to_string(struct Cluster *pCluster,
struct Parameters *pParam)
{
int i;
//To avoid warning(returing local varialbe in stack), changed local variable
//to outside variable(pParam->clusterDescription).
//char clusterDescription[MAXLOGMSGLEN];
char strTmp[MAXLOGMSGLEN];
*pParam->clusterDescription = 0;
for (i = 1; i <= pCluster->constants; i++)
{
if (pCluster->fullWildcard[i * 2 + 1])
{
sprintf(strTmp, "*{%d,%d} ", pCluster->fullWildcard[i * 2],
pCluster->fullWildcard[i * 2 + 1]);
strcat(pParam->clusterDescription, strTmp);
}
sprintf(strTmp, "%s ", pCluster->ppWord[i]->pKey);
strcat(pParam->clusterDescription, strTmp);
}
if (pCluster->fullWildcard[1])
{
sprintf(strTmp, "*{%d,%d}", pCluster->fullWildcard[0],
pCluster->fullWildcard[1]);
strcat(pParam->clusterDescription, strTmp);
}
//return clusterDescription;
}
void step_3_print_clusters(struct Parameters *pParam)
{
printf("\n");
switch (pParam->outputMode)
{
case 0:
//Default printing configuration. Clusters are sorted by support.
print_clusters_default_config(pParam);
break;
case 1:
/* Alternative printing configuration. Clusters are sorted by the number
of constants. */
print_clusters_constant_config(pParam);
break;
default:
break;
}
printf("\n");
}
//clusters are arranged according to their support value
static void print_clusters_default_config(struct Parameters *pParam)
{
char logStr[MAXLOGMSGLEN];
char digit[MAXDIGITBIT];
if (pParam->wordWeightThreshold)
{
print_clusters_if_join_cluster_default_0(pParam);
str_format_int_grouped(digit, pParam->clusterNum -
pParam->joinedClusterInputNum +
pParam->joinedClusterOutputNum);
}
else
{
print_clusters_default_0(pParam);
str_format_int_grouped(digit, pParam->clusterNum);
}
sprintf(logStr, "Total number of clusters: %s", digit);
log_msg(logStr, LOG_INFO, pParam);
}
//clusters are arranged according to the number of constants (frequent words)
static void print_clusters_constant_config(struct Parameters *pParam)
{
char logStr[MAXLOGMSGLEN];
char digit[MAXDIGITBIT];
if (pParam->wordWeightThreshold)
{
print_clusters_if_join_cluster_constant_1(pParam);
str_format_int_grouped(digit, pParam->clusterNum -
pParam->joinedClusterInputNum +
pParam->joinedClusterOutputNum);
}
else
{
print_clusters_constant_1(pParam);
str_format_int_grouped(digit, pParam->clusterNum);
}
sprintf(logStr, "Total number of clusters: %s", digit);
log_msg(logStr, LOG_INFO, pParam);
}
/* When making changes to this function, don't forget to also change its
brother function print_clusters_default_0().
For the sake of computing performance, sorry for this inconvenience. It will
be fixed with better solution in the following updates. */
static void print_clusters_if_join_cluster_default_0(
struct Parameters *pParam)
{
int i, j, k;
struct Cluster *pCluster;
struct ClusterWithToken *pClusterWithToken, *ptr;
struct Elem **ppSortedArray;
wordnumber_t toBeSortedNum;
toBeSortedNum = (pParam->clusterNum - pParam->joinedClusterInputNum) +
pParam->joinedClusterOutputNum;
ppSortedArray = (struct Elem **) malloc(sizeof(struct Elem *) *
toBeSortedNum);
if (!ppSortedArray)
{
log_msg(MALLOC_ERR_6020, LOG_ERR, pParam);
exit(1);
}
j = 0;
for (i = 1; i <= pParam->biggestConstants; i++)
{
pCluster = pParam->pClusterFamily[i];
while (pCluster)
{
if (pCluster->bIsJoined == 0)
{
ppSortedArray[j] = pCluster->pElem;
j++;
}
pCluster = pCluster->pNext;
}
pClusterWithToken = pParam->pClusterWithTokenFamily[i];
while (pClusterWithToken)
{
ppSortedArray[j] = pClusterWithToken->pElem;
j++;
pClusterWithToken = pClusterWithToken->pNext;
}
}
sort_elements(ppSortedArray, toBeSortedNum, pParam);
for (k = 0; k < toBeSortedNum; k++)
{
/* For clusters in pClusterFamily[], only print those who were not
marked as bIsJoined. Those who were joined, will be printed later, by
accessing pClusterWithTokenFamily[]. */
if (ppSortedArray[k]->pCluster->bIsJoined == 1)
{
ptr = (struct ClusterWithToken *) ppSortedArray[k]->pCluster;
print_cluster_with_token(ptr, pParam);
}
else
{
print_cluster(ppSortedArray[k]->pCluster);
}
}
free((void *) ppSortedArray);
}
//Outdated function
//void (O)_print_clusters_if_join_cluster_default_0(struct Parameters *pParam)
//{
// int i, j, k, c, u, b;
// struct Cluster *pCluster;
// struct ClusterWithToken *pClusterWithToken;
// struct Elem **ppSortedArray;
//
// ppSortedArray = (struct Elem **) malloc(sizeof(struct Elem *) *
// pParam->clusterNum);
// if (!ppSortedArray)
// {
// log_msg(MALLOC_ERR_6019, LOG_ERR, pParam);
// exit(1);
// }
//
// j = 0;
// for (i = 1; i <= pParam->biggestConstants; i++)
// {
// pCluster = pParam->pClusterFamily[i];
// while (pCluster)
// {
// ppSortedArray[j] = pCluster->pElem;
// j++;
// pCluster = pCluster->pNext;
// }
// }
//
// sort_elements(ppSortedArray, pParam->clusterNum, pParam);
//
// if (pParam->clusterNum - pParam->joinedClusterInputNum)
// {
// printf(">>>>>>The %lu clusters that are not joined:\n\n",
// pParam->clusterNum - pParam->joinedClusterInputNum);
// }
//
// for (k = 0; k < pParam->clusterNum; k++)
// {
// /* For clusters in pClusterFamily[], only print those who were not
// marked as bIsJoined. Those who were joined, will be printed later, by
// accessing pClusterWithTokenFamily[]. */
// if (ppSortedArray[k]->pCluster->bIsJoined == 0)
// {
// print_cluster(ppSortedArray[k]->pCluster);
// }
//
// }
//
// free((void *) ppSortedArray);
//
// ppSortedArray = (struct Elem **) malloc(sizeof(struct Elem *) *
// pParam->joinedClusterOutputNum);
// if (!ppSortedArray)
// {
// log_msg(MALLOC_ERR_6019, LOG_ERR, pParam);
// exit(1);
// }
//
// u = 0;
// for (c = 1; c <= pParam->biggestConstants; c++)
// {
// pClusterWithToken = pParam->pClusterWithTokenFamily[c];
// while (pClusterWithToken)
// {
// ppSortedArray[u] = pClusterWithToken->pElem;
// u++;
// pClusterWithToken = pClusterWithToken->pNext;
// }
// }
//
// sort_elements(ppSortedArray, pParam->joinedClusterOutputNum, pParam);
//
// if (pParam->joinedClusterOutputNum)
// {
// printf(">>>>>>The %lu joined clusters:\n\n",
// pParam->joinedClusterOutputNum);
// }
//
// for (b = 0; b < pParam->joinedClusterOutputNum; b++)
// {
// print_cluster_with_token((struct ClusterWithToken *)
// ppSortedArray[b]->pCluster, pParam);
// }
//
// free((void *) ppSortedArray);
//}
/* When making changes to this function, don't forget to also change its
brother function print_clusters_if_join_cluster_default_0().
For the sake of computing performance, sorry for this inconvenience. It will
be fixed with better solution in the following updates. */
static void print_clusters_default_0(struct Parameters *pParam)
{
int i, j, k;
struct Cluster *pCluster;
struct Elem **ppSortedArray;
ppSortedArray = (struct Elem **) malloc(sizeof(struct Elem *) *
pParam->clusterNum);
if (!ppSortedArray)
{
log_msg(MALLOC_ERR_6018, LOG_ERR, pParam);
exit(1);
}
j = 0;
for (i = 1; i <= pParam->biggestConstants; i++)
{
pCluster = pParam->pClusterFamily[i];
while (pCluster)
{
ppSortedArray[j] = pCluster->pElem;
j++;
pCluster = pCluster->pNext;
}
}
sort_elements(ppSortedArray, pParam->clusterNum, pParam);
for (k = 0; k < pParam->clusterNum; k++)
{
print_cluster(ppSortedArray[k]->pCluster);
}
free((void *) ppSortedArray);
}
/* When making changes to this function, don't forget to also change its
brother function print_clusters_constant_1().
For the sake of computing performance, sorry for this inconvenience. It will
be fixed with better solution in the following updates. */
static void print_clusters_if_join_cluster_constant_1(
struct Parameters *pParam)
{
int i;
struct Cluster *pCluster;
struct ClusterWithToken *pClusterWithToken;
for (i = 1; i <= pParam->biggestConstants; i++)
{
/* For clusters in pClusterFamily[], only print those who were not
marked as bIsJoined. Those who were joined, will be printed later, by
accessing pClusterWithTokenFamily[]. */
pCluster = pParam->pClusterFamily[i];
while (pCluster)
{
if (pCluster->bIsJoined == 0)
{
print_cluster(pCluster);
}
pCluster = pCluster->pNext;
}
pClusterWithToken = pParam->pClusterWithTokenFamily[i];
while (pClusterWithToken)
{
print_cluster_with_token(pClusterWithToken, pParam);
pClusterWithToken = pClusterWithToken->pNext;
}
}
}
//outdated function
//void (O)_print_clusters_if_join_cluster_constant_1(struct Parameters *pParam)
//{
// int i, j;
// struct Cluster *pCluster;
// struct ClusterWithToken *pClusterWithToken;
//
// if (pParam->clusterNum - pParam->joinedClusterInputNum)
// {
// printf(">>>>>>The %lu clusters that are not joined:\n\n",
// pParam->clusterNum - pParam->joinedClusterInputNum);
// }
//
// for (i = 1; i <= pParam->biggestConstants; i++)
// {
// /* For clusters in pClusterFamily[], only print those who were not
// marked as bIsJoined. Those who were joined, will be printed later, by
// accessing pClusterWithTokenFamily[]. */
// pCluster = pParam->pClusterFamily[i];
// while (pCluster)
// {
// if (pCluster->bIsJoined == 0)
// {
// print_cluster(pCluster);
//
// }
// pCluster = pCluster->pNext;
// }
// }
//
// if (pParam->joinedClusterOutputNum)
// {
// printf(">>>>>>The %lu joined clusters:\n\n",
// pParam->joinedClusterOutputNum);
// }
//
// for (j = 1; j <= pParam->biggestConstants; j++)
// {
// pClusterWithToken = pParam->pClusterWithTokenFamily[j];
// while (pClusterWithToken)
// {
// print_cluster_with_token(pClusterWithToken, pParam);
// pClusterWithToken = pClusterWithToken->pNext;
// }
// }
//}
/* When making changes to this function, don't forget to also change its
brother function print_clusters_if_join_cluster_constant_1().
For the sake of computing performance, sorry for this inconvenience. It will
be fixed with better solution in the following updates. */
static void print_clusters_constant_1(struct Parameters *pParam)
{
int i;
struct Cluster *pCluster;
for (i = 1; i <= pParam->biggestConstants; i++)
{
pCluster = pParam->pClusterFamily[i];
while (pCluster)
{
print_cluster(pCluster);
pCluster = pCluster->pNext;
}
}
}
static void print_cluster(struct Cluster* pCluster)
{
char digit[MAXDIGITBIT];
int i;
for (i = 1; i <= pCluster->constants; i++)
{
if (pCluster->fullWildcard[i * 2 + 1])
{
printf("*{%d,%d} ", pCluster->fullWildcard[i * 2],
pCluster->fullWildcard[i * 2 + 1]);
}
printf("%s ", pCluster->ppWord[i]->pKey);
}
if (pCluster->fullWildcard[1])
{
printf("*{%d,%d}", pCluster->fullWildcard[0],
pCluster->fullWildcard[1]);
}
printf("\n");
str_format_int_grouped(digit, pCluster->count);
printf("Support : %s\n\n", digit);
}
static void print_cluster_with_token(struct ClusterWithToken *pClusterWithToken,
struct Parameters *pParam)
{
char digit[MAXDIGITBIT];
struct Token *pToken;
int i;
for (i = 1; i <= pClusterWithToken->constants; i++)
{
if (pClusterWithToken->fullWildcard[i * 2 + 1])
{
printf("*{%d,%d} ", pClusterWithToken->fullWildcard[i * 2],
pClusterWithToken->fullWildcard[i * 2 + 1]);
}
if (pClusterWithToken->ppToken[i] != 0)
{
if (pParam->bDetailedTokenFlag == 0)
{
/* This solution will not mark a token, if it is the only word.
*/
if (pClusterWithToken->ppToken[i]->pNext != 0)
{
printf("(");
pToken = pClusterWithToken->ppToken[i];
while (pToken)
{
printf("%s", pToken->pWord->pKey);
if (pToken->pNext)
{
printf("|");
}
pToken = pToken->pNext;
}
printf(") ");
}
else
{
printf("%s ", pClusterWithToken->ppToken[i]->pWord->pKey);
}
}
else
{
/* This solution marks a token with (), no matter how many words
it contains. */
printf("(");
pToken = pClusterWithToken->ppToken[i];
while (pToken)
{
printf("%s", pToken->pWord->pKey);
if (pToken->pNext)
{
printf("|");
}
pToken = pToken->pNext;
}
printf(") ");
}
}
else
{
printf("%s ", pClusterWithToken->ppWord[i]->pKey);
}
}
if (pClusterWithToken->fullWildcard[1])
{
printf("*{%d,%d}", pClusterWithToken->fullWildcard[0],
pClusterWithToken->fullWildcard[1]);
}
printf("\n");
str_format_int_grouped(digit, pClusterWithToken->count);
printf("Support : %s\n\n", digit);
}