aboutsummaryrefslogtreecommitdiff
path: root/src/std/log.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2019-02-27 22:26:35 +0100
committerdec05eba <dec05eba@protonmail.com>2020-07-25 14:36:46 +0200
commit76d85a10f6cda93eba29dad5372e8160af7289c8 (patch)
treecfec3043ec45a5e83494ec109e87c239dad6cc47 /src/std/log.c
parent8201cd9f40897cf6b8e6973b28a8661108702ab1 (diff)
Use multiple threads to parse
Diffstat (limited to 'src/std/log.c')
-rw-r--r--src/std/log.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/std/log.c b/src/std/log.c
new file mode 100644
index 0000000..1fd0e4e
--- /dev/null
+++ b/src/std/log.c
@@ -0,0 +1,76 @@
+#include "../../include/std/log.h"
+#include "../../include/std/thread.h"
+#include "../../include/std/misc.h"
+#include <stdio.h>
+#include <stdarg.h>
+
+static amal_mutex mutex;
+static bool mutex_initialized = bool_false;
+
+/* Safe to call multiple times */
+static void mutex_init() {
+ if(!mutex_initialized) {
+ amal_mutex_init(&mutex);
+ mutex_initialized = bool_true;
+ }
+}
+
+amal_mutex* amal_log_get_mutex() {
+ mutex_init();
+ return &mutex;
+}
+
+void amal_log_debug(const char *fmt, ...) {
+ va_list args;
+ mutex_init();
+ ignore_result_int(amal_mutex_lock(&mutex, NULL));
+ fprintf(stderr, "Debug: ");
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ ignore_result_int(amal_mutex_unlock(&mutex));
+}
+
+void amal_log_error(const char *fmt, ...) {
+ va_list args;
+ mutex_init();
+ ignore_result_int(amal_mutex_lock(&mutex, NULL));
+ fprintf(stderr, "Error: ");
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ ignore_result_int(amal_mutex_unlock(&mutex));
+}
+
+void amal_log_info(const char *fmt, ...) {
+ va_list args;
+ mutex_init();
+ ignore_result_int(amal_mutex_lock(&mutex, NULL));
+ fprintf(stderr, "Info: ");
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ ignore_result_int(amal_mutex_unlock(&mutex));
+}
+
+void amal_log_warning(const char *fmt, ...) {
+ va_list args;
+ mutex_init();
+ ignore_result_int(amal_mutex_lock(&mutex, NULL));
+ fprintf(stderr, "Warning: ");
+ va_start(args, fmt);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
+ fprintf(stderr, "\n");
+ ignore_result_int(amal_mutex_unlock(&mutex));
+}
+
+void amal_log_perror(const char *prefix) {
+ mutex_init();
+ ignore_result_int(amal_mutex_lock(&mutex, NULL));
+ perror(prefix);
+ ignore_result_int(amal_mutex_unlock(&mutex));
+} \ No newline at end of file