Tutorial_CustomLog.cpp

Tutorial_CustomLog.cpp
/*
* Copyright 2015 Autodesk, Inc. All rights reserved.
* Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
* or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
*/
#include <common/basesystemenv.h>
//RUN_THIS_FILE
namespace
{
#define TEST_ENV_CLASS TestEnv
TEST_ENV {}
// A custom log can be passed to receive Gameware Navigation messages in your logging system.
class CustomLog : public Kaim::BaseLog
{
public:
CustomLog() : m_visualDebugServer(KY_NULL) {}
// called by Kaim::World::StartVisualDebugServer() if GetVisualDebugServer() returns KY_NULL
// and by Kaim::World::StopVisualDebugServer() if GetVisualDebugServer() == Kaim::World::m_visualdebugServer
virtual void SetVisualDebugServer(Kaim::VisualDebugServer* server) { m_visualDebugServer = server; }
virtual Kaim::VisualDebugServer* GetVisualDebugServer() { return m_visualDebugServer; }
// called when BaseSystem::Destroy() is called, this funciton must release all resources relying on BaseSytem
virtual void ReleaseMemorySystemResources() { m_visualDebugServer = KY_NULL; }
virtual void LogMessageVarg(Kaim::LogMessageId messageId, const char* fmt, va_list argList)
{
char buffer[Kaim::Log::MaxLogBufferMessageSize];
Kaim::BaseLog::FormatLog(buffer, Kaim::Log::MaxLogBufferMessageSize, messageId, fmt, argList);
// Here you do what you want, printf, output to the IDE, write a file (or a combination of these).
printf("%s", buffer);
// beware that a VisualDebugServer is tied to one Kaim::World, but the Kaim::BaseLog is used by all Worlds and other components
if (m_visualDebugServer != KY_NULL) // VisualDebug activated for logging
{
if (m_visualDebugServer->CanSafelyOperate() // thread-safe to call i.e. running in the main thread
&& m_visualDebugServer->IsConnected() // and worthwhile to call send
&& (messageId.GetChannel() != Kaim::LogChannel_MonitoringSystem)) // send everything except MonitoringSystem i.e. internal visualDebug messages due to potential deadlocks and/or recursive calls!
{
Kaim::LogBlobBuilder builder(buffer, (KyUInt32)Kaim::SFstrlen(buffer), messageId);
m_visualDebugServer->Send(builder);
}
}
}
private:
Kaim::VisualDebugServer* m_visualDebugServer;
};
TEST // Basic Initialization of Kaim::BaseSystem
{
KY_UNUSED(env);
// We need to set the custom Log.
CustomLog customLog;
config.m_log = &customLog;
// Key.
config.m_gamewareKeyConfig.m_gamewareKey = "1GAMEWARE1KEY1PROVIDED1BY1AUTODESK1";
// Instantiating the BaseSystem is a good practice. Doing this allows it to be properly destroyed if you have several exit points in your code.
Kaim::BaseSystem baseSystem(config);
if (baseSystem.IsInitialized() == false)
{
return; // BaseSystem is destroyed implicitly, and Kaim::BaseSystem::Destroy() is called.
}
for (KyUInt32 i = 0; i < 3; ++i)
KY_LOG_MESSAGE(("Log Something!")); // The custom log will be used to log rather than the default log of Navigation.
return;
// The base system is destroyed here: allocation, creation, deallocation, or destruction with Gameware Navigation objects must not occur.
// This means if your Custom log is destroyed here you MUST not have allocated it with Navigation or use any other Navigation resources within it (e.g. File IO etc).
}
}