summaryrefslogtreecommitdiff
path: root/src/logger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/logger.cpp')
-rw-r--r--src/logger.cpp114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/logger.cpp b/src/logger.cpp
new file mode 100644
index 0000000..38d17b7
--- /dev/null
+++ b/src/logger.cpp
@@ -0,0 +1,114 @@
+/*
+* Copyright (c) 2025 Aldrik Ramaekers <aldrik.ramaekers@gmail.com>
+*
+* Permission to use, copy, modify, and/or distribute this software for any
+* purpose with or without fee is hereby granted, provided that the above
+* copyright notice and this permission notice appear in all copies.
+*
+* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <time.h>
+#include "timer.h"
+#include "logger.hpp"
+
+namespace logger {
+
+ static logger::program_log g_log = {0};
+
+ static void log_message(const char* fmt, ImVec4 color, va_list args)
+ {
+ vsnprintf(g_log.history[g_log.write_cursor], logger::MAX_LEN_LOG_TXT, fmt, args);
+ g_log.colors[g_log.write_cursor] = color;
+
+ tick_t ms_since_epoch = timer_system();
+ char time_buf[50];
+ time_t seconds = (time_t)(ms_since_epoch / 1000);
+ int milliseconds = (int)(ms_since_epoch % 1000);
+
+ // Convert to local time
+ struct tm tm_time;
+ #if defined(_WIN32)
+ localtime_s(&tm_time, &seconds);
+ #else
+ localtime_r(&seconds, &tm_time);
+ #endif
+
+ snprintf(time_buf, 50, "%02d:%02d %02d.%03d",
+ tm_time.tm_hour,
+ tm_time.tm_min,
+ tm_time.tm_sec,
+ milliseconds);
+
+ char tmp[logger::MAX_LEN_LOG_TXT];
+ snprintf(tmp, logger::MAX_LEN_LOG_TXT, "[%s] %s", time_buf, g_log.history[g_log.write_cursor]);
+ tmp[logger::MAX_LEN_LOG_TXT-1] = 0;
+ memcpy(g_log.history[g_log.write_cursor], tmp, logger::MAX_LEN_LOG_TXT);
+
+ g_log.write_cursor++;
+ if (g_log.write_cursor >= logger::MAX_LEN_LOG_HISTORY) g_log.write_cursor = 0;
+
+ g_log.history_length++;
+ if (g_log.history_length > logger::MAX_LEN_LOG_HISTORY) g_log.history_length = logger::MAX_LEN_LOG_HISTORY;
+ }
+
+ logger::program_log* get()
+ {
+ return &g_log;
+ }
+
+ void clear()
+ {
+ g_log.write_cursor = 0;
+ g_log.history_length = 0;
+ }
+
+ void aerr(a_err errors)
+ {
+ if (errors & A_ERR_GENERIC) logger::error(" ERROR: Generic error");
+ if (errors & A_ERR_NOT_FOUND) logger::error(" ERROR: Not found");
+ if (errors & A_ERR_MISSING_DESCRIPTION) logger::error(" ERROR: Missing description");
+ if (errors & A_ERR_CODE_EXISTS) logger::error(" ERROR: Code already exists");
+ if (errors & A_ERR_MISSING_BILLING_ITEMS) logger::error(" ERROR: Missing billing items");
+ if (errors & A_ERR_INVALID_ADDRESSEE) logger::error(" ERROR: Invalid addressee");
+ if (errors & A_ERR_INVALID_CUSTOMER) logger::error(" ERROR: Invalid customer");
+ if (errors & A_ERR_INVALID_SUPPLIER) logger::error(" ERROR: Invalid supplier");
+ if (errors & A_ERR_MISSING_NAME) logger::error(" ERROR: Missing name");
+ if (errors & A_ERR_MISSING_CITY) logger::error(" ERROR: Missing city");
+ if (errors & A_ERR_MISSING_POSTAL) logger::error(" ERROR: Missing postal code");
+ if (errors & A_ERR_MISSING_ADDRESS1) logger::error(" ERROR: Missing address line 1");
+ if (errors & A_ERR_MISSING_COUNTRYCODE) logger::error(" ERROR: Missing country code");
+ if (errors & A_ERR_MISSING_TAXID) logger::error(" ERROR: Missing tax ID");
+ if (errors & A_ERR_MISSING_BUSINESSID) logger::error(" ERROR: Missing business ID");
+ if (errors & A_ERR_MAX_ITEMS_REACHED) logger::error(" ERROR: Maximum items reached");
+ if (errors & A_ERR_MISSING_CODE) logger::error(" ERROR: Missing code");
+ if (errors & A_ERR_MISSING_EMAIL) logger::error(" ERROR: Missing email");
+ if (errors & A_ERR_MISSING_TAX_RATE) logger::error(" ERROR: Missing tax rate");
+ if (errors & A_ERR_INVALID_BILLING_ITEM) logger::error(" ERROR: Invalid billing item");
+ }
+
+ void error(const char* fmt, ...)
+ {
+ va_list args;
+ va_start(args, fmt);
+ log_message(fmt, ImVec4(1,0,0,1), args);
+ va_end(args);
+ }
+
+ void info(const char* fmt, ...)
+ {
+ va_list args;
+ va_start(args, fmt);
+ log_message(fmt, ImVec4(1,1,1,1), args);
+ va_end(args);
+ }
+
+} \ No newline at end of file