summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2014-06-12 12:46:00 +0200
committerYorhel <git@yorhel.nl>2014-06-12 12:46:00 +0200
commit7fddf1b392c681a57c3c248e4920f6f4fafe144a (patch)
treecc2109774efb92f587c23bcc493e8b55bb329af9
parent8e3a7fa24ece9c75e9a1644d84b4b8068307a0be (diff)
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.
-rw-r--r--src/main.c25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/main.c b/src/main.c
index 2c7a9e2..bd48173 100644
--- a/src/main.c
+++ b/src/main.c
@@ -236,15 +236,15 @@ char *ncdc_version() {
}
-static gboolean stderr_redir = FALSE;
+static FILE *stderrlog;
-// redirect all non-fatal errors to stderr (NOT stdout!)
+// redirect all non-fatal errors to the log
static void log_redirect(const gchar *dom, GLogLevelFlags level, const gchar *msg, gpointer dat) {
- if(!(level & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG)) || (stderr_redir && var_log_debug)) {
+ if(!(level & (G_LOG_LEVEL_INFO|G_LOG_LEVEL_DEBUG)) || (stderrlog != stderr && var_log_debug)) {
char *ts = localtime_fmt("[%F %H:%M:%S %Z]");
- fprintf(stderr, "%s *%s* %s\n", ts, loglevel_to_str(level), msg);
+ fprintf(stderrlog, "%s *%s* %s\n", ts, loglevel_to_str(level), msg);
g_free(ts);
- fflush(stderr);
+ fflush(stderrlog);
}
}
@@ -252,10 +252,10 @@ static void log_redirect(const gchar *dom, GLogLevelFlags level, const gchar *ms
// clean-up our ncurses window before throwing a fatal error
static void log_fatal(const gchar *dom, GLogLevelFlags level, const gchar *msg, gpointer dat) {
endwin();
- // print to both stderr (log file) and stdout
- if(stderr_redir) {
- fprintf(stderr, "\n\n*%s* %s\n", loglevel_to_str(level), msg);
- fflush(stderr);
+ // print to both log file and stdout
+ if(stderrlog != stderr) {
+ fprintf(stderrlog, "\n\n*%s* %s\n", loglevel_to_str(level), msg);
+ fflush(stderrlog);
}
printf("\n\n*%s* %s\n", loglevel_to_str(level), msg);
}
@@ -398,6 +398,8 @@ static void init_crypt() {
int main(int argc, char **argv) {
setlocale(LC_ALL, "");
+ // Early logging goes to stderr
+ stderrlog = stderr;
// parse commandline options
GOptionContext *optx = g_option_context_new("- NCurses Direct Connect");
@@ -432,14 +434,13 @@ int main(int argc, char **argv) {
db_init();
vars_init();
- // redirect stderr to a log file
+ // open log file
char *errlog = g_build_filename(db_dir, "stderr.log", NULL);
- if(!freopen(errlog, "w", stderr)) {
+ if(!(stderrlog = fopen(errlog, "w"))) {
fprintf(stderr, "ERROR: Couldn't open %s for writing: %s\n", errlog, strerror(errno));
exit(1);
}
g_free(errlog);
- stderr_redir = TRUE;
// Init more stuff
hub_init_global();