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

Kel Modderman kel
Mon Dec 10 03:26:49 PST 2007


On Monday 10 December 2007 03:56:49 Dan Williams wrote:
> On Sun, 2007-12-09 at 07:58 -0800, Jouni Malinen wrote:
> > On Sun, Dec 09, 2007 at 10:26:29PM +1000, Kel Modderman wrote:
> > > Is there an opportunity to control the target file location for this
> > > output?
> >
> > Not in this version. The file name (or well, template with %d in it) is
> > hardcoded in wpa_debug_open_file(). I would assume it would be
> > relatively simple change to pass in similar template as an argument to
> > that function, though.
>
> Yes, I was looking into that at first, but hadn't gotten there quite
> yet.  It would be awesome if somebody came up with a patch first :)  I
> assume that it would be as simple as adding another to struct wpa_params
> for the path to the logfile, as a char [PATH_MAX] or something like
> that.  Not sure what needs to be done on the windows side, though if the
> field is NULL then it could simply default to the current Windows
> logfile location when wpa_debug_use_file is true.
>
> Shouldn't be too hard.

It looks like you beat me to having the first patch but I'll attach my take on
it anyway.

Attached patch builds upon the enhancement previously discussed
in this thread, by allowing specification of specific filename to write debug
output to. Below is the long description of the patch:

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 311a9b5..1f4c0e1 100644
--- a/src/utils/wpa_debug.h
+++ b/src/utils/wpa_debug.h
@@ -33,7 +33,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 e81202c..85fea7a 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