capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / log.c
blobe956853d6281c1d9499af21c441848c692923d68
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 /***
6 log.c - the iptraf logging facility
8 ***/
10 #include "iptraf-ng-compat.h"
12 #include "attrs.h"
13 #include "deskman.h"
14 #include "dirs.h"
15 #include "log.h"
17 #include "tui/input.h"
18 #include "tui/msgboxes.h"
19 #include "tui/winops.h"
21 #define TARGET_LOGNAME_MAX 160
23 int rotate_flag;
24 char target_logname[TARGET_LOGNAME_MAX];
25 char current_logfile[TARGET_LOGNAME_MAX];
28 * Generates a log file based on a template for a particular instance of
29 * a facility. Used by the IP Traffic Monitor and LAN Station Monitor.
32 char *gen_instance_logname(char *template, int instance_num)
34 static char filename[80];
36 snprintf(filename, 80, "%s-%d.log", template, instance_num);
37 return filename;
40 void input_logfile(char *target, int *logging)
42 WINDOW *dlgwin;
43 PANEL *dlgpanel;
44 struct FIELDLIST fieldlist;
45 int aborted;
47 dlgwin = newwin(11, 60, (LINES - 11) / 2, (COLS - 60) / 2);
48 dlgpanel = new_panel(dlgwin);
50 wattrset(dlgwin, DLGBOXATTR);
51 tx_colorwin(dlgwin);
52 tx_box(dlgwin, ACS_VLINE, ACS_HLINE);
53 mvwprintw(dlgwin, 0, 1, " Logging Enabled ");
54 wattrset(dlgwin, DLGTEXTATTR);
55 mvwprintw(dlgwin, 2, 2,
56 "Enter the name of the file to which to write the log.");
57 mvwprintw(dlgwin, 4, 2,
58 "If you don't specify a path, the log file will");
59 mvwprintw(dlgwin, 5, 2, "be placed in %s.", LOGDIR);
60 wmove(dlgwin, 9, 2);
61 stdkeyhelp(dlgwin);
62 wprintw(dlgwin, " (turns logging off)");
64 tx_initfields(&fieldlist, 1, 50, (LINES - 1) / 2 + 2,
65 (COLS - 50) / 2 - 3, DLGTEXTATTR, FIELDATTR);
66 tx_addfield(&fieldlist, 48, 0, 0, target);
67 tx_fillfields(&fieldlist, &aborted);
69 if (!aborted) {
70 if (strchr(fieldlist.list->buf, '/') == NULL)
71 snprintf(target, 48, "%s/%s", LOGDIR,
72 fieldlist.list->buf);
73 else
74 strncpy(target, fieldlist.list->buf, 48);
77 *logging = !aborted;
79 tx_destroyfields(&fieldlist);
80 del_panel(dlgpanel);
81 delwin(dlgwin);
82 update_panels();
83 doupdate();
86 void opentlog(FILE ** fd, char *logfilename)
88 *fd = fopen(logfilename, "a");
90 if (*fd == NULL)
91 tui_error(ANYKEY_MSG, "Unable to open log file");
93 rotate_flag = 0;
94 strcpy(target_logname, "");
97 void genatime(time_t now, char *atime)
99 memset(atime, 0, TIME_TARGET_MAX);
100 strncpy(atime, ctime(&now), 26);
101 atime[strlen(atime) - 1] = '\0';
104 void writelog(int logging, FILE * fd, char *msg)
106 char atime[TIME_TARGET_MAX];
108 if (logging) {
109 genatime(time(NULL), atime);
110 fprintf(fd, "%s; %s\n", atime, msg);
113 fflush(fd);
116 void write_daemon_err(char *msg, va_list vararg)
118 char atime[TIME_TARGET_MAX];
119 FILE *fd;
121 genatime(time(NULL), atime);
122 fd = fopen(DAEMONLOG, "a");
123 fprintf(fd, "%s iptraf[%u]: ", atime, getpid());
124 vfprintf(fd, msg, vararg);
125 fprintf(fd, "\n");
126 fclose(fd);
129 void rotate_logfile(FILE ** fd, char *name)
131 fclose(*fd);
132 *fd = fopen(name, "a");
133 rotate_flag = 0;
137 void announce_rotate_prepare(FILE * fd)
139 writelog(1, fd,
140 "***** USR1 signal received, preparing to reopen log file *****");
143 void announce_rotate_complete(FILE * fd)
145 writelog(1, fd, "***** Logfile reopened *****");
148 void check_rotate_flag(FILE ** logfile)
150 if (rotate_flag == 1) {
151 announce_rotate_prepare(*logfile);
152 rotate_logfile(logfile, target_logname);
153 announce_rotate_complete(*logfile);
154 rotate_flag = 0;