factor out the stuck syscall info to own function
[trinity.git] / log.c
blobc441261e35efbc352817fa671c55e920ad74c0ce
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdarg.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include "params.h" // logging, monochrome, quiet_level
9 #include "shm.h"
10 #include "pids.h"
11 #include "log.h"
13 FILE *mainlogfile;
15 void open_logfiles(void)
17 unsigned int i;
18 char *logfilename;
20 logfilename = malloc(64);
21 sprintf(logfilename, "trinity.log");
22 unlink(logfilename);
23 mainlogfile = fopen(logfilename, "a");
24 if (!mainlogfile) {
25 printf("## couldn't open logfile %s\n", logfilename);
26 exit(EXIT_FAILURE);
29 for_each_pidslot(i) {
30 sprintf(logfilename, "trinity-child%d.log", i);
31 unlink(logfilename);
32 shm->logfiles[i] = fopen(logfilename, "a");
33 if (!shm->logfiles[i]) {
34 printf("## couldn't open logfile %s\n", logfilename);
35 exit(EXIT_FAILURE);
38 free(logfilename);
41 void close_logfiles(void)
43 unsigned int i;
45 for_each_pidslot(i)
46 if (shm->logfiles[i] != NULL)
47 fclose(shm->logfiles[i]);
50 static FILE * find_logfile_handle(void)
52 pid_t pid;
53 int i;
54 unsigned int j;
56 pid = getpid();
57 if (pid == initpid)
58 return mainlogfile;
60 if (pid == mainpid)
61 return mainlogfile;
63 if (pid == watchdog_pid)
64 return mainlogfile;
66 i = find_pid_slot(pid);
67 if (i != PIDSLOT_NOT_FOUND)
68 return shm->logfiles[i];
69 else {
70 /* try one more time. FIXME: This is awful. */
71 sleep(1);
72 i = find_pid_slot(pid);
73 if (i != PIDSLOT_NOT_FOUND)
74 return shm->logfiles[i];
76 printf("[%d] ## Couldn't find logfile for pid %d\n", getpid(), pid);
77 dump_pid_slots();
78 printf("## Logfiles for pids: ");
79 for_each_pidslot(j)
80 printf("%p ", shm->logfiles[j]);
81 printf("\n");
83 return NULL;
86 unsigned int highest_logfile(void)
88 FILE *file;
89 int ret;
91 if (logging == FALSE)
92 return 0;
94 file = shm->logfiles[shm->max_children - 1];
95 ret = fileno(file);
97 return ret;
100 void synclogs(void)
102 unsigned int i;
103 int fd, ret;
105 if (logging == FALSE)
106 return;
108 for_each_pidslot(i) {
109 ret = fflush(shm->logfiles[i]);
110 if (ret == EOF) {
111 printf("## logfile flushing failed! %s\n", strerror(errno));
112 continue;
115 fd = fileno(shm->logfiles[i]);
116 if (fd != -1) {
117 ret = fsync(fd);
118 if (ret != 0)
119 printf("## fsyncing logfile %d failed. %s\n", i, strerror(errno));
123 (void)fflush(mainlogfile);
124 fsync(fileno(mainlogfile));
128 * level defines whether it gets displayed to the screen with printf.
129 * (it always logs).
130 * 0 = everything, even all the registers
131 * 1 = Watchdog prints syscall count
132 * 2 = Just the reseed values
135 void output(unsigned char level, const char *fmt, ...)
137 va_list args;
138 int n;
139 FILE *handle;
140 unsigned int len, i, j;
141 char outputbuf[1024];
142 char monobuf[1024];
144 if (logging == FALSE && level >= quiet_level)
145 return;
147 va_start(args, fmt);
148 n = vsnprintf(outputbuf, sizeof(outputbuf), fmt, args);
149 va_end(args);
151 if (n < 0) {
152 printf("## Something went wrong in output() [%d]\n", n);
153 exit(EXIT_FAILURE);
156 if (quiet_level > level) {
157 printf("%s", outputbuf);
158 (void)fflush(stdout);
161 if (logging == FALSE)
162 return;
164 handle = find_logfile_handle();
165 if (!handle) {
166 printf("## child logfile handle was null logging to main!\n");
167 (void)fflush(stdout);
168 for_each_pidslot(j)
169 shm->logfiles[j] = mainlogfile;
170 sleep(5);
171 return;
174 /* If we've specified monochrome, we can just dump the buffer
175 * into the logfile as is. Otherwise, we need to strip out
176 * any ANSI codes that may be present.
178 if (monochrome == TRUE) {
179 fprintf(handle, "%s", outputbuf);
180 (void)fflush(handle);
181 return;
184 /* copy buffer, sans ANSI codes */
185 len = strlen(outputbuf);
186 for (i = 0, j = 0; i < len; i++) {
187 if (outputbuf[i] == '\e')
188 if (outputbuf[i + 2] == '1')
189 i += 6; // ANSI_COLOUR
190 else
191 i += 3; // ANSI_RESET
192 else {
193 monobuf[j] = outputbuf[i];
194 j++;
197 monobuf[j] = '\0';
199 fprintf(handle, "%s", monobuf);
200 (void)fflush(handle);