[PATCH] Allow logging to specific file with -F option

Kel Modderman kel at otaku42.de
Sun Dec 16 20:49:40 EST 2007


Hi,

(Re-sending this one, as it was not commented on in last patch frenzy.)

Logging to file was recently added, but the file path and name template
is hardcoded in wpa_debug_open_file(). This change adds an extra -F
command line option which allows specification of a specific output
filename to write debug output to.

wpa_debug_open_file() also prints an error message when the output file
failed to be opened for writing, and wpa_debug_close_file() does not
attempt to fclose the output file if it failed to be opened.

Signed-off-by: Kel Modderman <kel at otaku42.de>
---
diff --git a/src/utils/wpa_debug.c b/src/utils/wpa_debug.c
index b2f8c8b..7d3fff8 100644
--- a/src/utils/wpa_debug.c
+++ b/src/utils/wpa_debug.c
@@ -227,36 +227,42 @@ void wpa_hexdump_ascii_key(int level, const char *title, 
const u8 *buf,
 }
 
 
-int wpa_debug_open_file(void)
+int wpa_debug_open_file(const char *file)
 {
 #ifdef CONFIG_DEBUG_FILE
 	static int count = 0;
 	char fname[64];
 	if (!wpa_debug_use_file)
 		return 0;
+	if (file != NULL && out_file == NULL) {
+		out_file = fopen(file, "a");
+	} else {
 #ifdef _WIN32
-	os_snprintf(fname, sizeof(fname), "\\Temp\\wpa_supplicant-log-%d.txt",
-		    count++);
+		os_snprintf(fname, sizeof(fname), "\\Temp\\wpa_supplicant-log-%d.txt",
+			    count++);
 #else /* _WIN32 */
-	os_snprintf(fname, sizeof(fname), "/tmp/wpa_supplicant-log-%d.txt",
-		    count++);
+		os_snprintf(fname, sizeof(fname), "/tmp/wpa_supplicant-log-%d.txt",
+			    count++);
 #endif /* _WIN32 */
-	out_file = fopen(fname, "w");
+		out_file = fopen(fname, "w");
+	}
+	if (out_file == NULL) {
+		wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open output file, "
+			   "using standard output");
+		return -1;
+	}
 #ifndef _WIN32
-	if (out_file)
-		setvbuf(out_file, NULL, _IOLBF, 0);
+	setvbuf(out_file, NULL, _IOLBF, 0);
 #endif /* _WIN32 */
-	return out_file == NULL ? -1 : 0;
-#else /* CONFIG_DEBUG_FILE */
-	return 0;
 #endif /* CONFIG_DEBUG_FILE */
+	return 0;
 }
 
 
 void wpa_debug_close_file(void)
 {
 #ifdef CONFIG_DEBUG_FILE
-	if (!wpa_debug_use_file)
+	if (!wpa_debug_use_file || out_file == NULL)
 		return;
 	fclose(out_file);
 	out_file = NULL;
diff --git a/src/utils/wpa_debug.h b/src/utils/wpa_debug.h
index 9890945..8372cce 100644
--- a/src/utils/wpa_debug.h
+++ b/src/utils/wpa_debug.h
@@ -37,7 +37,7 @@ enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, 
MSG_ERROR };
 
 #else /* CONFIG_NO_STDOUT_DEBUG */
 
-int wpa_debug_open_file(void);
+int wpa_debug_open_file(const char *file);
 void wpa_debug_close_file(void);
 
 /**
diff --git a/wpa_supplicant/doc/docbook/wpa_supplicant.sgml 
b/wpa_supplicant/doc/docbook/wpa_supplicant.sgml
index 296e660..1c7234a 100644
--- a/wpa_supplicant/doc/docbook/wpa_supplicant.sgml
+++ b/wpa_supplicant/doc/docbook/wpa_supplicant.sgml
@@ -17,6 +17,7 @@
       <arg>-c<replaceable>config file</replaceable></arg>
       <arg>-D<replaceable>driver</replaceable></arg>
       <arg>-P<replaceable>PID_file</replaceable></arg>
+      <arg>-F<replaceable>output file</replaceable></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
   <refsect1>
@@ -383,6 +384,13 @@
       </varlistentry>
 
       <varlistentry>
+	<term>-F output file</term>
+	<listitem>
+	  <para>Log output to specified file.</para>
+	</listitem>
+      </varlistentry>
+
+      <varlistentry>
 	<term>-g global ctrl_interface</term>
 	<listitem>
 	  <para>Path to global ctrl_interface socket.</para>
diff --git a/wpa_supplicant/main.c b/wpa_supplicant/main.c
index e5af13d..dc89a8d 100644
--- a/wpa_supplicant/main.c
+++ b/wpa_supplicant/main.c
@@ -40,7 +40,12 @@ static void usage(void)
 	printf("%s\n\n%s\n"
 	       "usage:\n"
 	       "  wpa_supplicant [-BddfhKLqqtuvwW] [-P<pid file>] "
-	       "[-g<global ctrl>] \\\n"
+	       "[-g<global ctrl>]"
+#ifdef CONFIG_DEBUG_FILE
+	       " [-F<output file>] \\\n"
+#else /* CONFIG_DEBUG_FILE */
+	       " \\\n"
+#endif /* CONFIG_DEBUG_FILE */
 	       "        -i<ifname> -c<config file> [-C<ctrl>] [-D<driver>] "
 	       "[-p<driver_param>] \\\n"
 	       "        [-b<br_ifname> [-N -i<ifname> -c<conf> [-C<ctrl>] "
@@ -67,6 +72,7 @@ static void usage(void)
 	       "  -D = driver name\n"
 #ifdef CONFIG_DEBUG_FILE
 	       "  -f = Log output to default log location (normally /tmp)\n"
+	       "  -F = Log output to specified file\n"
 #endif /* CONFIG_DEBUG_FILE */
 	       "  -g = global ctrl_interface\n"
 	       "  -K = include keys (passwords, etc.) in debug output\n"
@@ -146,7 +152,7 @@ int main(int argc, char *argv[])
 	wpa_supplicant_fd_workaround();
 
 	for (;;) {
-		c = getopt(argc, argv, "b:Bc:C:D:dfg:hi:KLNp:P:qtuvwW");
+		c = getopt(argc, argv, "b:Bc:C:D:dF:fg:hi:KLNp:P:qtuvwW");
 		if (c < 0)
 			break;
 		switch (c) {
@@ -179,6 +185,10 @@ int main(int argc, char *argv[])
 		case 'f':
 			params.wpa_debug_use_file = 1;
 			break;
+		case 'F':
+			params.wpa_debug_use_file = 1;
+			params.wpa_debug_file = optarg;
+			break;
 #endif /* CONFIG_DEBUG_FILE */
 		case 'g':
 			params.ctrl_interface = optarg;
diff --git a/wpa_supplicant/wpa_supplicant.c b/wpa_supplicant/wpa_supplicant.c
index 0c1a142..bcae686 100644
--- a/wpa_supplicant/wpa_supplicant.c
+++ b/wpa_supplicant/wpa_supplicant.c
@@ -1828,7 +1828,7 @@ struct wpa_global * wpa_supplicant_init(struct 
wpa_params *params)
 		return NULL;
 
 	wpa_debug_use_file = params->wpa_debug_use_file;
-	wpa_debug_open_file();
+	wpa_debug_open_file(params->wpa_debug_file);
 
 	ret = eap_peer_register_methods();
 	if (ret) {
diff --git a/wpa_supplicant/wpa_supplicant_i.h 
b/wpa_supplicant/wpa_supplicant_i.h
index 00e3162..f741244 100644
--- a/wpa_supplicant/wpa_supplicant_i.h
+++ b/wpa_supplicant/wpa_supplicant_i.h
@@ -154,6 +154,11 @@ struct wpa_params {
 	 * wpa_debug_use_file - Write debug to a file (instead of stdout)
 	 */
 	int wpa_debug_use_file;
+
+	/**
+	 * wpa_debug_file - Write debug to specified file.
+	 */
+	char *wpa_debug_file;
 };
 
 /**
-- 
1.5.3.7




More information about the HostAP mailing list