Skip to contentSkip to conversation

Use \r\n as newline in log files on Windows

PATCH In review David Sommerseth 5 participants 7 messages Dec 13, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use \r\n as newline in log files on Windows

The newline style is different from most Unices and Windows, where Windows uses \r\n instead of just \n.

Reported-by Thomas Reifferscheid
---
 config-win32.h |    3 +++
 error.c        |   12 ++++++++----
 2 files changed, 11 insertions(+), 4 deletions(-)
config-win32.h +3 -0
index be0b320..56c40cd 100644
@@ -275,6 +275,9 @@ typedef unsigned long in_addr_t;
 #error The Windows version of OpenVPN does not support PTHREAD yet
 #endif
 
+/* Windows uses a different NL style than *nix for logging */
+#define NEWLINE "\r\n"
+
 #ifdef _MSC_VER
 /* MSVC++ hacks */
 #pragma warning(disable:4244) // conversion from 'foo' to 'bar', possible loss of data
error.c +8 -4
index 873718c..40c8107 100644
@@ -49,6 +49,10 @@
 #endif
 #endif
 
+#ifndef NEWLINE
+#define NEWLINE "\n"
+#endif
+
 /* Globals */
 unsigned int x_debug_level; /* GLOBAL */
 
@@ -326,7 +330,7 @@ void x_msg (const unsigned int flags, const char *format, ...)
 		       prefix,
 		       prefix_sep,
 		       m1,
-		       (flags&M_NOLF) ? "" : "\n");
+		       (flags&M_NOLF) ? "" : NEWLINE);
 	    }
 	  else
 	    {
@@ -337,14 +341,14 @@ void x_msg (const unsigned int flags, const char *format, ...)
 		       prefix,
 		       prefix_sep,
 		       m1,
-		       (flags&M_NOLF) ? "" : "\n");
+		       (flags&M_NOLF) ? "" : NEWLINE);
 #else
 	      fprintf (fp, "%s %s%s%s%s",
 		       time_string (0, 0, show_usec, &gc),
 		       prefix,
 		       prefix_sep,
 		       m1,
-		       (flags&M_NOLF) ? "" : "\n");
+		       (flags&M_NOLF) ? "" : NEWLINE);
 #endif
 	    }
 	  fflush(fp);
@@ -411,7 +415,7 @@ assert_failed (const char *filename, int line)
 void
 out_of_memory (void)
 {
-  fprintf (stderr, PACKAGE_NAME ": Out of Memory\n");
+  fprintf (stderr, PACKAGE_NAME ": Out of Memory" NEWLINE);
   exit (1);
 }
 
Signature · 1 line
1.7.2.3
#1 lore raw

Use text mode file io, don't mess with the constants.

Quoted 88 lines · David Sommerseth

The newline style is different from most Unices and Windows, where Windows uses \r\n instead of just \n.

Reported-by Thomas Reifferscheid

---
 config-win32.h |    3 +++
 error.c        |   12 ++++++++----

 2 files changed, 11 insertions(+), 4 deletions(-)
config-win32.h +3 -0
index be0b320..56c40cd 100644
@@ -275,6 +275,9 @@ typedef unsigned long in_addr_t;
  #error The Windows version of OpenVPN does not support PTHREAD yet
  #endif
 
+/* Windows uses a different NL style than *nix for logging */
+#define NEWLINE "\r\n"
+
  #ifdef _MSC_VER
  /* MSVC++ hacks */
  #pragma warning(disable:4244) // conversion from 'foo' to 'bar', possible loss of data
error.c +4 -0
index 873718c..40c8107 100644
@@ -49,6 +49,10 @@
  #endif
  #endif
 
+#ifndef NEWLINE
+#define NEWLINE "\n"
+#endif
+
  /* Globals */

 unsigned int x_debug_level; /* GLOBAL */

error.c +1 -1
@@ -326,7 +330,7 @@ void x_msg (const unsigned int flags, const char *format, ...)
                       prefix,
                       prefix_sep,
                       m1,
-                      (flags&M_NOLF) ? "" : "\n");
+                      (flags&M_NOLF) ? "" : NEWLINE);
            }

         else
           {

error.c +2 -2
@@ -337,14 +341,14 @@ void x_msg (const unsigned int flags, const char *format, ...)
                       prefix,
                       prefix_sep,
                       m1,
-                      (flags&M_NOLF) ? "" : "\n");
+                      (flags&M_NOLF) ? "" : NEWLINE);
  #else
              fprintf (fp, "%s %s%s%s%s",
                       time_string (0, 0, show_usec, &gc),
                       prefix,
                       prefix_sep,
                       m1,
-                      (flags&M_NOLF) ? "" : "\n");
+                      (flags&M_NOLF) ? "" : NEWLINE);
  #endif
            }
          fflush(fp);
@@ -411,7 +415,7 @@ assert_failed (const char *filename, int line)

 void
 out_of_memory (void)
 {
-  fprintf (stderr, PACKAGE_NAME ": Out of Memory\n");
+  fprintf (stderr, PACKAGE_NAME ": Out of Memory" NEWLINE); exit (1);
 }

--
1.7.2.3

------------------------------------------------------------------------------
Oracle to DB2 Conversion Guide: Learn learn about native support for PL/SQL, new data types, scalar functions, improved concurrency, built-in packages, OCI, SQL*Plus, data movement tools, best practices and more.
http://p.sf.net/sfu/oracle-sfdev2dev
_______________________________________________
Openvpn-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel

#2 lore raw

PATCH 1/1 Open log files as text files on Windows

By giving the "t" flag to _fdopen() on Windows, the file will be opened in a "translate mode", where it will take care of converting \n to \r\n, and also look for the CTRL-Z mark when opening the log file in append mode.

Reference:
<http://msdn.microsoft.com/en-us/library/dye30d82%28VS.80%29.aspx>

Thanks to Alon Bar-Lev for pointing out this solution.

Reported-by Thomas Reifferscheid
---
 error.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
error.c +1 -1
index 873718c..887f2d0 100644
@@ -521,7 +521,7 @@ redirect_stdout_stderr (const char *file, bool append)
       
       /* open log_handle as FILE stream */
       ASSERT (msgfp == NULL);
-      msgfp = _fdopen (log_fd, "w");
+      msgfp = _fdopen (log_fd, "wt");
       if (msgfp == NULL)
 	msg (M_ERR, "Error: --log redirect failed due to _fdopen");
 
Signature · 1 line
1.7.2.3
#3 lore raw

Re: [Openvpn-devel] [PATCH] Open log files as text files on Windows

Quoted 4 lines · David Sommerseth

By giving the "t" flag to _fdopen() on Windows, the file will be opened in a "translate mode", where it will take care of converting \n to \r\n, and also look for the CTRL-Z mark when opening the log file in append mode.

It should be possible to have a consistent log format across hydrogenous hosts so that, need be, the logs can all be processed in the same fashion. (Think swatch, etc.) Which raises the following
questions in my devoid-of-MS-isms mind:

How does this affect network logging; syslog et-al?

Shouldn't there be a flag that turns this on (or off)?

Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

#4 lore raw

Re: [Openvpn-devel] [PATCH] Open log files as text files on Windows

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Quoted 12 lines · Karl O. Pinc
Quoted 4 lines · David Sommerseth

By giving the "t" flag to _fdopen() on Windows, the file will be opened in a "translate mode", where it will take care of converting \n to \r\n, and also look for the CTRL-Z mark when opening the log file in append mode.

It should be possible to have a consistent log format across hydrogenous hosts so that, need be, the logs can all be processed in the same fashion. (Think swatch, etc.) Which raises the following
questions in my devoid-of-MS-isms mind:

Quoted 4 lines

How does this affect network logging; syslog et-al?

Shouldn't there be a flag that turns this on (or off)?

This last patch will only add the "text/translate mode" on Windows platform when it opens a log file. In all other scenarios, the behaviour will be as it was before.

So seeing it in this light, Alon's remark was correct - thus my new patch which is a lot simpler. My initial patch would fail here.

In fact the WIN32 part of the redirect_stderr_stdout() function does set O_TEXT when opening a file descriptor. But it seems it was forgotten on the _fd_open() call later on. The redirect_stderr_stdout() function is also only called when --log or --log-append is used in the configuration file.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAk0GUDoACgkQDC186MBRfrqDZwCff+8ctGZfoHbDzYrZm/91W55M UcEAoKC/DXcmEm/Eln6ysQ1UZEFp71DY
=2g85
-----END PGP SIGNATURE-----

#5 lore raw

Re: [Openvpn-devel] [PATCH] Open log files as text files on Windows

Quoted 14 lines · David Sommerseth
Quoted 3 lines · Karl O. Pinc
Quoted 2 lines · David Sommerseth

By giving the "t" flag to _fdopen() on Windows, the file will be opened in a "translate mode", where it will take care of

converting

Quoted 9 lines
Quoted 2 lines

\n to \r\n, and also look for the CTRL-Z mark when opening the log file in append mode.

It should be possible to have a consistent log format across hydrogenous hosts so that, need be, the logs can all be processed in the same fashion. (Think swatch, etc.) Which raises the following
questions in my devoid-of-MS-isms mind:

Quoted 8 lines
Quoted 4 lines

How does this affect network logging; syslog et-al?

Shouldn't there be a flag that turns this on (or off)?

This last patch will only add the "text/translate mode" on Windows platform when it opens a log file. In all other scenarios, the behaviour will be as it was before.

Just because it's a file does not mean that it's local to MS Windows. E.g., there could be a samba mount involved.

It just sounds spooky to have a sometimes-this-way sometimes-that-way file format without a way to force a single file format. Especially when this is a backwards-incompatible change.

Having said that I've no immediate need for the log format to be universally uniform. We could always add a flag later if someone complains.

Karl <[email protected]...>
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

#6 lore raw

Re: [Openvpn-devel] [PATCH] Open log files as text files on Windows

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 13/12/10 21:23, Karl O. Pinc wrote:

| On 12/13/2010 10:56:28 AM, David Sommerseth wrote:
|> On 13/12/10 17:29, Karl O. Pinc wrote:
|>> On 12/13/2010 05:56:16 AM, David Sommerseth wrote:
|>>> By giving the "t" flag to _fdopen() on Windows, the file will be
|>>> opened in a "translate mode", where it will take care of
|> converting
|>>> \n to \r\n, and also look for the CTRL-Z mark when opening the log
|>>> file in append mode.
|>>
|>> It should be possible to have a consistent log format
|>> across hydrogenous hosts so that, need be, the logs can all
|>> be processed in the same fashion.  (Think swatch,
|>> etc.)  Which raises the following
|>> questions in my devoid-of-MS-isms mind:
|>>
|>> How does this affect network logging; syslog et-al?
|>>
|>> Shouldn't there be a flag that turns this on (or off)?
|>>
|>
|> This last patch will only add the "text/translate mode" on Windows
|> platform when it opens a log file.  In all other scenarios, the
|> behaviour will be as it was before.
|
| Just because it's a file does not mean that it's
| local to MS Windows.  E.g., there could be a samba mount
| involved.
|
| It just sounds spooky to have a sometimes-this-way
| sometimes-that-way file format without a way to
| force a single file format.  Especially when
| this is a backwards-incompatible change.

I do see your argument here. But I will still claim that the current implementation is more broken. If you write a log file to C:\ and open it in notepad, you get all log lines on one line. Of course, more advanced users will try to open these files in wordpad instead. But not everyone knows this. ~ Most Windows applications expects \r\n for newline.

I would even probably claim that the majority of Windows users uses these log files in a Windows environment, thus not having proper newlines are much more annoying for the majority of the users.

| Having said that I've no immediate need for
| the log format to be universally uniform.
| We could always add a flag later if someone
| complains.

System admins who sets up Windows applications to write text files to a Samba share will in most cases be aware of the origins of these files and can parse these files accordingly. It's also a pretty straight forward call using the 'file' command to get the newline format identified.

kind regards,

David Sommerseth
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

iEYEARECAAYFAk0GoQoACgkQDC186MBRfrrLfQCeISxGqyEo1dY4GESAbe2wBDOf tSIAn2HQPiubZHSOh6YMGEzpxScDRRLR
=3UCL
-----END PGP SIGNATURE-----