From 3b983bca8edc7ab5833333fb5afb47d5e9943ecc Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Fri, 2 Jan 2015 20:20:20 -0500 Subject: [PATCH] Convert vault tree printing to plString After hacking the old to just barely use plString, I received some UTF-8 asserts. So, that seemed like a good excuse to convert the whole mess to plString. Note that the output is now saved in VaultClient.log. This should help with getting some of the chaos out of OLD_ASYNC_LOG.log --- .../pfConsole/pfConsoleCommands.cpp | 4 +- Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp | 2 +- Sources/Plasma/NucleusLib/pnUtils/pnUtMisc.h | 14 -- .../plNetClient/plNetCliAgeJoiner.cpp | 2 +- .../plNetClientComm/plNetClientComm.cpp | 4 +- Sources/Plasma/PubUtilLib/plVault/Pch.h | 1 + .../PubUtilLib/plVault/plVaultClientApi.cpp | 231 ++++++------------ .../PubUtilLib/plVault/plVaultClientApi.h | 11 +- 8 files changed, 86 insertions(+), 183 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommands.cpp b/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommands.cpp index 12c9828c..651491b2 100644 --- a/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommands.cpp +++ b/Sources/Plasma/FeatureLib/pfConsole/pfConsoleCommands.cpp @@ -6913,8 +6913,8 @@ PF_CONSOLE_GROUP(Vault) PF_CONSOLE_CMD(Vault, Dump, "", "Prints the vault structure of current player and age to the nearest log file") { - VaultDump(L"Player", NetCommGetPlayer()->playerInt); - VaultDump(L"Age", NetCommGetAge()->ageVaultId); + VaultDump("Player", NetCommGetPlayer()->playerInt); + VaultDump("Age", NetCommGetAge()->ageVaultId); } #endif diff --git a/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp b/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp index 37367743..5ca79aba 100644 --- a/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/cyMisc.cpp @@ -2932,7 +2932,7 @@ void cyMisc::ForceVaultNodeUpdate(unsigned nodeId) void cyMisc::VaultDownload(unsigned nodeId) { VaultDownloadAndWait( - L"PyVaultDownload", + "PyVaultDownload", nodeId, nil, nil diff --git a/Sources/Plasma/NucleusLib/pnUtils/pnUtMisc.h b/Sources/Plasma/NucleusLib/pnUtils/pnUtMisc.h index 80c9f1b6..a416fce0 100644 --- a/Sources/Plasma/NucleusLib/pnUtils/pnUtMisc.h +++ b/Sources/Plasma/NucleusLib/pnUtils/pnUtMisc.h @@ -69,18 +69,4 @@ void * ModuleGetInstance (); const wchar_t * AppGetCommandLine (); - -/***************************************************************************** -* -* Misc types -* -***/ - -// used to dump the internal state of a module -typedef void (CDECL * FStateDump)( - void * param, - const wchar_t fmt[], - ... -); - #endif diff --git a/Sources/Plasma/PubUtilLib/plNetClient/plNetCliAgeJoiner.cpp b/Sources/Plasma/PubUtilLib/plNetClient/plNetCliAgeJoiner.cpp index 58425280..923571b8 100644 --- a/Sources/Plasma/PubUtilLib/plNetClient/plNetCliAgeJoiner.cpp +++ b/Sources/Plasma/PubUtilLib/plNetClient/plNetCliAgeJoiner.cpp @@ -408,7 +408,7 @@ bool plNCAgeJoiner::MsgReceive (plMessage * msg) { else if (unsigned ageVaultId = NetCommGetAge()->ageVaultId) { // Download the age vault VaultDownload( - L"AgeJoin", + "AgeJoin", ageVaultId, AgeVaultDownloadCallback, this, diff --git a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp index f05d76f5..d16634e7 100644 --- a/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp +++ b/Sources/Plasma/PubUtilLib/plNetClientComm/plNetClientComm.cpp @@ -311,7 +311,7 @@ static void INetCliAuthSetPlayerRequestCallback ( s_needAvatarLoad = true; VaultDownload( - L"SetActivePlayer", + "SetActivePlayer", s_player->playerInt, PlayerInitCallback, param, @@ -367,7 +367,7 @@ static void INetCliAuthLoginSetPlayerRequestCallback ( } else { VaultDownload( - L"SetActivePlayer", + "SetActivePlayer", s_player->playerInt, LoginPlayerInitCallback, param, diff --git a/Sources/Plasma/PubUtilLib/plVault/Pch.h b/Sources/Plasma/PubUtilLib/plVault/Pch.h index 9edf74ce..338d5508 100644 --- a/Sources/Plasma/PubUtilLib/plVault/Pch.h +++ b/Sources/Plasma/PubUtilLib/plVault/Pch.h @@ -80,6 +80,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plGImage/plJPEG.h" #include "plMessage/plVaultNotifyMsg.h" #include "plNetClientComm/plNetClientComm.h" +#include "plStatusLog/plStatusLog.h" #define KI_CONSTANTS_ONLY #include "pfMessage/pfKIMsg.h" // for KI level constants =( diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp index c484a501..db113cbf 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.cpp @@ -173,7 +173,7 @@ struct VaultDownloadTrans { FVaultProgressCallback progressCallback; void * cbProgressParam; - wchar_t tag[MAX_PATH]; + plString tag; unsigned nodeCount; unsigned nodesLeft; unsigned vaultId; @@ -183,18 +183,15 @@ struct VaultDownloadTrans { : callback(nil), cbParam(nil), progressCallback(nil), cbProgressParam(nil), nodeCount(0), nodesLeft(0), vaultId(0), result(kNetSuccess) { - memset(tag, 0, sizeof(tag)); } - VaultDownloadTrans (const wchar_t * _tag, FVaultDownloadCallback _callback, + VaultDownloadTrans (const plString& _tag, FVaultDownloadCallback _callback, void * _cbParam, FVaultProgressCallback _progressCallback, void * _cbProgressParam, unsigned _vaultId) : callback(_callback), cbParam(_cbParam), progressCallback(_progressCallback), cbProgressParam(_cbProgressParam), nodeCount(0), nodesLeft(0), - vaultId(_vaultId), result(kNetSuccess) + vaultId(_vaultId), result(kNetSuccess), tag(_tag) { - wcsncpy(tag, _tag, arrsize(tag)); - tag[arrsize(tag)-1] = 0; } @@ -545,7 +542,7 @@ static void VaultNodeFetched ( link->node->CopyFrom(node); InitFetchedNode(link->node); - link->node->Print(L"Fetched", LogDumpProc, 0); + link->node->Print("Fetched", 0); } //============================================================================ @@ -636,7 +633,7 @@ static void VaultNodeAdded ( continue; prevId = link->node->GetNodeId(); VaultDownload( - L"NodeAdded", + "NodeAdded", nodeIds[i], VaultNodeAddedDownloadCallback, (void*)nodeIds[i], @@ -714,7 +711,7 @@ static void SaveDirtyNodes () { if (link->node->IsDirty()) { if (unsigned bytes = NetCliAuthVaultNodeSave(link->node, nil, nil)) { bytesWritten += bytes; - link->node->Print(L"Saving", LogDumpProc, 0); + link->node->Print("Saving", 0); } } } @@ -862,7 +859,7 @@ void VaultDownloadTrans::VaultNodeFetched ( } if (!trans->nodesLeft) { - VaultDump(trans->tag, trans->vaultId, LogDumpProc); + VaultDump(trans->tag, trans->vaultId); if (trans->callback) trans->callback( @@ -1369,93 +1366,12 @@ void RelVaultNode::SetSeen (unsigned parentId, bool seen) { } //============================================================================ -template -static bool IStrSqlEscape (const T src[], T * dst, unsigned dstChars) { - // count the number of ' chars - unsigned ticks = 0; - { - const T * cur = src; - while (*cur) { - if (*cur == L'\'') - ++ticks; - cur++; - } - } - - unsigned reqChars = StrLen(src) + ticks + 1; - - if (dstChars < reqChars) - // failure! - return false; - - T * cur = dst; - - // copy src to dst, escaping ' chars - while (*src) { - if (*src == L'\'') { - *cur++ = L'\''; - *cur++ = *src++; - continue; - } - *cur++ = *src++; - } - - // null-terminate dst string - *cur = 0; - - // success! - return true; -} - -static void IGetStringFieldValue ( - const plString & value, - wchar_t * dst, - size_t dstChars -) { - wchar_t * tmp = (wchar_t*)malloc(sizeof(wchar_t) * dstChars); - IStrSqlEscape(value.ToWchar().GetData(), tmp, dstChars); - swprintf(dst, dstChars, L"'%s'", tmp); - free(tmp); -} - -static void IGetUuidFieldValue ( - const plUUID & value, - wchar_t * dst, - size_t dstChars -) { - swprintf(dst, dstChars, L"hextoraw('%S')", value.AsString().c_str()); -} - -static void IGetUintFieldValue ( - uint32_t value, - wchar_t * dst, - size_t dstChars -) { - swprintf(dst, dstChars, L"%u", value); -} - -static void IGetIntFieldValue ( - int32_t value, - wchar_t * dst, - size_t dstChars -) { - swprintf(dst, dstChars, L"%d", value); -} - -void RelVaultNode::Print (const wchar_t tag[], FStateDump dumpProc, unsigned level) { - wchar_t str[1024]; - StrPrintf( - str, - arrsize(str), - L"%s%*s%*s%u, %S", - tag ? tag : L"", - tag ? 1 : 0, - " ", - level * 2, - " ", - GetNodeId(), - plVault::NodeTypeStr(GetNodeType(), false) - ); +void RelVaultNode::Print (const plString& tag, unsigned level) { + plStringStream ss; + ss << tag; + ss << plFormat(plFormat("{{{}}", level * 2).c_str(), " "); // creates the indentation + ss << " " << GetNodeId(); + ss << " " << plVault::NodeTypeStr(GetNodeType()); for (uint64_t bit = 1; bit; bit <<= 1) { if (!(GetFieldFlags() & bit)) @@ -1463,60 +1379,66 @@ void RelVaultNode::Print (const wchar_t tag[], FStateDump dumpProc, unsigned lev if (bit > GetFieldFlags()) break; - #define STPRINT(flag, func) case k##flag: { \ - wcsncat(str, L", " L ## #flag L"=", arrsize(str)); \ - const size_t chars = wcslen(str); \ - func(Get##flag(), str + chars, arrsize(str) - chars * sizeof(str[0])); \ - }; break - #define STNAME(flag) case k##flag: { \ - wcsncat(str, L", " L ## #flag, arrsize(str)); \ - }; break +#define STPRINT(flag) \ + case k##flag: \ + ss << ", " #flag "=\"" << Get##flag() << "\""; \ + break; +#define STPRINT_ESCAPE(flag) \ + case k##flag: \ + ss << ", " #flag "=\"" << Get##flag().Replace("\"", "\\\"") << "\""; \ + break; +#define STNAME(flag) \ + case k##flag: \ + ss << ", " << #flag; \ + break; + switch (bit) { - STPRINT(NodeId, IGetUintFieldValue); - STPRINT(CreateTime, IGetUintFieldValue); - STPRINT(ModifyTime, IGetUintFieldValue); - STPRINT(CreateAgeName, IGetStringFieldValue); - STPRINT(CreateAgeUuid, IGetUuidFieldValue); - STPRINT(CreatorAcct, IGetUuidFieldValue); - STPRINT(CreatorId, IGetUintFieldValue); - STPRINT(NodeType, IGetUintFieldValue); - STPRINT(Int32_1, IGetIntFieldValue); - STPRINT(Int32_2, IGetIntFieldValue); - STPRINT(Int32_3, IGetIntFieldValue); - STPRINT(Int32_4, IGetIntFieldValue); - STPRINT(UInt32_1, IGetUintFieldValue); - STPRINT(UInt32_2, IGetUintFieldValue); - STPRINT(UInt32_3, IGetUintFieldValue); - STPRINT(UInt32_4, IGetUintFieldValue); - STPRINT(Uuid_1, IGetUuidFieldValue); - STPRINT(Uuid_2, IGetUuidFieldValue); - STPRINT(Uuid_3, IGetUuidFieldValue); - STPRINT(Uuid_4, IGetUuidFieldValue); - STPRINT(String64_1, IGetStringFieldValue); - STPRINT(String64_2, IGetStringFieldValue); - STPRINT(String64_3, IGetStringFieldValue); - STPRINT(String64_4, IGetStringFieldValue); - STPRINT(String64_5, IGetStringFieldValue); - STPRINT(String64_6, IGetStringFieldValue); - STPRINT(IString64_1, IGetStringFieldValue); - STPRINT(IString64_2, IGetStringFieldValue); + STPRINT(NodeId); + STPRINT(CreateTime); + STPRINT(ModifyTime); + STPRINT(CreateAgeName); + STPRINT(CreateAgeUuid); + STPRINT(CreatorAcct); + STPRINT(CreatorId); + STPRINT(NodeType); + STPRINT(Int32_1); + STPRINT(Int32_2); + STPRINT(Int32_3); + STPRINT(Int32_4); + STPRINT(UInt32_1); + STPRINT(UInt32_2); + STPRINT(UInt32_3); + STPRINT(UInt32_4); + STPRINT(Uuid_1); + STPRINT(Uuid_2); + STPRINT(Uuid_3); + STPRINT(Uuid_4); + STPRINT_ESCAPE(String64_1); + STPRINT_ESCAPE(String64_2); + STPRINT_ESCAPE(String64_3); + STPRINT_ESCAPE(String64_4); + STPRINT_ESCAPE(String64_5); + STPRINT_ESCAPE(String64_6); + STPRINT_ESCAPE(IString64_1); + STPRINT_ESCAPE(IString64_2); STNAME(Text_1); STNAME(Text_2); STNAME(Blob_1); STNAME(Blob_2); DEFAULT_FATAL(bit); } - #undef STPRINT +#undef STPRINT +#undef STNAME } - dumpProc(nil, str); + plStatusLog::AddLineS("VaultClient.log", ss.GetString().c_str()); } //============================================================================ -void RelVaultNode::PrintTree (FStateDump dumpProc, unsigned level) { - Print(L"", dumpProc, level); +void RelVaultNode::PrintTree (unsigned level) { + Print("", level); for (RelVaultNodeLink * link = state->children.Head(); link; link = state->children.Next(link)) - link->node->PrintTree(dumpProc, level + 1); + link->node->PrintTree(level + 1); } //============================================================================ @@ -2649,7 +2571,7 @@ bool VaultRegisterOwnedAgeAndWait (const plAgeLinkStruct * link) { memset(¶m, 0, sizeof(param)); VaultDownload( - L"RegisterOwnedAge", + "RegisterOwnedAge", ageInfoId, _FetchVaultCallback, ¶m, @@ -2831,7 +2753,7 @@ namespace _VaultRegisterOwnedAge { p->fSpawn = (plSpawnPointInfo*)param; VaultDownload( - L"RegisterOwnedAge", + "RegisterOwnedAge", ageInfoVaultId, (FVaultDownloadCallback)_DownloadCallback, p, @@ -3009,7 +2931,7 @@ bool VaultRegisterVisitAgeAndWait (const plAgeLinkStruct * link) { memset(¶m, 0, sizeof(param)); VaultDownload( - L"RegisterVisitAge", + "RegisterVisitAge", ageInfoId, _FetchVaultCallback, ¶m, @@ -3183,7 +3105,7 @@ namespace _VaultRegisterVisitAge { _Params* p = (_Params*)param; p->fAgeInfoId = (void*)ageInfoId; - VaultDownload(L"RegisterVisitAge", + VaultDownload("RegisterVisitAge", ageInfoId, (FVaultDownloadCallback)_DownloadCallback, param, @@ -3419,18 +3341,13 @@ bool VaultSetCCRStatus (bool online) { } //============================================================================ -void VaultDump (const wchar_t tag[], unsigned vaultId, FStateDump dumpProc) { - LogMsg(kLogDebug, L"<---- ID:%u, Begin Vault%*s%s ---->", vaultId, tag ? 1 : 0, L" ", tag); +void VaultDump (const plString& tag, unsigned vaultId) { + plStatusLog::AddLineS("VaultClient.log", plFormat("<---- ID:{}, Begin Vault {} ---->", vaultId, tag).c_str()); if (hsRef rvn = VaultGetNode(vaultId)) - rvn->PrintTree(dumpProc, 0); - - LogMsg(kLogDebug, L"<---- ID:%u, End Vault%*s%s ---->", vaultId, tag ? 1 : 0, L" ", tag); -} + rvn->PrintTree(0); -//============================================================================ -void VaultDump (const wchar_t tag[], unsigned vaultId) { - VaultDump (tag, vaultId, LogDumpProc); + plStatusLog::AddLineS("VaultClient.log", plFormat("<---- ID:{}, End Vault {} ---->", vaultId, tag).c_str()); } //============================================================================ @@ -4122,7 +4039,7 @@ bool VaultAgeFindOrCreateSubAgeLinkAndWait ( memset(¶m, 0, sizeof(param)); VaultDownload( - L"CreateSubAge", + "CreateSubAge", ageInfoId, _FetchVaultCallback, ¶m, @@ -4239,7 +4156,7 @@ namespace _VaultCreateSubAge { } // Download age vault - VaultDownload(L"CreateSubAge", + VaultDownload("CreateSubAge", ageInfoId, (FVaultDownloadCallback)_DownloadCallback, (void*)ageInfoId, @@ -4462,7 +4379,7 @@ bool VaultAgeFindOrCreateChildAgeLinkAndWait ( memset(¶m, 0, sizeof(param)); VaultDownload( - L"CreateChildAge", + "CreateChildAge", ageInfoId, _FetchVaultCallback, ¶m, @@ -4591,7 +4508,7 @@ namespace _VaultCreateChildAge { p->fAgeInfoId = (void*)ageInfoId; // Download age vault - VaultDownload(L"CreateChildAge", + VaultDownload("CreateChildAge", ageInfoId, (FVaultDownloadCallback)_DownloadCallback, param, @@ -4683,7 +4600,7 @@ void VaultCCRDumpPlayers() { //============================================================================ void VaultDownload ( - const wchar_t tag[], + const plString& tag, unsigned vaultId, FVaultDownloadCallback callback, void * cbParam, @@ -4715,7 +4632,7 @@ static void _DownloadVaultCallback ( } void VaultDownloadAndWait ( - const wchar_t tag[], + const plString& tag, unsigned vaultId, FVaultProgressCallback progressCallback, void * cbProgressParam diff --git a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h index 5ff236ec..a76991ed 100644 --- a/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h +++ b/Sources/Plasma/PubUtilLib/plVault/plVaultClientApi.h @@ -181,8 +181,8 @@ struct RelVaultNode : NetVaultNode { void SetSeen (unsigned parentId, bool seen); // logging - void Print (const wchar_t tag[], FStateDump dumpProc, unsigned level); - void PrintTree (FStateDump dumpProc, unsigned level); + void Print (const plString& tag, unsigned level); + void PrintTree (unsigned level); // AgeInfoNode-specific (and it checks!) hsRef GetParentAgeLink (); @@ -366,8 +366,7 @@ bool VaultAmIgnoringPlayer (unsigned playerId); unsigned VaultGetKILevel (); bool VaultGetCCRStatus (); // true=online, false=away bool VaultSetCCRStatus (bool online); // true=online, false=away -void VaultDump (const wchar_t tag[], unsigned vaultId, FStateDump dumpProc); -void VaultDump (const wchar_t tag[], unsigned vaultId); +void VaultDump (const plString& tag, unsigned vaultId); bool VaultAmInMyPersonalAge (); bool VaultAmInMyNeighborhoodAge (); @@ -470,7 +469,7 @@ typedef void (*FVaultProgressCallback)( ); void VaultDownload ( - const wchar_t tag[], + const plString& tag, unsigned vaultId, FVaultDownloadCallback callback, void * cbParam, @@ -478,7 +477,7 @@ void VaultDownload ( void * cbProgressParam ); void VaultDownloadAndWait ( - const wchar_t tag[], + const plString& tag, unsigned vaultId, FVaultProgressCallback progressCallback, void * cbProgressParam