Tutorial_CustomLog.cpp

Tutorial_CustomLog.cpp
/*
* Copyright 2016 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"
namespace
{
#define KT_TEST_ENV_CLASS NavTest::Env
KT_TEST_ENV {}
// A custom log can be passed to receive Autodesk Navigation messages in your logging system.
class CustomLog : public Kaim::BaseLog
{
public:
CustomLog() : m_visualDebugServer(nullptr) {}
// called by Kaim::World::StartVisualDebugServer() if GetVisualDebugServer() returns nullptr
// 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 = nullptr; }
virtual void LogMessageVarg(Kaim::LogMessageId messageId, const char* fmt, va_list argList)
{
if (NavTest::Log::Instance().IsEnabled() == false)
return;
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 != nullptr) // VisualDebug activated for logging
m_visualDebugServer->SendLog(messageId, buffer);
}
private:
Kaim::VisualDebugServer* m_visualDebugServer;
};
KT_TEST // Basic Initialization of Kaim::BaseSystem
{
// We need to set the custom Log.
CustomLog customLog;
config.m_log = &customLog;
// 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 Autodesk 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).
}
}