summaryrefslogtreecommitdiff
path: root/error.c
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@sun.com>2009-09-17 18:33:54 -0700
committerAlan Coopersmith <alan.coopersmith@sun.com>2009-09-17 19:22:24 -0700
commitda3cfcdd197bd900633c563412685315316804f4 (patch)
tree2eb75e260543cf41cc264e697ff083184328f86c /error.c
parenta7e166298a94c012420713dbfcd0b1628f5c7062 (diff)
Refactor & constify error logging code
Signed-off-by: Alan Coopersmith <alan.coopersmith@sun.com>
Diffstat (limited to 'error.c')
-rw-r--r--error.c121
1 files changed, 56 insertions, 65 deletions
diff --git a/error.c b/error.c
index f1cb951..d1bd16e 100644
--- a/error.c
+++ b/error.c
@@ -42,70 +42,63 @@ from The Open Group.
#include "dm.h"
#include "dm_error.h"
-#define WRITES(fd, buf) write(fd, buf, strlen(buf))
-
-/* Append more text to the log without a new header, right after
- having called LogInfo or LogError */
-void LogAppend(char * fmt, ...)
+/* This function does the actual log message writes. */
+static inline void
+LogVWrite(const char *fmt, va_list args)
{
char buf[1024];
+ int len;
- {
- va_list args;
- va_start(args, fmt);
- vsnprintf (buf, sizeof buf, fmt, args);
- va_end(args);
+ len = vsnprintf (buf, sizeof(buf), fmt, args);
+ if (len >= sizeof(buf)) {
+ len = sizeof(buf) - 1;
}
- WRITES(STDERR_FILENO, buf);
+ write(STDERR_FILENO, buf, len);
}
-void LogInfo(char * fmt, ...)
-{
- char buf[1024];
+#define LogVarArgsWrite(fmt) \
+ do { \
+ va_list args; \
+ va_start(args, fmt); \
+ LogVWrite(fmt, args); \
+ va_end(args); \
+ } while(0)
- snprintf(buf, sizeof buf, "xdm info (pid %ld): ", (long)getpid());
- WRITES(STDERR_FILENO, buf);
- {
- va_list args;
- va_start(args, fmt);
- vsnprintf (buf, sizeof buf, fmt, args);
- va_end(args);
- }
- WRITES(STDERR_FILENO, buf);
-}
+#define LogHeader(type) \
+ LogAppend("xdm %s (pid %ld): ", type, (long)getpid())
-void LogError (char * fmt, ...)
+/* Append more text to the log without a new header, right after
+ having called LogInfo or LogError */
+void
+LogAppend(const char * fmt, ...)
{
- char buf[1024];
+ LogVarArgsWrite(fmt);
+}
- snprintf (buf, sizeof buf, "xdm error (pid %ld): ", (long)getpid());
- WRITES(STDERR_FILENO, buf);
- {
- va_list args;
- va_start(args, fmt);
- vsnprintf (buf, sizeof buf, fmt, args);
- va_end(args);
- }
- WRITES(STDERR_FILENO, buf);
+void
+LogInfo(const char * fmt, ...)
+{
+ LogHeader("info");
+ LogVarArgsWrite(fmt);
}
-void LogPanic (char * fmt, ...)
+void
+LogError (const char * fmt, ...)
{
- char buf[1024];
+ LogHeader("error");
+ LogVarArgsWrite(fmt);
+}
- snprintf (buf, sizeof buf, "xdm panic (pid %ld): ", (long)getpid());
- WRITES(STDERR_FILENO, buf);
- {
- va_list args;
- va_start(args, fmt);
- vsnprintf (buf, sizeof buf, fmt, args);
- va_end(args);
- }
- WRITES(STDERR_FILENO, buf);
+void
+LogPanic (const char * fmt, ...)
+{
+ LogHeader("panic");
+ LogVarArgsWrite(fmt);
_exit (1);
}
-void LogOutOfMem (char * fmt, ...)
+void
+LogOutOfMem (const char * fmt, ...)
{
fprintf (stderr, "xdm: out of memory in routine ");
{
@@ -117,31 +110,29 @@ void LogOutOfMem (char * fmt, ...)
fflush (stderr);
}
-void Debug (char * fmt, ...)
+void
+Debug (const char * fmt, ...)
{
char buf[1024];
if (debugLevel > 0)
{
- va_list args;
- va_start(args, fmt);
- vsnprintf (buf, sizeof buf, fmt, args);
- va_end(args);
- WRITES(STDOUT_FILENO, buf);
+ LogVarArgsWrite(fmt);
}
}
-void InitErrorLog (void)
+void
+InitErrorLog (void)
{
- int i;
- if (errorLogFile[0]) {
- i = creat (errorLogFile, 0666);
- if (i != -1) {
- if (i != 2) {
- dup2 (i, 2);
- close (i);
- }
- } else
- LogError ("Cannot open errorLogFile %s\n", errorLogFile);
- }
+ int i;
+ if (errorLogFile[0]) {
+ i = creat (errorLogFile, 0666);
+ if (i != -1) {
+ if (i != STDERR_FILENO) {
+ dup2 (i, STDERR_FILENO);
+ close (i);
+ }
+ } else
+ LogError ("Cannot open errorLogFile %s\n", errorLogFile);
+ }
}