summaryrefslogtreecommitdiff
path: root/src/lib/log
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2018-06-27 14:45:17 -0400
committerNick Mathewson <nickm@torproject.org>2018-06-27 14:45:17 -0400
commit000de2f2ac4f740be8351673691371d78a5da7e9 (patch)
tree1212f745111239dd7713c956ae60b081edfe912a /src/lib/log
parent21136037182f55b5aef3730853e65fa2c7ebd722 (diff)
parent3d606dddb9a104466906069bd21acbdfb1270b58 (diff)
downloadtor-000de2f2ac4f740be8351673691371d78a5da7e9.tar.gz
tor-000de2f2ac4f740be8351673691371d78a5da7e9.zip
Merge branch 'fs_refactor'
Diffstat (limited to 'src/lib/log')
-rw-r--r--src/lib/log/include.am7
-rw-r--r--src/lib/log/torlog.c4
-rw-r--r--src/lib/log/win32err.c56
-rw-r--r--src/lib/log/win32err.h17
4 files changed, 81 insertions, 3 deletions
diff --git a/src/lib/log/include.am b/src/lib/log/include.am
index 235c95fdf1..f0491b3863 100644
--- a/src/lib/log/include.am
+++ b/src/lib/log/include.am
@@ -11,6 +11,10 @@ src_lib_libtor_log_a_SOURCES = \
src/lib/log/torlog.c \
src/lib/log/util_bug.c
+if WIN32
+src_lib_libtor_log_a_SOURCES += src/lib/log/win32err.c
+endif
+
src_lib_libtor_log_testing_a_SOURCES = \
$(src_lib_libtor_log_a_SOURCES)
src_lib_libtor_log_testing_a_CPPFLAGS = $(AM_CPPFLAGS) $(TEST_CPPFLAGS)
@@ -23,4 +27,5 @@ noinst_HEADERS += \
src/lib/log/escape.h \
src/lib/log/ratelim.h \
src/lib/log/torlog.h \
- src/lib/log/util_bug.h
+ src/lib/log/util_bug.h \
+ src/lib/log/win32err.h
diff --git a/src/lib/log/torlog.c b/src/lib/log/torlog.c
index bffffbf1bd..1c9f33790d 100644
--- a/src/lib/log/torlog.c
+++ b/src/lib/log/torlog.c
@@ -348,7 +348,7 @@ log_tor_version(logfile_t *lf, int reset)
tor_snprintf(buf+n, sizeof(buf)-n,
"Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
}
- if (write_all_to_fd(lf->fd, buf, strlen(buf)) < 0) /* error */
+ if (write_all_to_fd_minimal(lf->fd, buf, strlen(buf)) < 0) /* error */
return -1; /* failed */
return 0;
}
@@ -562,7 +562,7 @@ logfile_deliver(logfile_t *lf, const char *buf, size_t msg_len,
lf->callback(severity, domain, msg_after_prefix);
}
} else {
- if (write_all_to_fd(lf->fd, buf, msg_len) < 0) { /* error */
+ if (write_all_to_fd_minimal(lf->fd, buf, msg_len) < 0) { /* error */
/* don't log the error! mark this log entry to be blown away, and
* continue. */
lf->seems_dead = 1;
diff --git a/src/lib/log/win32err.c b/src/lib/log/win32err.c
new file mode 100644
index 0000000000..4586c23c84
--- /dev/null
+++ b/src/lib/log/win32err.c
@@ -0,0 +1,56 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifdef _WIN32
+#include "orconfig.h"
+#include "lib/log/win32err.h"
+#include "lib/malloc/util_malloc.h"
+
+#include <tchar.h>
+#include <windows.h>
+
+/** Return a newly allocated string describing the windows system error code
+ * <b>err</b>. Note that error codes are different from errno. Error codes
+ * come from GetLastError() when a winapi call fails. errno is set only when
+ * ANSI functions fail. Whee. */
+char *
+format_win32_error(DWORD err)
+{
+ TCHAR *str = NULL;
+ char *result;
+ DWORD n;
+
+ /* Somebody once decided that this interface was better than strerror(). */
+ n = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+ FORMAT_MESSAGE_FROM_SYSTEM |
+ FORMAT_MESSAGE_IGNORE_INSERTS,
+ NULL, err,
+ MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
+ (LPVOID)&str,
+ 0, NULL);
+
+ if (str && n) {
+#ifdef UNICODE
+ size_t len;
+ if (n > 128*1024)
+ len = (128 * 1024) * 2 + 1; /* This shouldn't be possible, but let's
+ * make sure. */
+ else
+ len = n * 2 + 1;
+ result = tor_malloc(len);
+ wcstombs(result,str,len);
+ result[len-1] = '\0';
+#else /* !(defined(UNICODE)) */
+ result = tor_strdup(str);
+#endif /* defined(UNICODE) */
+ } else {
+ result = tor_strdup("<unformattable error>");
+ }
+ if (str) {
+ LocalFree(str); /* LocalFree != free() */
+ }
+ return result;
+}
+#endif /* defined(_WIN32) */
diff --git a/src/lib/log/win32err.h b/src/lib/log/win32err.h
new file mode 100644
index 0000000000..61d3af57dd
--- /dev/null
+++ b/src/lib/log/win32err.h
@@ -0,0 +1,17 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_WIN32ERR_H
+#define TOR_WIN32ERR_H
+
+#include "orconfig.h"
+
+/* Platform-specific helpers. */
+#ifdef _WIN32
+#include <windef.h>
+char *format_win32_error(DWORD err);
+#endif
+
+#endif