BVB Source Codes

CRYENGINE Show ExecutionStackFileLogger.cpp Source code

Return Download CRYENGINE: download ExecutionStackFileLogger.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #include "StdAfx.h"
  4. #include "ExecutionStackFileLogger.h"
  5. #include <CryAISystem/BehaviorTree/Node.h>
  6.  
  7. #ifdef USING_BEHAVIOR_TREE_EXECUTION_STACKS_FILE_LOG
  8. namespace BehaviorTree
  9. {
  10.  
  11. static bool IsCharAllowedInFileNames(const char ch)
  12. {
  13.         if (ch >= 'a' && ch <= 'z')
  14.                 return true;
  15.  
  16.         if (ch >= 'A' && ch <= 'Z')
  17.                 return true;
  18.  
  19.         if (ch >= '0' && ch <= '9')
  20.                 return true;
  21.  
  22.         if (ch == '_' || ch == '-' || ch == '.')
  23.                 return true;
  24.  
  25.         return false;
  26. }
  27.  
  28. static void SanitizeFileName(stack_string& fileName)
  29. {
  30.         for (stack_string::iterator it = fileName.begin(); it != fileName.end(); ++it)
  31.         {
  32.                 if (!IsCharAllowedInFileNames(*it))
  33.                         *it = '_';
  34.         }
  35. }
  36.  
  37. ExecutionStackFileLogger::ExecutionStackFileLogger(const EntityId entityId)
  38. {
  39.         if (const IEntity* pEntity = gEnv->pEntitySystem->GetEntity(entityId))
  40.         {
  41.                 m_agentName = pEntity->GetName();
  42.         }
  43.  
  44.         stack_string fileName;
  45.         fileName.Format("MBT_%s_%i.txt", m_agentName.c_str(), entityId);
  46.         SanitizeFileName(fileName);
  47.  
  48.         stack_string filePath("%USER%/MBT_Logs/");
  49.         filePath.append(fileName);
  50.  
  51.         m_logFilePath[0] = '\0';
  52.         if (gEnv->pCryPak->AdjustFileName(filePath.c_str(), m_logFilePath, ICryPak::FLAGS_FOR_WRITING))
  53.         {
  54.                 m_openState = NotYetAttemptedToOpenForWriteAccess;
  55.         }
  56.         else
  57.         {
  58.                 m_openState = CouldNotAdjustFileName;
  59.         }
  60. }
  61.  
  62. ExecutionStackFileLogger::~ExecutionStackFileLogger()
  63. {
  64.         m_logFile.Close();
  65. }
  66.  
  67. void ExecutionStackFileLogger::LogDebugTree(const DebugTree& debugTree, const UpdateContext& updateContext, const BehaviorTreeInstance& instance)
  68. {
  69.         if (m_openState == NotYetAttemptedToOpenForWriteAccess)
  70.         {
  71.                 m_openState = m_logFile.Open(m_logFilePath, "w") ? OpenForWriteAccessSucceeded : OpenForWriteAccessFailed;
  72.  
  73.                 // successfully opened? -> write header
  74.                 if (m_openState == OpenForWriteAccessSucceeded)
  75.                 {
  76.                         stack_string header;
  77.                         char startTime[1024] = "???";
  78.                         time_t ltime;
  79.                         if (time(&ltime) != (time_t)-1)
  80.                         {
  81.                                 if (struct tm* pTm = localtime(&ltime))
  82.                                 {
  83.                                         strftime(startTime, CRY_ARRAY_COUNT(startTime), "%Y-%m-%d %H:%M:%S", pTm);
  84.                                 }
  85.                         }
  86.                         header.Format("Modular Behavior Tree '%s' for agent '%s' with EntityId = %i (logging started at %s)\n\n", instance.behaviorTreeTemplate->mbtFilename.c_str(), m_agentName.c_str(), updateContext.entityId, startTime);
  87.                         m_logFile.Write(header.c_str(), header.length() + 1);
  88.                         m_logFile.Flush();
  89.                 }
  90.         }
  91.  
  92.         if (m_openState == OpenForWriteAccessSucceeded)
  93.         {
  94.                 if (const DebugNode* pRoot = debugTree.GetFirstNode().get())
  95.                 {
  96.                         LogNodeRecursively(*pRoot, updateContext, instance, 0);
  97.                         stack_string textLine("-----------------------------------------\n");
  98.                         m_logFile.Write(textLine.c_str(), textLine.length() + 1);
  99.                 }
  100.         }
  101. }
  102.  
  103. void ExecutionStackFileLogger::LogNodeRecursively(const DebugNode& debugNode, const UpdateContext& updateContext, const BehaviorTreeInstance& instance, const int indentLevel)
  104. {
  105.         const Node* pNode = static_cast<const Node*>(debugNode.node);
  106.         const uint32 lineNum = pNode->GetXmlLine();
  107.         const char* nodeType = pNode->GetCreator()->GetTypeName();
  108.         stack_string customText;
  109.         const RuntimeDataID runtimeDataID = MakeRuntimeDataID(updateContext.entityId, pNode->GetNodeID());
  110.         UpdateContext updateContextWithValidRuntimeData = updateContext;
  111.         updateContextWithValidRuntimeData.runtimeData = pNode->GetCreator()->GetRuntimeData(runtimeDataID);
  112.         pNode->GetCustomDebugText(updateContextWithValidRuntimeData, customText);
  113.         if (!customText.empty())
  114.                 customText.insert(0, " - ");
  115.  
  116.         stack_string textLine;
  117.         textLine.Format("%5i %*s %s%s\n", lineNum, indentLevel * 2, "", nodeType, customText.c_str());
  118.         m_logFile.Write(textLine.c_str(), textLine.length() + 1);
  119.  
  120.         for (DebugNode::Children::const_iterator it = debugNode.children.begin(), end = debugNode.children.end(); it != end; ++it)
  121.         {
  122.                 LogNodeRecursively(*(*it), updateContext, instance, indentLevel + 1);
  123.         }
  124. }
  125.  
  126. }
  127. #endif  // USING_BEHAVIOR_TREE_EXECUTION_STACKS_FILE_LOG
  128.  
downloadExecutionStackFileLogger.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top