mutexでwrite()とprintf()を排他制御に。

ファイルオープンのタイミングの変更。
write()とprintf()の頭でinit済みかどうかのチェックを入れた。

git-svn-id: file:///Users/lillianskinner/Downloads/platinum/twl/TwlIPL/trunk@2296 b08762b0-b915-fc4b-9d8c-17b2551a87ff
This commit is contained in:
aoki_ryoma 2008-08-28 01:21:56 +00:00
parent 3ee3435309
commit 31d232d74a
2 changed files with 59 additions and 16 deletions

View File

@ -61,7 +61,7 @@ typedef enum CheckStatus {
/*-- global variables ----------------------*/ /*-- global variables ----------------------*/
ErrorLogWork elWork; ErrorLogWork elWork;
static BOOL isInitialized = FALSE;
/*-- function prototype ----------------------*/ /*-- function prototype ----------------------*/
CheckStatus ERRORLOGi_CheckAndCreateDirectory( const char *path ); CheckStatus ERRORLOGi_CheckAndCreateDirectory( const char *path );
CheckStatus ERRORLOGi_CheckAndCreateFile( const char *path ); CheckStatus ERRORLOGi_CheckAndCreateFile( const char *path );
@ -92,15 +92,28 @@ static char *s_strError[FATAL_ERROR_MAX];
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
BOOL ERRORLOG_Init( void* (*AllocFunc) (u32) , void (*FreeFunc) (void*) ) BOOL ERRORLOG_Init( void* (*AllocFunc) (u32) , void (*FreeFunc) (void*) )
{ {
if( isInitialized )
{
return FALSE ;
}
SDK_POINTER_ASSERT(AllocFunc); SDK_POINTER_ASSERT(AllocFunc);
SDK_POINTER_ASSERT(FreeFunc); SDK_POINTER_ASSERT(FreeFunc);
elWork.Alloc = AllocFunc; elWork.Alloc = AllocFunc;
elWork.Free = FreeFunc; elWork.Free = FreeFunc;
OS_InitMutex( &elWork.mutex );
OS_LockMutex( &elWork.mutex );
// ログ読み出し用のバッファを確保 // ログ読み出し用のバッファを確保
elWork.entry = (ErrorLogEntry*) elWork.Alloc ( sizeof (ErrorLogEntry) * ERRORLOG_NUM_ENTRY ); if( elWork.entry == NULL )
MI_CpuClear8( elWork.entry, sizeof (ErrorLogEntry) * ERRORLOG_NUM_ENTRY); {
elWork.entry = (ErrorLogEntry*) elWork.Alloc ( sizeof (ErrorLogEntry) * ERRORLOG_NUM_ENTRY );
MI_CpuClear8( elWork.entry, sizeof (ErrorLogEntry) * ERRORLOG_NUM_ENTRY);
}
SDK_ASSERT( elWork.entry );
if( !FS_IsAvailable() ) if( !FS_IsAvailable() )
{ {
@ -113,12 +126,15 @@ BOOL ERRORLOG_Init( void* (*AllocFunc) (u32) , void (*FreeFunc) (void*) )
// ファイルの存在確認 // ファイルの存在確認
if( ERRORLOGi_CheckAndCreateDirectory( ERRORLOG_DIRECTORYPATH ) == CHECK_FAILED ) if( ERRORLOGi_CheckAndCreateDirectory( ERRORLOG_DIRECTORYPATH ) == CHECK_FAILED )
{ {
OS_UnlockMutex( &elWork.mutex );
return FALSE; return FALSE;
} }
// ファイルの存在を確認、ついでに中でオープンしてエントリの読み込み
switch ( ERRORLOGi_CheckAndCreateFile( ERRORLOG_FILEPATH ) ) switch ( ERRORLOGi_CheckAndCreateFile( ERRORLOG_FILEPATH ) )
{ {
case CHECK_FAILED: case CHECK_FAILED:
OS_UnlockMutex( &elWork.mutex );
return FALSE; return FALSE;
break; break;
@ -132,6 +148,10 @@ BOOL ERRORLOG_Init( void* (*AllocFunc) (u32) , void (*FreeFunc) (void*) )
break; break;
} }
FS_CloseFile( &elWork.file );
OS_UnlockMutex( &elWork.mutex );
isInitialized = TRUE;
return TRUE; return TRUE;
} }
@ -159,10 +179,7 @@ void ERRORLOG_End( void )
elWork.Free( elWork.entry ); elWork.Free( elWork.entry );
if( !FS_CloseFile( &elWork.file ) ) isInitialized = FALSE;
{
OS_TPrintf("EL Error: FS_CloseFile() failed.\n" );
}
} }
/*---------------------------------------------------------------------------* /*---------------------------------------------------------------------------*
@ -178,10 +195,18 @@ BOOL ERRORLOG_Printf( const char *fmt, ... )
{ {
BOOL result = TRUE; BOOL result = TRUE;
va_list arglist; va_list arglist;
if( !isInitialized )
{
return FALSE;
}
OS_LockMutex( &elWork.mutex );
va_start( arglist, fmt ); va_start( arglist, fmt );
result = ERRORLOGi_WriteCommon( FALSE, 0, fmt, arglist ); result = ERRORLOGi_WriteCommon( FALSE, 0, fmt, arglist );
va_end( arglist ); va_end( arglist );
OS_UnlockMutex( &elWork.mutex );
return result; return result;
} }
@ -196,7 +221,18 @@ BOOL ERRORLOG_Printf( const char *fmt, ... )
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
BOOL ERRORLOG_Write( u64 errorCode ) BOOL ERRORLOG_Write( u64 errorCode )
{ {
return ERRORLOGi_WriteCommon( TRUE, errorCode, NULL, NULL ); BOOL res;
if( !isInitialized )
{
return FALSE;
}
OS_LockMutex( &elWork.mutex );
res = ERRORLOGi_WriteCommon( TRUE, errorCode, NULL, NULL );
OS_UnlockMutex( &elWork.mutex );
return res;
} }
/*---------------------------------------------------------------------------* /*---------------------------------------------------------------------------*
@ -673,9 +709,6 @@ void ERRORLOGi_WriteLogToBuf( char *buf )
int counter; int counter;
int counterMax = elWork.numEntry <= ERRORLOG_NUM_ENTRY ? elWork.numEntry : ERRORLOG_NUM_ENTRY ; int counterMax = elWork.numEntry <= ERRORLOG_NUM_ENTRY ? elWork.numEntry : ERRORLOG_NUM_ENTRY ;
// ファイルの頭に戻って書き込みなおす
FS_SeekFileToBegin( &elWork.file );
for( counter = 0; counter < counterMax ; counter++ ) for( counter = 0; counter < counterMax ; counter++ )
{ {
// bufに一エントリずつ文字列化して詰めていく // bufに一エントリずつ文字列化して詰めていく
@ -704,20 +737,23 @@ void ERRORLOGi_WriteLogToBuf( char *buf )
BOOL ERRORLOGi_WriteLogToFile( char *buf ) BOOL ERRORLOGi_WriteLogToFile( char *buf )
{ {
FSResult res; // ファイルの頭に戻って書き込みなおす
FS_OpenFileEx( &elWork.file, ERRORLOG_FILEPATH, FS_FILEMODE_W );
FS_SeekFileToBegin( &elWork.file ); FS_SeekFileToBegin( &elWork.file );
if( FS_WriteFile( &elWork.file, buf, ERRORLOG_SIZE ) != ERRORLOG_SIZE ) if( FS_WriteFile( &elWork.file, buf, ERRORLOG_SIZE ) != ERRORLOG_SIZE )
{ {
OS_TPrintf("EL Error: FS_WriteFile() failed.\n"); OS_TPrintf("EL Error: FS_WriteFile() failed.\n");
FS_CloseFile( &elWork.file );
return FALSE; return FALSE;
} }
if( ( res = FS_FlushFile( &elWork.file )) != FS_RESULT_SUCCESS ) if( ! FS_CloseFile( &elWork.file ) )
{ {
OS_TPrintf("EL Error: FS_FlushFile() failed. FSResult: %d\n", res); OS_TPrintf("EL Error: FS_CloseFile() in WriteLogToFile() failed.");
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }

View File

@ -65,12 +65,19 @@ typedef struct ErrorLogWork{
// メモリ確保用関数 // メモリ確保用関数
void* (*Alloc) ( u32 ) ; void* (*Alloc) ( u32 ) ;
void (*Free) ( void* ) ; void (*Free) ( void* ) ;
// エラーログエントリ保持用変数 // エラーログエントリ保持用変数
ErrorLogEntry *entry; ErrorLogEntry *entry;
// エラーログのエントリ数 // エラーログのエントリ数
int numEntry; int numEntry;
// エラーログのファイルポインタ // エラーログのファイルポインタ
FSFile file; FSFile file;
// 排他制御用Mutex
OSMutex mutex;
} ErrorLogWork; } ErrorLogWork;