Add a function for handling possible access log output

This commit is contained in:
Alex Barney 2019-09-29 11:54:34 -05:00
parent 838bb18a09
commit 22bbf07c2b
5 changed files with 75 additions and 74 deletions

View file

@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
using LibHac.FsService;
namespace LibHac.Fs
@ -32,7 +33,7 @@ namespace LibHac.Fs
internal bool IsEnabledAccessLog(LocalAccessLogMode mode)
{
if (!LocalAccessLogMode.HasFlag(mode))
if ((LocalAccessLogMode & mode) == 0)
{
return false;
}
@ -72,6 +73,26 @@ namespace LibHac.Fs
{
}
public Result RunOperationWithAccessLog(LocalAccessLogMode logType, Func<Result> operation, Func<string> textGenerator, [CallerMemberName] string caller = "")
{
Result rc;
if (IsEnabledAccessLog(logType))
{
TimeSpan startTime = Time.GetCurrent();
rc = operation();
TimeSpan endTime = Time.GetCurrent();
OutputAccessLog(rc, startTime, endTime, textGenerator(), caller);
}
else
{
rc = operation();
}
return rc;
}
}
[Flags]

View file

@ -91,6 +91,11 @@ namespace LibHac.Fs
{
AccessLogEnabled = isEnabled;
if (isEnabled && FsSrv != null)
{
SetGlobalAccessLogMode(GlobalAccessLogMode.All);
}
if (accessLog != null) AccessLog = accessLog;
}

View file

@ -1,5 +1,4 @@
using System;
using LibHac.Common;
using LibHac.Common;
using LibHac.FsService;
namespace LibHac.Fs
@ -24,62 +23,33 @@ namespace LibHac.Fs
return fs.Register(mountName, fileSystem);
}
public static Result CreateSystemSaveData(this FileSystemClient fs, SaveDataSpaceId spaceId, ulong saveDataId,
UserId userId, ulong ownerId, long size, long journalSize, uint flags)
public static Result CreateSystemSaveData(this FileSystemClient fs, SaveDataSpaceId spaceId,
ulong saveDataId, UserId userId, ulong ownerId, long size, long journalSize, uint flags)
{
if (fs.IsEnabledAccessLog(LocalAccessLogMode.Internal))
{
TimeSpan startTime = fs.Time.GetCurrent();
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
var attribute = new SaveDataAttribute
return fs.RunOperationWithAccessLog(LocalAccessLogMode.Internal,
() =>
{
UserId = userId,
SaveDataId = saveDataId
};
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
var createInfo = new SaveDataCreateInfo
{
Size = size,
JournalSize = journalSize,
BlockSize = 0x4000,
OwnerId = ownerId,
Flags = flags,
SpaceId = spaceId
};
var attribute = new SaveDataAttribute
{
UserId = userId,
SaveDataId = saveDataId
};
Result rc = fsProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref createInfo);
var createInfo = new SaveDataCreateInfo
{
Size = size,
JournalSize = journalSize,
BlockSize = 0x4000,
OwnerId = ownerId,
Flags = flags,
SpaceId = spaceId
};
TimeSpan endTime = fs.Time.GetCurrent();
fs.OutputAccessLog(rc, startTime, endTime,
$", savedataspaceid: {spaceId}, savedataid: 0x{saveDataId:X}, userid: 0x{userId.Id.High:X16}{userId.Id.Low:X16}, save_data_owner_id: 0x{ownerId:X}, save_data_size: {size}, save_data_journal_size: {journalSize}, save_data_flags: 0x{flags:X8}");
return rc;
}
else
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
var attribute = new SaveDataAttribute
{
UserId = userId,
SaveDataId = saveDataId
};
var createInfo = new SaveDataCreateInfo
{
Size = size,
JournalSize = journalSize,
BlockSize = 0x4000,
OwnerId = ownerId,
Flags = flags,
SpaceId = spaceId
};
return fsProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref createInfo);
}
return fsProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref createInfo);
},
() => $", savedataspaceid: {spaceId}, savedataid: 0x{saveDataId:X}, userid: 0x{userId.Id.High:X16}{userId.Id.Low:X16}, save_data_owner_id: 0x{ownerId:X}, save_data_size: {size}, save_data_journal_size: {journalSize}, save_data_flags: 0x{flags:X8}");
}
public static Result CreateSystemSaveData(this FileSystemClient fs, ulong saveDataId, UserId userId,
@ -114,24 +84,13 @@ namespace LibHac.Fs
public static Result DeleteSaveData(this FileSystemClient fs, ulong saveDataId)
{
if (fs.IsEnabledAccessLog(LocalAccessLogMode.Internal))
{
TimeSpan startTime = fs.Time.GetCurrent();
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
Result result = fsProxy.DeleteSaveDataFileSystem(saveDataId);
TimeSpan endTime = fs.Time.GetCurrent();
fs.OutputAccessLog(result, startTime, endTime, $", savedataid: 0x{saveDataId:X}");
return result;
}
else
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.DeleteSaveDataFileSystem(saveDataId);
}
return fs.RunOperationWithAccessLog(LocalAccessLogMode.Internal,
() =>
{
IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.DeleteSaveDataFileSystem(saveDataId);
},
() => $", savedataid: 0x{saveDataId:X}");
}
public static Result DisableAutoSaveDataCreation(this FileSystemClient fsClient)

View file

@ -522,12 +522,14 @@ namespace LibHac.FsService
public Result SetGlobalAccessLogMode(GlobalAccessLogMode mode)
{
throw new NotImplementedException();
// Missing permission check
return FsProxyCore.SetGlobalAccessLogMode(mode);
}
public Result GetGlobalAccessLogMode(out GlobalAccessLogMode mode)
{
throw new NotImplementedException();
return FsProxyCore.GetGlobalAccessLogMode(out mode);
}
public Result GetProgramIndexForAccessLog(out int programIndex, out int programCount)

View file

@ -14,6 +14,8 @@ namespace LibHac.FsService
private const string NintendoDirectoryName = "Nintendo";
private const string ContentDirectoryName = "Contents";
private GlobalAccessLogMode LogMode { get; set; }
public FileSystemProxyCore(FileSystemCreators fsCreators)
{
FsCreators = fsCreators;
@ -243,6 +245,18 @@ namespace LibHac.FsService
}
}
public Result SetGlobalAccessLogMode(GlobalAccessLogMode mode)
{
LogMode = mode;
return Result.Success;
}
public Result GetGlobalAccessLogMode(out GlobalAccessLogMode mode)
{
mode = LogMode;
return Result.Success;
}
private string GetSaveDataIdPath(ulong saveDataId)
{
return $"/{saveDataId:x16}";