rename project to visftp
[oftp] / src / util.c
index 7be1859..ceea684 100644 (file)
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include "util.h"
+#include "tui.h"
 
 void *malloc_nf_impl(size_t sz, const char *file, int line)
 {
@@ -52,3 +53,62 @@ int match_prefix(const char *str, const char *prefix)
        }
        return *prefix ? 0 : 1;
 }
+
+static FILE *logfile;
+
+static void closelog(void)
+{
+       fclose(logfile);
+}
+
+static void logmsg(const char *tag, const char *fmt, va_list ap)
+{
+       if(!logfile) {
+               if(!(logfile = fopen("visftp.log", "w"))) {
+                       return;
+               }
+               setvbuf(logfile, 0, _IOLBF, 0);
+               atexit(closelog);
+       }
+       fprintf(logfile, "%s: ", tag);
+       vfprintf(logfile, fmt, ap);
+}
+
+void errmsg(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       tui_vmsgbox(TUI_ERROR, "error", fmt, ap);
+       va_end(ap);
+
+       va_start(ap, fmt);
+       logmsg("error", fmt, ap);
+       va_end(ap);
+}
+
+void warnmsg(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       tui_status(TUI_WARN, fmt, ap);
+       va_end(ap);
+
+       va_start(ap, fmt);
+       logmsg("warning", fmt, ap);
+       va_end(ap);
+}
+
+void infomsg(const char *fmt, ...)
+{
+       va_list ap;
+
+       va_start(ap, fmt);
+       tui_status(TUI_INFO, fmt, ap);
+       va_end(ap);
+
+       va_start(ap, fmt);
+       logmsg("info", fmt, ap);
+       va_end(ap);
+}