Files
o3de/Code/Legacy/CrySystem/AZCoreLogSink.h
T
Chris Burel b53bf52e0d Perform global deinitialization when exiting the game launcher (#4131)
* Fix code that deregisters the Atom Scene subsystem from the AzFramework Scene

The AzFramework Scene subsystem API is a generic container based on the
type of argument that is passed to it. It maintains a vector of typeids,
and only one object of any type is stored at a time. The Bootstrap system
component registers the Atom scene as a `ScenePtr` (aka
`AZStd::shared_ptr<RPI:Scene>`) with the AzFramework Scene's generic
subsystem. However, the component was previously deregistering the type by
value, `RPI::Scene`. Since no subsystem for the type `RPI::Scene` was set,
unsetting this type did nothing. The result was that the `RPI::Scene`
object would still be around by the time that all the Atom
`InstanceDatabse`s were being destroyed, resulting in a large number of
errors reported about leaked instances during global shutdown.

This fixes the above issue by passing the `m_defaultScene` as a parameter
to `AzFramework::Scene::UnsetSubsystem`, the same value that is passed to
`SetSubsystem`. This is better, because instead of providing explicit
template arguments (which were specifying the incorrect type), this now
allows the compiler to deduce the correct type, and the syntax is symmetric
with the call to `SetSubsystem`.

Signed-off-by: Chris Burel <burelc@amazon.com>

* Correctly release the AWS API from the `HttpRequestManager` module

This code was incorrectly assuming that
`AWSNativeSDKInit::InitializationManager::Shutdown()` would be called
automatically by the `InitializationManager` itself. However, all that
`InitAwsApi()` does is create an `AZ::EnvironmentVariable`, which is a
ref-counted type, and stores it in a global static. That global static is
defined in a static library (namely `AWSNativeSDKInit`), which is linked
in to the `HttpRequestManager` dynamic lib. Because it is a global static,
it has to be explicitly cleared with the call to `Shutdown()`. Otherwise
the destructor of the EnvironmentVariable doesn't happen until global
destruction, by which time the allocator that is supplied to the AWS SDK
has already been destroyed, and the shutdown of the AWS SDK attempts to use
the already-destroyed allocator.

Signed-off-by: Chris Burel <burelc@amazon.com>

* Avoid blocking the remote console server thread if there are no connections

The Remote console server runs in a separate thread. Previously, it would
directly call `AzSock::Accept()` and block the server thread until some
client connected to it. However, if no client connected, the thread would
continue to be blocked, even if the game launcher tried to exit.

This adds a check to see if there's a client on the socket before calling
`Accept()`, to avoid the deadlock on launcher exit.

Signed-off-by: Chris Burel <burelc@amazon.com>

* Fix a log message to print one message per line

Signed-off-by: Chris Burel <burelc@amazon.com>

* Allow pumping the event loop to close the launcher window

Events from the OS are handled in the game's main loop. The general loop
looks like this:

 * Read events from the OS
 * Tick the game application

One of the events that can come from the OS is that the window hosting the
game is closed. When this event happens, many resources provided by the
renderer are freed, and the game application's `shouldExit` bit is set.
However, when the game's `Tick()` is called, there is lots of code that
assumes the renderer is still there. To avoid crashing in the `Tick()`
call, check if the game should exit after pumping the system events.

Signed-off-by: Chris Burel <burelc@amazon.com>

* Unload the level when exiting the launcher

This ensures that any resources held onto by the level are freed before the
launcher exits.

Signed-off-by: Chris Burel <burelc@amazon.com>

* Add an explicit bus `Disconnect()` call to `AZCoreLogSink`

This is necessary because this bus has virtual functions and can be called
from multiple threads.

Signed-off-by: Chris Burel <burelc@amazon.com>

* Allow normal cleanup to take place when exiting the game launcher

Previously, global cleanup was side-stepped by calling `TerminateProcess`
or `exit`, when quitting the game launcher. This is in contrast to the call
to `_exit` on Linux and Mac when exiting the Editor. That leading `_` makes
a big difference: the former runs object destruction, the latter does not.
Instead of making the launcher exit with `_exit` on Linux, instead, remove
that call and actually run all the atexit code.

This does not modify the Editor's behavior however. It still uses `_exit`
and `TerminateProcess`.

Signed-off-by: Chris Burel <burelc@amazon.com>
2021-09-15 11:48:08 -05:00

190 lines
5.8 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include <AzCore/Memory/SystemAllocator.h>
#include <AzCore/Math/Crc.h>
#include <AzCore/Debug/TraceMessageBus.h>
#include <AzCore/std/containers/unordered_map.h>
#include <AzCore/Console/IConsole.h>
#include <CryAssert.h>
namespace AZ
{
AZ_CVAR_EXTERNED(int, bg_traceLogLevel);
}
/**
* Hook Trace bus so we can funnel AZ asserts, warnings, etc to CryEngine.
*
* Note: This is currently owned by CrySystem, because CrySystem owns
* the logging mechanism for which it is relevant.
*/
class AZCoreLogSink
: public AZ::Debug::TraceMessageBus::Handler
{
public:
~AZCoreLogSink()
{
Disconnect();
}
inline static void Connect()
{
GetInstance().m_ignoredAsserts = new IgnoredAssertMap();
GetInstance().BusConnect();
}
inline static void Disconnect()
{
GetInstance().BusDisconnect();
delete GetInstance().m_ignoredAsserts;
}
static AZCoreLogSink& GetInstance()
{
static AZCoreLogSink s_sink;
return s_sink;
}
static bool IsCryLogReady()
{
bool ready = gEnv && gEnv->pSystem && gEnv->pLog;
#ifdef _RELEASE
static bool hasSetCVar = false;
if(!hasSetCVar && ready)
{
// AZ logging only has a concept of 3 levels (error, warning, info) but cry logging has 4 levels (..., messaging). If info level is set, we'll turn on messaging as well
int logLevel = AZ::bg_traceLogLevel == AZ::Debug::LogLevel::Info ? 4 : AZ::bg_traceLogLevel;
gEnv->pConsole->GetCVar("log_WriteToFileVerbosity")->Set(logLevel);
hasSetCVar = true;
}
#endif
return ready;
}
bool OnPreAssert(const char* fileName, int line, const char* func, const char* message) override
{
#if defined(USE_CRY_ASSERT) && AZ_LEGACY_CRYSYSTEM_TRAIT_DO_PREASSERT
AZ::Crc32 crc;
crc.Add(&line, sizeof(line));
if (fileName)
{
crc.Add(fileName, strlen(fileName));
}
bool* ignore = nullptr;
auto foundIter = m_ignoredAsserts->find(crc);
if (foundIter == m_ignoredAsserts->end())
{
ignore = &((*m_ignoredAsserts)[crc]);
*ignore = false;
}
else
{
ignore = &((*m_ignoredAsserts)[crc]);
}
if (!(*ignore))
{
using namespace AZ::Debug;
Trace::Output(nullptr, "\n==================================================================\n");
AZ::OSString outputMsg = AZ::OSString::format("Trace::Assert\n %s(%d): '%s'\n%s\n", fileName, line, func, message);
Trace::Output(nullptr, outputMsg.c_str());
// Suppress 3 in stack depth - this function, the bus broadcast that got us here, and Trace::Assert
Trace::Output(nullptr, "------------------------------------------------\n");
Trace::PrintCallstack(nullptr, 3);
Trace::Output(nullptr, "\n==================================================================\n");
AZ::EnvironmentVariable<bool> inEditorBatchMode = AZ::Environment::FindVariable<bool>("InEditorBatchMode");
if (!inEditorBatchMode.IsConstructed() || !inEditorBatchMode.Get())
{
// Note - CryAssertTrace doesn't actually print any info to logging
// it just stores the message internally for the message box in CryAssert to use
CryAssertTrace("%s", message);
if (CryAssert("Assertion failed", fileName, line, ignore) || Trace::IsDebuggerPresent())
{
Trace::Break();
}
}
}
else
{
CryLogAlways("%s", message);
}
return true; // suppress default AzCore behavior.
#else
AZ_UNUSED(fileName);
AZ_UNUSED(line);
AZ_UNUSED(func);
AZ_UNUSED(message);
return false; // allow AZCore to do its default behavior. This usually results in an application shutdown.
#endif
}
bool OnPreError(const char* window, const char* fileName, int line, const char* func, const char* message) override
{
AZ_UNUSED(fileName);
AZ_UNUSED(line);
AZ_UNUSED(func);
if (!IsCryLogReady())
{
return false; // allow AZCore to do its default behavior.
}
gEnv->pLog->LogError("(%s) - %s", window, message);
return true; // suppress default AzCore behavior.
}
bool OnPreWarning(const char* window, const char* fileName, int line, const char* func, const char* message) override
{
AZ_UNUSED(fileName);
AZ_UNUSED(line);
AZ_UNUSED(func);
if (!IsCryLogReady())
{
return false; // allow AZCore to do its default behavior.
}
CryWarning(VALIDATOR_MODULE_UNKNOWN, VALIDATOR_WARNING, "(%s) - %s", window, message);
return true; // suppress default AzCore behavior.
}
bool OnOutput(const char* window, const char* message) override
{
if (!IsCryLogReady())
{
return false; // allow AZCore to do its default behavior.
}
if (window == AZ::Debug::Trace::GetDefaultSystemWindow())
{
CryLogAlways("%s", message);
}
else
{
CryLog("(%s) - %s", window, message);
}
return true; // suppress default AzCore behavior.
}
private:
using IgnoredAssertMap = AZStd::unordered_map<AZ::Crc32, bool, AZStd::hash<AZ::Crc32>, AZStd::equal_to<AZ::Crc32>, AZ::OSStdAllocator>;
IgnoredAssertMap* m_ignoredAsserts;
};