aboutsummaryrefslogtreecommitdiff
path: root/src/common.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-07-16 23:23:03 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-07-16 23:56:42 +0200
commit1842fa90dddd257b8b144a8592a893c8e101d3a1 (patch)
treee213fb0ca5ab58a2cd5656f22053b04690a761b6 /src/common.c
parente00d2079b57afd61bb05b4fc5bea14c67313c24c (diff)
downloadxK-1842fa90dddd257b8b144a8592a893c8e101d3a1.tar.gz
xK-1842fa90dddd257b8b144a8592a893c8e101d3a1.tar.xz
xK-1842fa90dddd257b8b144a8592a893c8e101d3a1.zip
Revise usage of print_{error,fatal}()
Let's limit print_fatal() to unexpected conditions. Also added exit_fatal() to save a few lines of code.
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c38
1 files changed, 15 insertions, 23 deletions
diff --git a/src/common.c b/src/common.c
index eb69b80..73f8e5c 100644
--- a/src/common.c
+++ b/src/common.c
@@ -131,11 +131,19 @@ log_message (int priority, const char *quote, const char *fmt, ...)
va_end (ap);
}
+// `fatal' is reserved for unexpected failures that would harm further operation
+
#define print_fatal(...) log_message (LOG_ERR, "fatal: ", __VA_ARGS__)
#define print_error(...) log_message (LOG_ERR, "error: ", __VA_ARGS__)
#define print_warning(...) log_message (LOG_WARNING, "warning: ", __VA_ARGS__)
#define print_status(...) log_message (LOG_INFO, "-- ", __VA_ARGS__)
+#define exit_fatal(...) \
+ BLOCK_START \
+ print_fatal (__VA_ARGS__); \
+ exit (EXIT_FAILURE); \
+ BLOCK_END
+
// --- Debugging and assertions ------------------------------------------------
// We should check everything that may possibly fail with at least a soft
@@ -191,10 +199,7 @@ xmalloc (size_t n)
{
void *p = malloc (n);
if (!p)
- {
- print_fatal ("malloc: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("malloc: %s", strerror (errno));
return p;
}
@@ -203,10 +208,7 @@ xcalloc (size_t n, size_t m)
{
void *p = calloc (n, m);
if (!p && n && m)
- {
- print_fatal ("calloc: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("calloc: %s", strerror (errno));
return p;
}
@@ -215,10 +217,7 @@ xrealloc (void *o, size_t n)
{
void *p = realloc (o, n);
if (!p && n)
- {
- print_fatal ("realloc: %s", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("realloc: %s", strerror (errno));
return p;
}
@@ -228,8 +227,7 @@ xreallocarray (void *o, size_t n, size_t m)
if (m && n > SIZE_MAX / m)
{
errno = ENOMEM;
- print_fatal ("reallocarray: %s", strerror (errno));
- exit (EXIT_FAILURE);
+ exit_fatal ("reallocarray: %s", strerror (errno));
}
return xrealloc (o, n * m);
}
@@ -1170,10 +1168,7 @@ poller_run (struct poller *self)
while (n_fds == -1 && errno == EINTR);
if (n_fds == -1)
- {
- print_fatal ("%s: %s", "epoll", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "epoll", strerror (errno));
poller_timers_dispatch (&self->timers);
@@ -1312,10 +1307,7 @@ poller_run (struct poller *self)
while (result == -1 && errno == EINTR);
if (result == -1)
- {
- print_fatal ("%s: %s", "poll", strerror (errno));
- exit (EXIT_FAILURE);
- }
+ exit_fatal ("%s: %s", "poll", strerror (errno));
poller_timers_dispatch (&self->timers);
@@ -1931,7 +1923,7 @@ call_write_default_config (const char *hint, const struct config_item *table)
char *filename = write_default_config (hint, prolog, table, &e);
if (!filename)
{
- print_fatal ("%s", e->message);
+ print_error ("%s", e->message);
error_free (e);
exit (EXIT_FAILURE);
}