mirror of
https://github.com/rvtr/ctr_Repair.git
synced 2025-10-31 13:51:08 -04:00
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@548 385bec56-5757-e545-9c3a-d8741f4650f1
288 lines
7.3 KiB
C++
288 lines
7.3 KiB
C++
/*---------------------------------------------------------------------------*
|
|
Project: Horizon
|
|
File: SdLogger.cpp
|
|
|
|
Copyright 2009 Nintendo. All rights reserved.
|
|
|
|
These coded instructions, statements, and computer programs contain
|
|
proprietary information of Nintendo of America Inc. and/or Nintendo
|
|
Company Ltd., and are protected by Federal copyright law. They may
|
|
not be disclosed to third parties or copied or duplicated in any form,
|
|
in whole or in part, without the prior written consent of Nintendo.
|
|
|
|
$Rev$
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
#include "SdLogger.h"
|
|
#include <string>
|
|
#include "SDMountManager.h"
|
|
#include "FileName.h"
|
|
|
|
namespace common
|
|
{
|
|
|
|
namespace Logger
|
|
{
|
|
|
|
static SdLogger s_SdLogger;
|
|
static nn::os::LightEvent s_SdEjectedEvent;
|
|
static nn::os::LightEvent s_SdInsertedEvent;
|
|
|
|
const size_t SDMC_EVENT_THREAD_STACK_SIZE = 1024;
|
|
nn::os::Thread s_SdmcEjectedEventThread;
|
|
nn::os::StackBuffer<SDMC_EVENT_THREAD_STACK_SIZE> s_SdmcEjectedEventThreadStack;
|
|
|
|
nn::os::Thread s_SdmcInsertedEventThread;
|
|
nn::os::StackBuffer<SDMC_EVENT_THREAD_STACK_SIZE> s_SdmcInsertedEventThreadStack;
|
|
|
|
void (*s_SdEjectedEventFunc)() = NULL;
|
|
void (*s_SdInsertedEventFunc)() = NULL;
|
|
|
|
void PrintResultIfFailed(nn::Result result, u32 line = 0)
|
|
{
|
|
if (result.IsFailure())
|
|
{
|
|
if(line != 0)
|
|
{
|
|
NN_LOG("%s, %d\n", __func__, line);
|
|
}
|
|
NN_DBG_PRINT_RESULT(result);
|
|
}
|
|
}
|
|
|
|
void SdmcEjectedEventThreadFunc()
|
|
{
|
|
NN_LOG("********************:SD Ejected Event Thread Start**********************\n");
|
|
|
|
for(;;)
|
|
{
|
|
s_SdEjectedEvent.Wait();
|
|
NN_LOG("********************:SD Card Ejected**********************\n");
|
|
|
|
if(s_SdEjectedEventFunc != NULL)
|
|
{
|
|
s_SdEjectedEventFunc();
|
|
}
|
|
SdMountManager::ForceUnmount();
|
|
s_SdEjectedEvent.ClearSignal();
|
|
s_SdLogger.Inactivate();
|
|
}
|
|
}
|
|
|
|
void SdmcInsertedEventThreadFunc()
|
|
{
|
|
NN_LOG("********************:SD Inserted Event Thread Start**********************\n");
|
|
|
|
for(;;)
|
|
{
|
|
s_SdInsertedEvent.Wait();
|
|
NN_LOG("********************:SD Card Inserted*********************\n");
|
|
|
|
if(s_SdInsertedEventFunc != NULL)
|
|
{
|
|
s_SdInsertedEventFunc();
|
|
}
|
|
SdMountManager::ForceUnmount();
|
|
s_SdInsertedEvent.ClearSignal();
|
|
s_SdLogger.Inactivate();
|
|
}
|
|
}
|
|
|
|
void InitializeEjectThread()
|
|
{
|
|
s_SdEjectedEvent.Initialize(true);
|
|
s_SdInsertedEvent.Initialize(true);
|
|
nn::fs::RegisterSdmcEjectedEvent(&s_SdEjectedEvent);
|
|
nn::fs::RegisterSdmcInsertedEvent(&s_SdInsertedEvent);
|
|
|
|
// SDカード抜けを検知するためのスレッド作成
|
|
s_SdmcEjectedEventThread.Start(SdmcEjectedEventThreadFunc, s_SdmcEjectedEventThreadStack);
|
|
|
|
// SDカード挿入を検知するためのスレッド作成
|
|
s_SdmcInsertedEventThread.Start(SdmcInsertedEventThreadFunc, s_SdmcInsertedEventThreadStack);
|
|
|
|
}
|
|
void SetEjectHandler(void (*func)())
|
|
{
|
|
s_SdEjectedEventFunc = func;
|
|
}
|
|
|
|
void SetInsertHandler(void (*func)())
|
|
{
|
|
s_SdInsertedEventFunc = func;
|
|
}
|
|
|
|
SdLogger::SdLogger() : m_TryActivate(false), m_Permitted(false)
|
|
{
|
|
|
|
}
|
|
|
|
SdLogger* GetSdInstance()
|
|
{
|
|
return &s_SdLogger;
|
|
}
|
|
|
|
void SdLogger::Print(const char* fmt, ::std::va_list arg)
|
|
{
|
|
Activate();
|
|
nn::Result result;
|
|
result = SdMountManager::Mount();
|
|
if (result.IsFailure())
|
|
{
|
|
PrintResultIfFailed(result, __LINE__);
|
|
}
|
|
|
|
s32 stringSize;
|
|
const size_t STRING_BUFFER_SIZE = 256;
|
|
char str[STRING_BUFFER_SIZE];
|
|
|
|
stringSize = nn::nstd::TVSNPrintf(str, sizeof(str), fmt, arg);
|
|
|
|
// 書き込み不可であればバッファリングのみ行う
|
|
if(!m_Permitted)
|
|
{
|
|
NN_LOG("SD Write Not Permitted\n");
|
|
m_Buffer.push_back(std::string(str));
|
|
return;
|
|
}
|
|
|
|
// バッファリング済みならまず吐き出す
|
|
for(std::vector<std::string>::iterator it = m_Buffer.begin(); it != m_Buffer.end(); it++)
|
|
{
|
|
PrintCore(it->c_str(), it->size());
|
|
}
|
|
m_Buffer.clear();
|
|
|
|
PrintCore(str, stringSize);
|
|
|
|
SdMountManager::Unmount();
|
|
}
|
|
|
|
nn::Result SdLogger::PrintCore(const char* str, size_t size)
|
|
{
|
|
nn::Result result;
|
|
::std::wstring log(common::SDMC_ROOT_DIRECTORY_PATH);
|
|
log += common::LOG_PATHNAME;
|
|
|
|
// ディレクトリが無ければ作る
|
|
nn::fs::Directory dir;
|
|
result = dir.TryInitialize(common::LOG_ROOT_DIRECTORY_PATH);
|
|
if(result.IsFailure())
|
|
{
|
|
result = nn::fs::TryCreateDirectory(common::LOG_ROOT_DIRECTORY_PATH);
|
|
}
|
|
|
|
result = sd.TryInitialize(log.c_str(), true);
|
|
if (result.IsSuccess())
|
|
{
|
|
// 追記する
|
|
// サイズ取得
|
|
s64 fileSize;
|
|
result = sd.TryGetSize(&fileSize);
|
|
if (result.IsSuccess())
|
|
{
|
|
// 末尾に移動
|
|
result = sd.TrySetPosition(fileSize);
|
|
if (result.IsSuccess())
|
|
{
|
|
s32 writeSize;
|
|
result = sd.TryWrite(&writeSize, str, size, true);
|
|
if (result.IsSuccess())
|
|
{
|
|
result = sd.TryFlush();
|
|
if (result.IsFailure())
|
|
{
|
|
NN_LOG("SD TryFlush failed\n");
|
|
PrintResultIfFailed(result, __LINE__);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
NN_LOG("SD TryWrite failed\n");
|
|
PrintResultIfFailed(result, __LINE__);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
NN_LOG("SD TrySetPosition failed\n");
|
|
PrintResultIfFailed(result, __LINE__);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
NN_LOG("SD TryGetSize failed\n");
|
|
PrintResultIfFailed(result, __LINE__);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
NN_LOG("SD TryInitialize failed, %s, %d\n", __func__, __LINE__);
|
|
PrintResultIfFailed(result, __LINE__);
|
|
}
|
|
|
|
dir.Finalize();
|
|
sd.Finalize();
|
|
|
|
return nn::ResultSuccess();
|
|
}
|
|
|
|
void SdLogger::Clear()
|
|
{
|
|
Activate();
|
|
if(!m_Permitted)
|
|
{
|
|
return;
|
|
}
|
|
|
|
nn::Result result;
|
|
SdMountManager::Mount();
|
|
|
|
::std::wstring log(common::SDMC_ROOT_DIRECTORY_PATH);
|
|
log += common::LOG_PATHNAME;
|
|
|
|
result = nn::fs::TryDeleteFile(log.c_str());
|
|
if(result.IsFailure())
|
|
{
|
|
NN_DBG_PRINT_RESULT(result);
|
|
}
|
|
|
|
SdMountManager::Unmount();
|
|
}
|
|
|
|
void SdLogger::Inactivate()
|
|
{
|
|
m_TryActivate = false;
|
|
m_Permitted = false;
|
|
}
|
|
|
|
void SdLogger::Activate()
|
|
{
|
|
if(m_TryActivate)
|
|
{
|
|
return;
|
|
}
|
|
|
|
nn::Result result;
|
|
result = common::SdMountManager::Mount();
|
|
|
|
if (result.IsSuccess())
|
|
{
|
|
nn::fs::FileInputStream fis;
|
|
|
|
result = fis.TryInitialize(common::AP_SETTING_PATHNAME);
|
|
if(result.IsSuccess())
|
|
{
|
|
m_Permitted = true;
|
|
}
|
|
fis.Finalize();
|
|
}
|
|
|
|
common::SdMountManager::Unmount();
|
|
m_TryActivate = true;
|
|
}
|
|
|
|
} // namespace Logger
|
|
} // namespace ConsoleBackup
|
|
|
|
|