summaryrefslogtreecommitdiff
path: root/src/error.c
diff options
context:
space:
mode:
authordaniel <daniel@planethacker.net>2025-05-06 16:57:32 -0700
committerdaniel <daniel@planethacker.net>2025-05-06 16:57:32 -0700
commit2278df1493e064c197913e49b5d1935942d83448 (patch)
tree42f06ab2f76e2ddf228bafbb03f79621975a4534 /src/error.c
initial import
Diffstat (limited to 'src/error.c')
-rw-r--r--src/error.c57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/error.c b/src/error.c
new file mode 100644
index 0000000..c5d718a
--- /dev/null
+++ b/src/error.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <syslog.h>
+
+extern bool use_syslog;
+
+/* error() - Print messages to stderr.
+ *
+ * Args:
+ * fmt - Message with format strings.
+ * ... - Optional arguments to fill format strings.
+ *
+ * Returns:
+ * Nothing.
+ */
+void error(const char *fmt, ...) {
+ char msg[8192] = {0};
+ va_list vl;
+
+ va_start(vl, fmt);
+ vsnprintf(msg, sizeof(msg), fmt, vl);
+ va_end(vl);
+
+ fprintf(stderr, "%s\n", msg);
+
+ if (use_syslog) {
+ syslog(LOG_INFO | LOG_USER, "%s", msg);
+ }
+}
+
+/* error_fatal() - Print message to stderr and exit with EXIT_FAILURE
+ *
+ * Args:
+ * fmt - Message with format strings.
+ * ... - Optional arguments to fill format strings.
+ *
+ * Returns
+ * Nothing, but exits the program with EXIT_FAILURE
+ */
+void error_fatal(const char *fmt, ...) {
+ char msg[8192] = {0};
+ va_list vl;
+
+ va_start(vl, fmt);
+ vsnprintf(msg, sizeof(msg), fmt, vl);
+ va_end(vl);
+
+ fprintf(stderr, "%s\n", msg);
+
+ if (use_syslog) {
+ syslog(LOG_INFO | LOG_USER, "%s", msg);
+ }
+
+ exit(EXIT_FAILURE);
+}