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;
using System.Runtime.CompilerServices;
using LibHac.FsService; using LibHac.FsService;
namespace LibHac.Fs namespace LibHac.Fs
@ -32,7 +33,7 @@ namespace LibHac.Fs
internal bool IsEnabledAccessLog(LocalAccessLogMode mode) internal bool IsEnabledAccessLog(LocalAccessLogMode mode)
{ {
if (!LocalAccessLogMode.HasFlag(mode)) if ((LocalAccessLogMode & mode) == 0)
{ {
return false; 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] [Flags]

View file

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

View file

@ -1,5 +1,4 @@
using System; using LibHac.Common;
using LibHac.Common;
using LibHac.FsService; using LibHac.FsService;
namespace LibHac.Fs namespace LibHac.Fs
@ -24,41 +23,11 @@ namespace LibHac.Fs
return fs.Register(mountName, fileSystem); return fs.Register(mountName, fileSystem);
} }
public static Result CreateSystemSaveData(this FileSystemClient fs, SaveDataSpaceId spaceId, ulong saveDataId, public static Result CreateSystemSaveData(this FileSystemClient fs, SaveDataSpaceId spaceId,
UserId userId, ulong ownerId, long size, long journalSize, uint flags) ulong saveDataId, UserId userId, ulong ownerId, long size, long journalSize, uint flags)
{ {
if (fs.IsEnabledAccessLog(LocalAccessLogMode.Internal)) return fs.RunOperationWithAccessLog(LocalAccessLogMode.Internal,
{ () =>
TimeSpan startTime = fs.Time.GetCurrent();
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
};
Result rc = fsProxy.CreateSaveDataFileSystemBySystemSaveDataId(ref attribute, ref createInfo);
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(); IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
@ -79,7 +48,8 @@ namespace LibHac.Fs
}; };
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, 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) public static Result DeleteSaveData(this FileSystemClient fs, ulong saveDataId)
{ {
if (fs.IsEnabledAccessLog(LocalAccessLogMode.Internal)) return fs.RunOperationWithAccessLog(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(); IFileSystemProxy fsProxy = fs.GetFileSystemProxyServiceObject();
return fsProxy.DeleteSaveDataFileSystem(saveDataId); return fsProxy.DeleteSaveDataFileSystem(saveDataId);
} },
() => $", savedataid: 0x{saveDataId:X}");
} }
public static Result DisableAutoSaveDataCreation(this FileSystemClient fsClient) public static Result DisableAutoSaveDataCreation(this FileSystemClient fsClient)

View file

@ -522,12 +522,14 @@ namespace LibHac.FsService
public Result SetGlobalAccessLogMode(GlobalAccessLogMode mode) public Result SetGlobalAccessLogMode(GlobalAccessLogMode mode)
{ {
throw new NotImplementedException(); // Missing permission check
return FsProxyCore.SetGlobalAccessLogMode(mode);
} }
public Result GetGlobalAccessLogMode(out GlobalAccessLogMode mode) public Result GetGlobalAccessLogMode(out GlobalAccessLogMode mode)
{ {
throw new NotImplementedException(); return FsProxyCore.GetGlobalAccessLogMode(out mode);
} }
public Result GetProgramIndexForAccessLog(out int programIndex, out int programCount) 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 NintendoDirectoryName = "Nintendo";
private const string ContentDirectoryName = "Contents"; private const string ContentDirectoryName = "Contents";
private GlobalAccessLogMode LogMode { get; set; }
public FileSystemProxyCore(FileSystemCreators fsCreators) public FileSystemProxyCore(FileSystemCreators fsCreators)
{ {
FsCreators = 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) private string GetSaveDataIdPath(ulong saveDataId)
{ {
return $"/{saveDataId:x16}"; return $"/{saveDataId:x16}";