Skip to content

Commit 7fddf1b

Browse files
author
Yorhel
committed
Dont freopen stderr, log to separate file handle
There shouldn't be any other code that writes to stderr, so no real need to reopen it. This fixes the bracketed paste mode that I broke in an earlier commit by having the codes sent to stderr. Since stderr was mapped to the log file, the codes would never reach the terminal and the bracketed paste mode was simply never enabled. This fix also makes me run into the vim issue described at reply 7 of http://dev.yorhel.nl/ncdc/bug/66 - I'll have to look into that again. For some reason the display of non-ASCII characters in the line editing ui is broken, too. I'll look at that in a bit.
1 parent 8e3a7fa commit 7fddf1b

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/main.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -236,26 +236,26 @@ char *ncdc_version() {
236236
}
237237

238238

239-
static gboolean stderr_redir = FALSE;
239+
static FILE *stderrlog;
240240

241-
// redirect all non-fatal errors to stderr (NOT stdout!)
241+
// redirect all non-fatal errors to the log
242242
static void log_redirect(const gchar *dom, GLogLevelFlags level, const gchar *msg, gpointer dat) {
243-
if(!(level & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG)) || (stderr_redir && var_log_debug)) {
243+
if(!(level & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG)) || (stderrlog != stderr && var_log_debug)) {
244244
char *ts = localtime_fmt("[%F %H:%M:%S %Z]");
245-
fprintf(stderr, "%s *%s* %s\n", ts, loglevel_to_str(level), msg);
245+
fprintf(stderrlog, "%s *%s* %s\n", ts, loglevel_to_str(level), msg);
246246
g_free(ts);
247-
fflush(stderr);
247+
fflush(stderrlog);
248248
}
249249
}
250250

251251

252252
// clean-up our ncurses window before throwing a fatal error
253253
static void log_fatal(const gchar *dom, GLogLevelFlags level, const gchar *msg, gpointer dat) {
254254
endwin();
255-
// print to both stderr (log file) and stdout
256-
if(stderr_redir) {
257-
fprintf(stderr, "\n\n*%s* %s\n", loglevel_to_str(level), msg);
258-
fflush(stderr);
255+
// print to both log file and stdout
256+
if(stderrlog != stderr) {
257+
fprintf(stderrlog, "\n\n*%s* %s\n", loglevel_to_str(level), msg);
258+
fflush(stderrlog);
259259
}
260260
printf("\n\n*%s* %s\n", loglevel_to_str(level), msg);
261261
}
@@ -398,6 +398,8 @@ static void init_crypt() {
398398

399399
int main(int argc, char **argv) {
400400
setlocale(LC_ALL, "");
401+
// Early logging goes to stderr
402+
stderrlog = stderr;
401403

402404
// parse commandline options
403405
GOptionContext *optx = g_option_context_new("- NCurses Direct Connect");
@@ -432,14 +434,13 @@ int main(int argc, char **argv) {
432434
db_init();
433435
vars_init();
434436

435-
// redirect stderr to a log file
437+
// open log file
436438
char *errlog = g_build_filename(db_dir, "stderr.log", NULL);
437-
if(!freopen(errlog, "w", stderr)) {
439+
if(!(stderrlog = fopen(errlog, "w"))) {
438440
fprintf(stderr, "ERROR: Couldn't open %s for writing: %s\n", errlog, strerror(errno));
439441
exit(1);
440442
}
441443
g_free(errlog);
442-
stderr_redir = TRUE;
443444

444445
// Init more stuff
445446
hub_init_global();

0 commit comments

Comments
 (0)