summaryrefslogtreecommitdiff
path: root/Build/source/libs/pplib/pplib-src/src/util/utillog.c
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/libs/pplib/pplib-src/src/util/utillog.c')
-rw-r--r--Build/source/libs/pplib/pplib-src/src/util/utillog.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/Build/source/libs/pplib/pplib-src/src/util/utillog.c b/Build/source/libs/pplib/pplib-src/src/util/utillog.c
new file mode 100644
index 00000000000..6d32514a7a7
--- /dev/null
+++ b/Build/source/libs/pplib/pplib-src/src/util/utillog.c
@@ -0,0 +1,60 @@
+
+#include <stdio.h>
+#include <string.h> // strlen
+#include <stdarg.h>
+#include "utillog.h"
+
+#define LOGGER_BUFFER_SIZE 256
+#define LOGGER_PREFIX_SIZE 32
+
+typedef struct {
+ logger_function callback;
+ void *context;
+ size_t pfxlen;
+} logger_struct;
+
+static logger_struct logger = { 0, NULL, 0 };
+
+static char logger_buffer[LOGGER_BUFFER_SIZE+LOGGER_PREFIX_SIZE];
+
+void loggerf (const char *format, ...)
+{
+ va_list args;
+ int length;
+
+ va_start(args, format);
+ length = vsnprintf(logger_buffer + logger.pfxlen, LOGGER_BUFFER_SIZE, format, args);
+ if (length > 0)
+ {
+ if (length > LOGGER_BUFFER_SIZE)
+ length = LOGGER_BUFFER_SIZE;
+ }
+ else
+ {
+ loggerf("logger encoding error '%s'", format);
+ length = (int)strlen(logger_buffer);
+ }
+ length += (int)logger.pfxlen;
+ if (logger.callback)
+ logger.callback(logger_buffer, logger.context);
+ else
+ printf("\n%s\n", logger_buffer);
+ va_end(args);
+}
+
+void logger_callback (logger_function callback, void *context)
+{
+ logger.callback = callback;
+ logger.context = context;
+}
+
+int logger_prefix (const char *prefix)
+{
+ size_t pfxlen;
+ pfxlen = strlen(prefix);
+ if (pfxlen > LOGGER_PREFIX_SIZE)
+ return 0;
+ memcpy(logger_buffer, prefix, pfxlen);
+ logger.pfxlen = pfxlen;
+ return 1;
+}