Skip to content

Commit e6034f0

Browse files
committed
chore: 添加日志
1 parent a5c05da commit e6034f0

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

‎Magpie/App.xaml.cs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
1-
using Magpie.Properties;
1+
// Copyright (c) 2021 - present, Liu Xu
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
17+
using Magpie.Properties;
218
using NLog;
319
using NLog.Config;
420
using System;

‎Runtime/EffectDrawer.cpp‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ bool EffectDrawer::Build(ComPtr<ID3D11Texture2D> input, ComPtr<ID3D11Texture2D>
316316

317317
SIZE outputSize;
318318
if (!CalcOutputSize(inputSize, outputSize)) {
319+
SPDLOG_LOGGER_ERROR(logger, "CalcOutputSize 失败");
319320
return false;
320321
}
321322

@@ -330,6 +331,7 @@ bool EffectDrawer::Build(ComPtr<ID3D11Texture2D> input, ComPtr<ID3D11Texture2D>
330331
// 从文件加载纹理
331332
_textures[i] = TextureLoader::Load((L"effects\\" + StrUtils::UTF8ToUTF16(_effectDesc.textures[i].source)).c_str());
332333
if (!_textures[i]) {
334+
SPDLOG_LOGGER_ERROR(logger, fmt::format("加载纹理 {} 失败", _effectDesc.textures[i].source));
333335
return false;
334336
}
335337
} else {
@@ -339,11 +341,13 @@ bool EffectDrawer::Build(ComPtr<ID3D11Texture2D> input, ComPtr<ID3D11Texture2D>
339341
texSize.cx = std::lround(exprParser.Eval());
340342
exprParser.SetExpr(_effectDesc.textures[i].sizeExpr.second);
341343
texSize.cy = std::lround(exprParser.Eval());
342-
} catch (...) {
344+
} catch (const mu::ParserError& e) {
345+
SPDLOG_LOGGER_ERROR(logger, fmt::format("计算中间纹理尺寸失败:{}", e.GetMsg()));
343346
return false;
344347
}
345348

346349
if (texSize.cx <= 0 || texSize.cy <= 0) {
350+
SPDLOG_LOGGER_ERROR(logger, "非法的中间纹理尺寸");
347351
return false;
348352
}
349353

‎Runtime/FrameSourceBase.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,17 @@ bool FrameSourceBase::_GetMapToOriginDPI(HWND hWnd, double& a, double& bx, doubl
2929
ReleaseDC(hWnd, hdcClient);
3030
});
3131

32-
HBITMAP hBmpWindow = (HBITMAP)GetCurrentObject(hdcWindow, OBJ_BITMAP);
32+
HGDIOBJ hBmpWindow = GetCurrentObject(hdcWindow, OBJ_BITMAP);
3333
if (!hBmpWindow) {
3434
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("GetCurrentObject 失败"));
3535
return false;
3636
}
3737

38+
if (GetObjectType(hBmpWindow) != OBJ_BITMAP) {
39+
SPDLOG_LOGGER_ERROR(logger, "无法获取窗口的重定向表面");
40+
return false;
41+
}
42+
3843
BITMAP bmp{};
3944
if (!GetObject(hBmpWindow, sizeof(bmp), &bmp)) {
4045
SPDLOG_LOGGER_ERROR(logger, MakeWin32ErrorMsg("GetObject 失败"));

‎Runtime/Renderer.cpp‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,12 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
741741
rapidjson::Document doc;
742742
if (doc.Parse(effectsJson.c_str(), effectsJson.size()).HasParseError()) {
743743
// 解析 json 失败
744+
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败\n\t错误码:{}", doc.GetParseError()));
744745
return false;
745746
}
746747

747748
if (!doc.IsArray()) {
749+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:根元素不为数组");
748750
return false;
749751
}
750752

@@ -757,23 +759,26 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
757759

758760
// 不得为空
759761
if (effectsArr.Empty()) {
762+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:根元素为空");
760763
return false;
761764
}
762765

763766
for (const auto& effectJson : effectsArr) {
764767
if (!effectJson.IsObject()) {
768+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:根数组中存在非法成员");
765769
return false;
766770
}
767771

768772
EffectDrawer& effect = _effects.emplace_back();
769773

770774
auto effectName = effectJson.FindMember("effect");
771775
if (effectName == effectJson.MemberEnd() || !effectName->value.IsString()) {
772-
// 未找到 effect 属性或该属性不合法
776+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:未找到 effect 属性或该属性的值不合法");
773777
return false;
774778
}
775779

776780
if (!effect.Initialize((L"effects\\" + StrUtils::UTF8ToUTF16(effectName->value.GetString()) + L".hlsl").c_str())) {
781+
SPDLOG_LOGGER_ERROR(logger, fmt::format("初始化效果 {} 失败", effectName->value.GetString()));
777782
return false;
778783
}
779784

@@ -782,6 +787,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
782787
auto scaleProp = effectJson.FindMember("scale");
783788
if (scaleProp != effectJson.MemberEnd()) {
784789
if (!scaleProp->value.IsArray()) {
790+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
785791
return false;
786792
}
787793

@@ -792,6 +798,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
792798

793799
const auto& scale = scaleProp->value.GetArray();
794800
if (scale.Size() != 2 || !scale[0].IsNumber() || !scale[1].IsNumber()) {
801+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
795802
return false;
796803
}
797804

@@ -804,18 +811,21 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
804811

805812
if (scaleX >= DELTA) {
806813
if (scaleY < DELTA) {
814+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
807815
return false;
808816
}
809817

810818
outputSize = { std::lroundf(outputSize.cx * scaleX), std::lroundf(outputSize.cy * scaleY) };
811819
} else if (std::abs(scaleX) < DELTA) {
812820
if (std::abs(scaleY) >= DELTA) {
821+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
813822
return false;
814823
}
815824

816825
outputSize = hostSize;
817826
} else {
818827
if (scaleY > -DELTA) {
828+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的 scale 属性");
819829
return false;
820830
}
821831

@@ -832,6 +842,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
832842

833843
for (const auto& prop : effectJson.GetObject()) {
834844
if (!prop.name.IsString()) {
845+
SPDLOG_LOGGER_ERROR(logger, "解析 json 失败:非法的效果名");
835846
return false;
836847
}
837848

@@ -843,10 +854,12 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
843854
auto type = effect.GetConstantType(name);
844855
if (type == EffectDrawer::ConstantType::Float) {
845856
if (!prop.value.IsNumber()) {
857+
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的类型非法", name));
846858
return false;
847859
}
848860

849861
if (!effect.SetConstant(name, prop.value.GetFloat())) {
862+
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的值非法", name));
850863
return false;
851864
}
852865
} else if (type == EffectDrawer::ConstantType::Int) {
@@ -857,26 +870,31 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
857870
// bool 值视为 int
858871
value = (int)prop.value.GetBool();
859872
} else {
873+
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的类型非法", name));
860874
return false;
861875
}
862876

863877
if (!effect.SetConstant(name, value)) {
878+
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:成员 {} 的值非法", name));
864879
return false;
865880
}
866881
} else {
882+
SPDLOG_LOGGER_ERROR(logger, fmt::format("解析 json 失败:非法成员 {}", name));
867883
return false;
868884
}
869885
}
870886
}
871887

872888
SIZE& outputSize = texSizes.emplace_back();
873889
if (!effect.CalcOutputSize(texSizes[texSizes.size() - 2], outputSize)) {
890+
SPDLOG_LOGGER_ERROR(logger, "CalcOutputSize 失败");
874891
return false;
875892
}
876893
}
877894

878895
if (_effects.size() == 1) {
879896
if (!_effects.back().Build(_effectInput, _backBuffer)) {
897+
SPDLOG_LOGGER_ERROR(logger, "构建效果失败");
880898
return false;
881899
}
882900
} else {
@@ -901,10 +919,12 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
901919
ComPtr<ID3D11Texture2D> outputTex;
902920
HRESULT hr = _d3dDevice->CreateTexture2D(&desc, nullptr, &outputTex);
903921
if (FAILED(hr)) {
922+
SPDLOG_LOGGER_ERROR(logger, MakeComErrorMsg("CreateTexture2D 失败", hr));
904923
return false;
905924
}
906925

907926
if (!_effects[i].Build(curTex, outputTex)) {
927+
SPDLOG_LOGGER_ERROR(logger, "构建效果失败");
908928
return false;
909929
}
910930

@@ -913,6 +933,7 @@ bool Renderer::_ResolveEffectsJson(const std::string& effectsJson, RECT& destRec
913933

914934
// 最后一个效果输出到后缓冲纹理
915935
if (!_effects.back().Build(curTex, _backBuffer)) {
936+
SPDLOG_LOGGER_ERROR(logger, "构建效果失败");
916937
return false;
917938
}
918939
}

0 commit comments

Comments
 (0)