/*******************************************************************
- * Miscellaneous utilities
+ * Error handling
*******************************************************************/
/* Set logging level */
void zck_set_log_level(zck_log_type ll);
+/* Set logging fd */
+void zck_set_log_fd(int fd);
+/* Get error message
+ * Returns char* containing error message, or NULL if there isn't one */
+char *zck_get_error(zckCtx *zck);
+/* Clear error message
+ * Returns 1 if message was cleared, 0 if error is fatal and can't be cleared */
+int zck_clear_error(zckCtx *zck);
+
+/*******************************************************************
+ * Miscellaneous utilities
+ *******************************************************************/
/* Validate the chunk and data checksums for the current file.
* Returns -1 for error, 0 for invalid checksum and 1 for valid checksum */
int zck_validate_checksums(zckCtx *zck)
--- /dev/null
+/*
+ * Copyright 2018 Jonathan Dieter <jdieter@gmail.com>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <assert.h>
+#include <string.h>
+#include <zck.h>
+
+#include "zck_private.h"
+
+void set_fatal_error(zckCtx *zck, const char *msg) {
+ assert(zck != NULL && zck->msg == NULL && msg != NULL);
+
+ zck->fatal_msg = zmalloc(strlen(msg)+1);
+ strncpy(zck->fatal_msg, msg, strlen(msg));
+}
+
+void set_error(zckCtx *zck, const char *msg) {
+ assert(zck != NULL && zck->msg == NULL && msg != NULL);
+
+ zck->msg = zmalloc(strlen(msg)+1);
+ strncpy(zck->msg, msg, strlen(msg));
+}
+
+char PUBLIC *zck_get_error(zckCtx *zck) {
+ assert(zck != NULL);
+
+ if(zck->fatal_msg)
+ return zck->fatal_msg;
+ return zck->msg;
+}
+
+int PUBLIC zck_clear_error(zckCtx *zck) {
+ assert(zck != NULL);
+
+ if(zck->fatal_msg)
+ return False;
+
+ free(zck->msg);
+ zck->msg = NULL;
+ return True;
+}
#include "zck_private.h"
static zck_log_type log_level = ZCK_LOG_ERROR;
+static int log_fd = STDERR_FILENO;
void PUBLIC zck_set_log_level(zck_log_type ll) {
log_level = ll;
}
+void PUBLIC zck_set_log_fd(int fd) {
+ log_fd = fd;
+}
+
void zck_log_wf(const char *function, zck_log_type lt, const char *format, ...) {
if(lt >= log_level) {
va_list args;
va_start(args, format);
- dprintf(STDERR_FILENO, "%s: ", function);
- vdprintf(STDERR_FILENO, format, args);
+ dprintf(log_fd, "%s: ", function);
+ vdprintf(log_fd, format, args);
va_end(args);
}
}
subdir('hash')
subdir('index')
subdir('dl')
-lib_sources += files('zck.c', 'header.c', 'io.c', 'log.c', 'compint.c')
+lib_sources += files('zck.c', 'header.c', 'io.c', 'log.c', 'compint.c', 'error.c')
zcklib = shared_library('zck',
lib_sources,
include_directories: inc,