Skip to content

Commit

Permalink
Fix logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Maschell committed Apr 27, 2024
1 parent 54618b3 commit 3e1d862
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 13 deletions.
2 changes: 1 addition & 1 deletion include/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
/// \brief Log level
enum LogLevel
{
DEBUG,
DEBUGLOG,
INFO,
ERROR,
COMMAND,
Expand Down
6 changes: 3 additions & 3 deletions source/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool s_logUpdated = true;

/// \brief Message prefix
static char const *const s_prefix[] = {
[DEBUG] = "[DEBUG]",
[DEBUGLOG] = "[DEBUG]",
[INFO] = "[INFO]",
[ERROR] = "[ERROR]",
[COMMAND] = "[COMMAND]",
Expand Down Expand Up @@ -114,7 +114,7 @@ void drawLog ()

#ifdef CLASSIC
char const *const s_colors[] = {
[DEBUG] = "\x1b[33;1m", // yellow
[DEBUGLOG] = "\x1b[33;1m", // yellow
[INFO] = "\x1b[37;1m", // white
[ERROR] = "\x1b[31;1m", // red
[COMMAND] = "\x1b[32;1m", // green
Expand Down Expand Up @@ -204,7 +204,7 @@ void debug (char const *const fmt_, ...)
va_list ap;

va_start (ap, fmt_);
addLog (DEBUG, fmt_, ap);
addLog (DEBUGLOG, fmt_, ap);
va_end (ap);
#endif
}
Expand Down
42 changes: 42 additions & 0 deletions source/wiiu/logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifdef DEBUG
#include <stdint.h>
#include <whb/log_cafe.h>
#include <whb/log_module.h>
#include <whb/log_udp.h>

uint32_t moduleLogInit = false;
uint32_t cafeLogInit = false;
uint32_t udpLogInit = false;
#endif // DEBUG

void initLogging ()
{
#ifdef DEBUG
if (!(moduleLogInit = WHBLogModuleInit ()))
{
cafeLogInit = WHBLogCafeInit ();
udpLogInit = WHBLogUdpInit ();
}
#endif // DEBUG
}

void deinitLogging ()
{
#ifdef DEBUG
if (moduleLogInit)
{
WHBLogModuleDeinit ();
moduleLogInit = false;
}
if (cafeLogInit)
{
WHBLogCafeDeinit ();
cafeLogInit = false;
}
if (udpLogInit)
{
WHBLogUdpDeinit ();
udpLogInit = false;
}
#endif // DEBUG
}
67 changes: 67 additions & 0 deletions source/wiiu/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#pragma once

#include <coreinit/debug.h>
#include <string.h>
#include <whb/log.h>

#ifdef __cplusplus
extern "C" {
#endif

#define LOG_APP_TYPE "P"
#define LOG_APP_NAME "homebrew_on_menu"

#define __FILENAME_X__ (strrchr (__FILE__, '\\') ? strrchr (__FILE__, '\\') + 1 : __FILE__)
#define __FILENAME__ (strrchr (__FILE__, '/') ? strrchr (__FILE__, '/') + 1 : __FILENAME_X__)

#define LOG(LOG_FUNC, FMT, ARGS...) LOG_EX (LOG_FUNC, "", "", FMT, ##ARGS)

#define LOG_EX(LOG_FUNC, LOG_LEVEL, LINE_END, FMT, ARGS...) \
do \
{ \
LOG_FUNC ("[(%s)%18s][%23s]%30s@L%04d: " LOG_LEVEL "" FMT "" LINE_END, \
LOG_APP_TYPE, \
LOG_APP_NAME, \
__FILENAME__, \
__FUNCTION__, \
__LINE__, \
##ARGS); \
} while (0)

#ifdef DEBUG

#ifdef VERBOSE_DEBUG
#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) LOG (WHBLogPrintf, FMT, ##ARGS)
#else
#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0)
#endif

#define DEBUG_FUNCTION_LINE(FMT, ARGS...) LOG (WHBLogPrintf, FMT, ##ARGS)

#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) LOG (WHBLogWritef, FMT, ##ARGS)

#define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX (WHBLogPrintf, "##ERROR## ", "", FMT, ##ARGS)
#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX (WHBLogPrintf, "##WARN ## ", "", FMT, ##ARGS)
#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX (WHBLogPrintf, "##INFO ## ", "", FMT, ##ARGS)

#else

#define DEBUG_FUNCTION_LINE_VERBOSE(FMT, ARGS...) while (0)

#define DEBUG_FUNCTION_LINE(FMT, ARGS...) while (0)

#define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...) while (0)

#define DEBUG_FUNCTION_LINE_ERR(FMT, ARGS...) LOG_EX (OSReport, "##ERROR## ", "\n", FMT, ##ARGS)
#define DEBUG_FUNCTION_LINE_WARN(FMT, ARGS...) LOG_EX (OSReport, "##WARN ## ", "\n", FMT, ##ARGS)
#define DEBUG_FUNCTION_LINE_INFO(FMT, ARGS...) LOG_EX (OSReport, "##INFO ## ", "\n", FMT, ##ARGS)

#endif

void initLogging ();

void deinitLogging ();

#ifdef __cplusplus
}
#endif
23 changes: 14 additions & 9 deletions source/wiiu/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "IOAbstraction.h"
#include "ftpServer.h"
#include "log.h"
#include "logger.h"

#include <mocha/mocha.h>
#include <nn/ac.h>
Expand Down Expand Up @@ -86,7 +87,8 @@ MochaUtilsStatus MountWrapper (const char *mount, const char *dev, const char *m
}
else
{
error ("Failed to mount %s: %s [%d]", mount, Mocha_GetStatusStr (res), res);
DEBUG_FUNCTION_LINE_ERR (
"Failed to mount %s: %s [%d]", mount, Mocha_GetStatusStr (res), res);
}
return res;
}
Expand Down Expand Up @@ -169,7 +171,8 @@ void start_server ()
}
else
{
OSReport ("Failed to init libmocha: %s [%d]\n", Mocha_GetStatusStr (res), res);
DEBUG_FUNCTION_LINE_ERR (
"Failed to init libmocha: %s [%d]\n", Mocha_GetStatusStr (res), res);
}

server = FtpServer::create ();
Expand Down Expand Up @@ -209,7 +212,7 @@ static void gFTPServerRunningChanged (ConfigItemBoolean *item, bool newValue)
auto res = WUPSStorageAPI::Store (FTPIIU_ENABLED_STRING, sFTPServerEnabled);
if (res != WUPS_STORAGE_ERROR_SUCCESS)
{
OSReport ("Failed to store gFTPServerEnabled: %s (%d)\n",
DEBUG_FUNCTION_LINE_ERR ("Failed to store gFTPServerEnabled: %s (%d)\n",
WUPSStorageAPI::GetStatusStr (res).data (),
res);
}
Expand All @@ -232,7 +235,7 @@ static void gSystemFilesAllowedChanged (ConfigItemBoolean *item, bool newValue)
auto res = WUPSStorageAPI::Store (SYSTEM_FILES_ALLOWED_STRING, sSystemFilesAllowed);
if (res != WUPS_STORAGE_ERROR_SUCCESS)
{
OSReport ("Failed to store gSystemFilesAllowed: %s (%d)\n",
DEBUG_FUNCTION_LINE_ERR ("Failed to store gSystemFilesAllowed: %s (%d)\n",
WUPSStorageAPI::GetStatusStr (res).data (),
res);
}
Expand Down Expand Up @@ -300,6 +303,7 @@ INITIALIZE_PLUGIN ()
if (WUPSConfigAPI_Init (configOptions, ConfigMenuOpenedCallback, ConfigMenuClosedCallback) !=
WUPSCONFIG_API_RESULT_SUCCESS)
{
DEBUG_FUNCTION_LINE_ERR ("Failed to init config api");
OSFatal ("ftpiiu plugin: Failed to init config api");
}

Expand All @@ -308,7 +312,7 @@ INITIALIZE_PLUGIN ()
FTPIIU_ENABLED_STRING, sFTPServerEnabled, DEFAULT_FTPIIU_ENABLED_VALUE)) !=
WUPS_STORAGE_ERROR_SUCCESS)
{
OSReport ("ftpiiu plugin: Failed to get or create item \"%s\": %s (%d)\n",
DEBUG_FUNCTION_LINE_ERR ("Failed to get or create item \"%s\": %s (%d)\n",
FTPIIU_ENABLED_STRING,
WUPSStorageAPI_GetStatusStr (err),
err);
Expand All @@ -317,17 +321,16 @@ INITIALIZE_PLUGIN ()
sSystemFilesAllowed,
DEFAULT_SYSTEM_FILES_ALLOWED_VALUE)) != WUPS_STORAGE_ERROR_SUCCESS)
{
OSReport ("ftpiiu plugin: Failed to get or create item \"%s\": %s (%d)\n",
DEBUG_FUNCTION_LINE_ERR ("Failed to get or create item \"%s\": %s (%d)\n",
SYSTEM_FILES_ALLOWED_STRING,
WUPSStorageAPI_GetStatusStr (err),
err);
}

if ((err = WUPSStorageAPI::SaveStorage ()) != WUPS_STORAGE_ERROR_SUCCESS)
{
OSReport ("ftpiiu plugin: Failed to save storage: %s (%d)\n",
WUPSStorageAPI_GetStatusStr (err),
err);
DEBUG_FUNCTION_LINE_ERR (
"Failed to save storage: %s (%d)\n", WUPSStorageAPI_GetStatusStr (err), err);
}
}

Expand All @@ -343,6 +346,7 @@ void wiiu_init ()

ON_APPLICATION_START ()
{
initLogging ();
nn::ac::Initialize ();
nn::ac::ConnectAsync ();

Expand All @@ -352,6 +356,7 @@ ON_APPLICATION_START ()
ON_APPLICATION_ENDS ()
{
stop_server ();
deinitLogging ();
}

bool platform::init ()
Expand Down

0 comments on commit 3e1d862

Please sign in to comment.