]> Sergey Matveev's repositories - nnn.git/commitdiff
An undocumented debug mode
authorArun Prakash Jana <engineerarun@gmail.com>
Mon, 19 Jun 2017 16:51:08 +0000 (22:21 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Mon, 19 Jun 2017 19:20:19 +0000 (00:50 +0530)
1. The debug file is located at /tmp/nnn_debug.
2. Debug mode is completely disabled by default. The debug binary `nnndbg` can
be built by running `make debug`.

Makefile
nnn.c

index 2394a4772975a147d72fdc45db995447963ce4eb..dd562ad9099bd3b553461eff71682a63d349e8ae 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 VERSION = 1.1
 
-PREFIX = /usr/local
+PREFIX ?= /usr/local
 MANPREFIX = $(PREFIX)/share/man
 
 CFLAGS += -O2 -Wall -Wextra -Wno-unused-parameter
@@ -27,9 +27,12 @@ $(LOCALCONFIG): config.def.h
 $(SRC): $(LOCALCONFIG)
 
 $(BIN): $(SRC)
-       $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
+       $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) $(LDLIBS)
        strip $@
 
+debug: $(SRC)
+       $(CC) -DDEBUGMODE -g $(CFLAGS) -o nnndbg $^ $(LDFLAGS) $(LDLIBS)
+
 install: all
        mkdir -p $(DESTDIR)$(PREFIX)/bin
        cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin
diff --git a/nnn.c b/nnn.c
index 2ab39d6f00d09fb7830eaac65d94852f2f74c677..89c0f0f09090b42f1e04c16208d8337354b52191 100644 (file)
--- a/nnn.c
+++ b/nnn.c
@@ -39,7 +39,9 @@
 #endif
 #include <ftw.h>
 
-#ifdef DEBUG
+#ifdef DEBUGMODE
+static int DEBUG_FD;
+
 static int
 xprintf(int fd, const char *fmt, ...)
 {
@@ -55,7 +57,29 @@ xprintf(int fd, const char *fmt, ...)
        return r;
 }
 
-#define DEBUG_FD 8
+static int enabledbg()
+{
+       FILE *fp = fopen("/tmp/nnn_debug", "w");
+
+       if (!fp) {
+               fprintf(stderr, "Cannot open debug file\n");
+               return -1;
+       }
+
+       DEBUG_FD = fileno(fp);
+       if (DEBUG_FD == -1) {
+               fprintf(stderr, "Cannot open debug file descriptor\n");
+               return -1;
+       }
+
+       return 0;
+}
+
+static void disabledbg()
+{
+       close(DEBUG_FD);
+}
+
 #define DPRINTF_D(x) xprintf(DEBUG_FD, #x "=%d\n", x)
 #define DPRINTF_U(x) xprintf(DEBUG_FD, #x "=%u\n", x)
 #define DPRINTF_S(x) xprintf(DEBUG_FD, #x "=%s\n", x)
@@ -82,7 +106,7 @@ xprintf(int fd, const char *fmt, ...)
 
 struct assoc {
        char *regex; /* Regex to match on filename */
-       char *mime; /* File type */
+       char *mime;  /* File type */
 };
 
 /* Supported actions */
@@ -2398,11 +2422,18 @@ main(int argc, char *argv[])
                exit(1);
        }
 
+#ifdef DEBUGMODE
+       enabledbg();
+#endif
+
        /* Set locale */
        setlocale(LC_ALL, "");
 
        initcurses();
        browse(ipath, ifilter);
        exitcurses();
+#ifdef DEBUGMODE
+       disabledbg();
+#endif
        exit(0);
 }