File: log-files.c

package info (click to toggle)
trinity 1.9%2Bgit20200331.4d2343bd18c7b-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,080 kB
  • sloc: ansic: 32,746; sh: 536; makefile: 164
file content (114 lines) | stat: -rw-r--r-- 2,080 bytes parent folder | download | duplicates (4)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "logfile.h"
#include "params.h"	// logging, quiet_level
#include "pids.h"
#include "shm.h"

FILE *mainlogfile;

static FILE *open_logfile(const char *logfilename)
{
	FILE *file;
	char *fullpath, *p;
	int len = strlen(logfilename) + 2;

	if (logging_args)
		len += strlen(logging_args);

	p = fullpath = zmalloc(len);
	if (logging_args)
		p += snprintf(fullpath, strlen(logging_args) + 2, "%s/", logging_args);
	p += snprintf(p, strlen(logfilename) + 1, "%s", logfilename);

	unlink(fullpath);

	file = fopen(fullpath, "w");
	if (!file)
		outputerr("## couldn't open logfile %s\n", fullpath);

	free(fullpath);
	return file;
}

void open_main_logfile(void)
{
	mainlogfile = open_logfile("trinity.log");
	if (!mainlogfile)
		exit(EXIT_FAILURE);
}

void open_child_logfile(struct childdata *child)
{
	char *logfilename;

	logfilename = zmalloc(64);
	sprintf(logfilename, "trinity-child%u.log", child->num);

	child->logfile = open_logfile(logfilename);
	if (!child->logfile) {
		shm->exit_reason = EXIT_LOGFILE_OPEN_ERROR;
		exit(EXIT_FAILURE);
	}

	free(logfilename);

	child->logdirty = FALSE;
}

void close_logfile(FILE **filehandle)
{
	if (*filehandle == NULL)
		return;

	fclose(*filehandle);
	*filehandle = NULL;
}

FILE *find_logfile_handle(void)
{
	struct childdata *child;
	pid_t pid;

	pid = getpid();
	if (pid == mainpid)
		return mainlogfile;

	child = this_child();
	if (child != NULL)
		return child->logfile;

	return NULL;
}

/*
 * Flush any pending log writes to disk.
 * Only to be called from child context.
 */
void synclogs(void)
{
	struct childdata *child;
	int fd;

	child = this_child();
	if (child->logdirty == FALSE)
		return;

	fflush(child->logfile);
	fd = fileno(child->logfile);
	if (fd != -1)
		(void) fsync(fd);

	child->logdirty = FALSE;

	/* If we're flushing the child log, may as well flush
	 * any other logs while we're writing to disk.
	 */
	(void)fflush(mainlogfile);
	fsync(fileno(mainlogfile));
}