ctr_Repair/trunk/ConsoleDataMigration/common/SdLogger.cpp
N2614 5ed32bde66 本体データ移行ツールを追加
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-05-23%20-%20ctr.7z%20+%20svn_v1.068.zip/ctr/svn/ctr_Repair@2 385bec56-5757-e545-9c3a-d8741f4650f1
2011-01-28 10:46:13 +00:00

196 lines
5.2 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"
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", __FILE__, line);
}
nn::dbg::PrintResult(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();
}
}
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();
}
s_SdInsertedEvent.ClearSignal();
}
}
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()
{
}
SdLogger* GetSdInstance()
{
return &s_SdLogger;
}
void SdLogger::Print(const char* fmt, ::std::va_list arg)
{
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);
::std::wstring log(SDMC_ROOT_NAME);
log += LOG_FILENAME;
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, stringSize, 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", __FILE__, __LINE__);
PrintResultIfFailed(result, __LINE__);
}
sd.Finalize();
SdMountManager::Unmount();
}
} // namespace Logger
} // namespace ConsoleBackup