Skip to content

Commit

Permalink
More std::string_view
Browse files Browse the repository at this point in the history
use std::format for logging
  • Loading branch information
CrendKing committed Jun 25, 2021
1 parent c83a993 commit 2b8c7ce
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion avisynth_filter/src/frameserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ FrameServerCommon::FrameServerCommon() {
IScriptEnvironment *env = CreateEnv();
AVS_linkage = env->GetAVSLinkage();
_versionString = env->Invoke("Eval", AVSValue("VersionString()")).AsString();
Environment::GetInstance().Log(L"AviSynth version: %S", GetVersionString());
Environment::GetInstance().Log(L"AviSynth version: %S", GetVersionString().data());
env->DeleteScriptEnvironment();

_sourceClip = new SourceClip(_sourceVideoInfo);
Expand Down
2 changes: 1 addition & 1 deletion avisynth_filter/src/frameserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class FrameServerCommon : public RefCountedSingleton<FrameServerCommon> {

auto SetScriptPath(const std::filesystem::path &scriptPath) -> void;
auto LinkFrameHandler(FrameHandler *frameHandler) const -> void;
constexpr auto GetVersionString() const -> const char * { return _versionString == nullptr ? "unknown AviSynth version" : _versionString; }
constexpr auto GetVersionString() const -> std::string_view { return _versionString == nullptr ? "unknown AviSynth version" : _versionString; }
constexpr auto GetScriptPath() const -> const std::filesystem::path & { return _scriptPath; }

private:
Expand Down
14 changes: 6 additions & 8 deletions filter_common/src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,15 @@ auto Environment::Log(const WCHAR *format, ...) -> void {
return;
}

const std::unique_lock logLock(_logMutex);
const std::wstring fmt = std::format(L"T {:6} @ {:8}: {}\n", GetCurrentThreadId(), timeGetTime() - _logStartTime, format);

fwprintf_s(_logFile, L"T %6lu @ %8lu: ", GetCurrentThreadId(), timeGetTime() - _logStartTime);
const std::unique_lock logLock(_logMutex);

va_list args;
va_start(args, format);
vfwprintf_s(_logFile, format, args);
vfwprintf_s(_logFile, fmt.c_str(), args);
va_end(args);

fputwc(L'\n', _logFile);

fflush(_logFile);
}

Expand All @@ -98,11 +96,11 @@ auto Environment::SetRemoteControlEnabled(bool enabled) -> void {
}
}

auto Environment::IsInputFormatEnabled(const WCHAR *formatName) const -> bool {
auto Environment::IsInputFormatEnabled(std::wstring_view formatName) const -> bool {
return _enabledInputFormats.contains(formatName);
}

auto Environment::SetInputFormatEnabled(const WCHAR *formatName, bool enabled) -> void {
auto Environment::SetInputFormatEnabled(std::wstring_view formatName, bool enabled) -> void {
if (enabled) {
_enabledInputFormats.emplace(formatName);
} else {
Expand Down Expand Up @@ -153,7 +151,7 @@ auto Environment::SaveSettingsToRegistry() const -> void {
static_cast<void>(_registry.WriteString(SETTING_NAME_SCRIPT_FILE, _scriptPath.c_str()));
static_cast<void>(_registry.WriteNumber(SETTING_NAME_REMOTE_CONTROL, _isRemoteControlEnabled));

std::ranges::for_each(Format::PIXEL_FORMATS, [this](const WCHAR *name) {
std::ranges::for_each(Format::PIXEL_FORMATS, [this](std::wstring_view name) {
const std::wstring settingName = std::format(L"{}{}", SETTING_NAME_INPUT_FORMAT_PREFIX, name);
static_cast<void>(_registry.WriteNumber(settingName.c_str(), IsInputFormatEnabled(name)));
}, &Format::PixelFormat::name);
Expand Down
6 changes: 3 additions & 3 deletions filter_common/src/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Environment : public RefCountedSingleton<Environment> {

constexpr auto GetScriptPath() const -> const std::filesystem::path & { return _scriptPath; }
auto SetScriptPath(const std::filesystem::path &scriptPath) -> void;
auto IsInputFormatEnabled(const WCHAR *formatName) const -> bool;
auto SetInputFormatEnabled(const WCHAR *formatName, bool enabled) -> void;
auto IsInputFormatEnabled(std::wstring_view formatName) const -> bool;
auto SetInputFormatEnabled(std::wstring_view formatName, bool enabled) -> void;
constexpr auto IsRemoteControlEnabled() const -> bool { return _isRemoteControlEnabled; }
auto SetRemoteControlEnabled(bool enabled) -> void;
constexpr auto GetExtraSourceBuffer() const -> int { return _extraSourceBuffer; }
Expand All @@ -42,7 +42,7 @@ class Environment : public RefCountedSingleton<Environment> {
Registry _registry;

std::filesystem::path _scriptPath;
std::unordered_set<const WCHAR *> _enabledInputFormats;
std::unordered_set<std::wstring_view> _enabledInputFormats;
bool _isRemoteControlEnabled = false;
int _extraSourceBuffer;

Expand Down
6 changes: 1 addition & 5 deletions vapoursynth_filter/src/frameserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FrameServerCommon::FrameServerCommon() {
}

_versionString = std::format("VapourSynth R{} API R{}.{}", coreInfo.core, coreInfo.api >> 16, coreInfo.api & 0xffff);
Environment::GetInstance().Log(L"VapourSynth version: %S", GetVersionString());
Environment::GetInstance().Log(L"VapourSynth version: %S", GetVersionString().data());

_vsApi->freeCore(vsCore);
}
Expand All @@ -55,10 +55,6 @@ FrameServerCommon::~FrameServerCommon() {
vsscript_finalize();
}

auto FrameServerCommon::GetVersionString() const -> const char * {
return _versionString.c_str();
}

auto FrameServerCommon::LinkFrameHandler(FrameHandler *frameHandler) -> void {
_frameHandler = frameHandler;
}
Expand Down
2 changes: 1 addition & 1 deletion vapoursynth_filter/src/frameserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FrameServerCommon : public RefCountedSingleton<FrameServerCommon> {
DISABLE_COPYING(FrameServerCommon)

auto SetScriptPath(const std::filesystem::path &scriptPath) -> void;
auto GetVersionString() const -> const char *;
auto LinkFrameHandler(FrameHandler *frameHandler) -> void;
constexpr auto GetVersionString() const -> std::string_view { return _versionString; }
constexpr auto GetScriptPath() const -> const std::filesystem::path & { return _scriptPath; }
constexpr auto GetFrameHandler() const -> FrameHandler & { return *_frameHandler; }
constexpr auto GetSourceVideoInfo() const -> const VSVideoInfo & { return _sourceVideoInfo; }
Expand Down

0 comments on commit 2b8c7ce

Please sign in to comment.