8 #include "params.h" // logging, monochrome, quiet_level
15 void open_logfiles(void)
20 logfilename
= malloc(64);
21 sprintf(logfilename
, "trinity.log");
23 mainlogfile
= fopen(logfilename
, "a");
25 printf("## couldn't open logfile %s\n", logfilename
);
30 sprintf(logfilename
, "trinity-child%d.log", i
);
32 shm
->logfiles
[i
] = fopen(logfilename
, "a");
33 if (!shm
->logfiles
[i
]) {
34 printf("## couldn't open logfile %s\n", logfilename
);
41 void close_logfiles(void)
46 if (shm
->logfiles
[i
] != NULL
)
47 fclose(shm
->logfiles
[i
]);
50 static FILE * find_logfile_handle(void)
63 if (pid
== watchdog_pid
)
66 i
= find_pid_slot(pid
);
67 if (i
!= PIDSLOT_NOT_FOUND
)
68 return shm
->logfiles
[i
];
70 /* try one more time. FIXME: This is awful. */
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
);
78 printf("## Logfiles for pids: ");
80 printf("%p ", shm
->logfiles
[j
]);
86 unsigned int highest_logfile(void)
94 file
= shm
->logfiles
[shm
->max_children
- 1];
105 if (logging
== FALSE
)
108 for_each_pidslot(i
) {
109 ret
= fflush(shm
->logfiles
[i
]);
111 printf("## logfile flushing failed! %s\n", strerror(errno
));
115 fd
= fileno(shm
->logfiles
[i
]);
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.
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
, ...)
140 unsigned int len
, i
, j
;
141 char outputbuf
[1024];
144 if (logging
== FALSE
&& level
>= quiet_level
)
148 n
= vsnprintf(outputbuf
, sizeof(outputbuf
), fmt
, args
);
152 printf("## Something went wrong in output() [%d]\n", n
);
156 if (quiet_level
> level
) {
157 printf("%s", outputbuf
);
158 (void)fflush(stdout
);
161 if (logging
== FALSE
)
164 handle
= find_logfile_handle();
166 printf("## child logfile handle was null logging to main!\n");
167 (void)fflush(stdout
);
169 shm
->logfiles
[j
] = mainlogfile
;
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
);
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
191 i
+= 3; // ANSI_RESET
193 monobuf
[j
] = outputbuf
[i
];
199 fprintf(handle
, "%s", monobuf
);
200 (void)fflush(handle
);