GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
mgba_logger.c
1#include "mgba_logger.h"
2
3#ifdef MGBA_LOGGING
4
5#include <stdarg.h>
6#include <stdint.h>
7#include <stdio.h>
8#include <string.h>
9#include <tonc.h>
10
11#define MGBA_REG_DEBUG_ENABLE ((vu16*)0x4FFF780)
12#define MGBA_REG_DEBUG_FLAGS ((vu16*)0x4FFF700)
13#define MGBA_REG_DEBUG_STRING ((char*)0x4FFF600)
14
15static const u32 MGBA_ENABLE_MAGIC = 0xC0DE;
16static const u32 MGBA_ENABLE_OK = 0x1DEA;
17static const u32 MGBA_LOG_SEND = 0x100;
18static const u32 MGBA_LOG_BUFFER_SIZE = 0x100;
19static const u16 MGBA_LOG_LEVEL_MASK = 0x7;
20
21static bool s_mgba_logger_available = false;
22
23bool mgba_logger_init(void)
24{
25 *MGBA_REG_DEBUG_ENABLE = MGBA_ENABLE_MAGIC;
26 s_mgba_logger_available = (*MGBA_REG_DEBUG_ENABLE == MGBA_ENABLE_OK);
27 return s_mgba_logger_available;
28}
29
30static void mgba_vprintf(MgbaLogLevel level, const char* fmt, va_list args)
31{
32 if (!s_mgba_logger_available || fmt == NULL)
33 return;
34
35 vsnprintf(MGBA_REG_DEBUG_STRING, MGBA_LOG_BUFFER_SIZE, fmt, args);
36
37 *MGBA_REG_DEBUG_FLAGS = ((uint16_t)level & MGBA_LOG_LEVEL_MASK) | MGBA_LOG_SEND;
38}
39
40void mgba_printf(MgbaLogLevel level, const char* fmt, ...)
41{
42 va_list args;
43 va_start(args, fmt);
44 mgba_vprintf(level, fmt, args);
45 va_end(args);
46}
47
48void mgba_func_printf(MgbaLogLevel level, const char* func_name, const char* fmt, ...)
49{
50 if (!s_mgba_logger_available || func_name == NULL || fmt == NULL)
51 {
52 // The one place where we can't log the error.
53 return;
54 }
55
56 char printed_str_buff[MGBA_LOG_BUFFER_SIZE];
57
58 // Expand the format first so the full string is truncated in case it's too long
59 va_list args;
60 va_start(args, fmt);
61 vsnprintf(printed_str_buff, sizeof(printed_str_buff), fmt, args);
62 va_end(args);
63 mgba_printf(level, "%s(): %s", func_name, printed_str_buff);
64}
65
66#else
67
68// Noop stubs
70{
71 return false;
72}
73
74void mgba_printf(MgbaLogLevel level, const char* fmt, ...)
75{
76}
77
78void mgba_func_printf(MgbaLogLevel level, const char* func_name, const char* fmt, ...)
79{
80}
81#endif
Interface to interact with the mgba logger.
bool mgba_logger_init(void)
Initialize mgba logger.
Definition mgba_logger.c:69
void mgba_func_printf(MgbaLogLevel level, const char *func_name, const char *fmt,...)
Print to mgba log with a format string and function name.
Definition mgba_logger.c:78
void mgba_printf(MgbaLogLevel level, const char *fmt,...)
Print to mgba log with a format string.
Definition mgba_logger.c:74