Files
Plugin/Source/SenseGloveLog/Private/SGLog/SGLog.cpp
T

284 lines
8.3 KiB
C++

/**
* @file
*
* @author Mamadou Babaei <mamadou@senseglove.com>
*
* @section LICENSE
*
* (The MIT License)
*
* Copyright (c) 2020 - 2025 SenseGlove
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @section DESCRIPTION
*
*
*/
#include "SGLog/SGLog.h"
#include "Containers/Map.h"
#include "Logging/LogMacros.h"
#include "Templates/Atomic.h"
DEFINE_LOG_CATEGORY(LogEditor)
DEFINE_LOG_CATEGORY(LogGeneric)
struct FSGLogCore::FStaticImpl
{
/************************
* Type definitions
************************/
struct VerbosityMapper
{
FString Tag;
FColor Color;
VerbosityMapper(const FString& InTag, const FColor& InColor);
};
/************************
* Member variables
************************/
TMap<FSGLogCore::EVerbosity, VerbosityMapper> VerbosityMap;
TAtomic<bool> bInitialized;
/************************
* Constructor / Destructor
************************/
FStaticImpl();
~FStaticImpl();
};
struct FSGLogCore::FImpl
{
/************************
* Member variables
************************/
FSGLogCore::EVerbosity Verbosity;
FSGLogCore::ECategory Category;
FString File;
FString Function;
FString Line;
/************************
* Constructor / Destructor
************************/
FImpl();
~FImpl();
/************************
* Default copy constructor & copy assignment operator
************************/
FImpl(const FImpl& Rhs) = default;
FImpl& operator=(const FImpl& Rhs) = default;
};
TUniquePtr<FSGLogCore::FStaticImpl, FSGLogCore::FStaticImplDeleter> FSGLogCore::SPimpl{
new FStaticImpl{}, FStaticImplDeleter()
};
FSGLogCore::FSGLogCore(const EVerbosity& Verbosity, const ECategory& Category, const std::string& File,
const std::string& Function, const int32 Line)
: Pimpl(TUniquePtr<FImpl, FImplDeleter>(new FImpl{}, PimplDeleter)),
bAnyEntries(false)
{
#if defined ( SENSEGLOVE_LOGGING )
if (!SPimpl->bInitialized)
{
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::Fatal,
FStaticImpl::VerbosityMapper(TEXT("FATAL"), FColor::White));
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::Error,
FStaticImpl::VerbosityMapper(TEXT("ERROR"), FColor::Red));
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::Warning,
FStaticImpl::VerbosityMapper(TEXT("WARNING"), FColor::Yellow));
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::Display,
FStaticImpl::VerbosityMapper(TEXT("DISPLAY"), FColor::Green));
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::Log,
FStaticImpl::VerbosityMapper(TEXT("LOG"), FColor::Silver));
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::Verbose,
FStaticImpl::VerbosityMapper(TEXT("VERBOSE"), FColor::Purple));
SPimpl->VerbosityMap.Add(FSGLogCore::EVerbosity::VeryVerbose,
FStaticImpl::VerbosityMapper(TEXT("VERY_VERBOSE"), FColor::Magenta));
SPimpl->bInitialized = true;
}
Pimpl->Verbosity = Verbosity;
Pimpl->Category = Category;
Pimpl->File = FString(StringCast<WIDECHAR>(File.c_str()).Get());
Pimpl->Function = FString(StringCast<WIDECHAR>(Function.c_str()).Get());
Pimpl->Line = FString::FromInt(Line);
#endif /* defined ( SENSEGLOVE_LOGGING ) */
}
FSGLogCore::~FSGLogCore()
{
#if defined ( SENSEGLOVE_LOGGING )
const EVerbosity& Verbosity = Pimpl->Verbosity;
const ECategory& Category = Pimpl->Category;
const FStaticImpl::VerbosityMapper& VerbosityMapper = SPimpl->VerbosityMap[Verbosity];
const FString& Tag = VerbosityMapper.Tag;
const FColor& Color = VerbosityMapper.Color;
const FString Message(FString::Printf(TEXT("[%s %s %s %s] %s"), Tag.GetCharArray().GetData(),
Pimpl->File.GetCharArray().GetData(),
Pimpl->Function.GetCharArray().GetData(),
Pimpl->Line.GetCharArray().GetData(), Buffer.GetCharArray().GetData()));
// Generic
if (Category == ECategory::Generic)
{
switch (Verbosity)
{
case EVerbosity::Display:
{
UE_LOG(LogGeneric, Display, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Error:
{
UE_LOG(LogGeneric, Error, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Fatal:
{
UE_LOG(LogGeneric, Fatal, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Log:
{
UE_LOG(LogGeneric, Log, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Verbose:
{
UE_LOG(LogGeneric, Verbose, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::VeryVerbose:
{
UE_LOG(LogGeneric, VeryVerbose, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Warning:
{
UE_LOG(LogGeneric, Warning, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
}
}
// Editor
if (Category == ECategory::Editor)
{
switch (Verbosity)
{
case EVerbosity::Display:
{
UE_LOG(LogEditor, Display, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Error:
{
UE_LOG(LogEditor, Error, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Fatal:
{
UE_LOG(LogEditor, Fatal, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Log:
{
UE_LOG(LogEditor, Log, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Verbose:
{
UE_LOG(LogEditor, Verbose, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::VeryVerbose:
{
UE_LOG(LogEditor, VeryVerbose, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
case EVerbosity::Warning:
{
UE_LOG(LogEditor, Warning, TEXT("%s"), Message.GetCharArray().GetData());
}
break;
}
}
#endif /* defined ( SENSEGLOVE_LOGGING ) */
}
FSGLogCore::FStaticImpl::FStaticImpl()
: bInitialized(false)
{
}
FSGLogCore::FStaticImpl::~FStaticImpl() = default;
FSGLogCore::FStaticImpl::VerbosityMapper::VerbosityMapper(const FString& InTag,
const FColor& InColor)
: Tag(InTag),
Color(InColor)
{
}
void FSGLogCore::FStaticImplDeleter::operator()(const FSGLogCore::FStaticImpl* Pointer) const
{
delete Pointer;
}
FSGLogCore::FImpl::FImpl()
: Verbosity(EVerbosity::Verbose),
Category(ECategory::Generic)
{
}
FSGLogCore::FImpl::~FImpl() = default;
void FSGLogCore::FImplDeleter::operator()(const FSGLogCore::FImpl* Pointer) const
{
delete Pointer;
}