1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
6 log.c - the iptraf logging facility
10 #include "iptraf-ng-compat.h"
17 #include "tui/input.h"
18 #include "tui/msgboxes.h"
19 #include "tui/winops.h"
21 #define TARGET_LOGNAME_MAX 160
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
);
40 void input_logfile(char *target
, int *logging
)
44 struct FIELDLIST fieldlist
;
47 dlgwin
= newwin(11, 60, (LINES
- 11) / 2, (COLS
- 60) / 2);
48 dlgpanel
= new_panel(dlgwin
);
50 wattrset(dlgwin
, DLGBOXATTR
);
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
);
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
);
70 if (strchr(fieldlist
.list
->buf
, '/') == NULL
)
71 snprintf(target
, 48, "%s/%s", LOGDIR
,
74 strncpy(target
, fieldlist
.list
->buf
, 48);
79 tx_destroyfields(&fieldlist
);
86 void opentlog(FILE ** fd
, char *logfilename
)
88 *fd
= fopen(logfilename
, "a");
91 tui_error(ANYKEY_MSG
, "Unable to open log file");
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
];
109 genatime(time(NULL
), atime
);
110 fprintf(fd
, "%s; %s\n", atime
, msg
);
116 void write_daemon_err(char *msg
, va_list vararg
)
118 char atime
[TIME_TARGET_MAX
];
121 genatime(time(NULL
), atime
);
122 fd
= fopen(DAEMONLOG
, "a");
123 fprintf(fd
, "%s iptraf[%u]: ", atime
, getpid());
124 vfprintf(fd
, msg
, vararg
);
129 void rotate_logfile(FILE ** fd
, char *name
)
132 *fd
= fopen(name
, "a");
137 void announce_rotate_prepare(FILE * 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
);