Skip to content

File Log.h

File List > Amplitude > IO > Log.h

Go to the documentation of this file

// Copyright (c) 2021-present Sparky Studios. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#ifndef _AM_IO_LOG_H
#define _AM_IO_LOG_H

#include <SparkyStudios/Audio/Amplitude/Core/Common.h>

#define amLogger SparkyStudios::Audio::Amplitude::Logger::GetLogger()

#define amLog(_level_, _message_, ...)                                                                                                     \
    if (amLogger != nullptr)                                                                                                               \
    {                                                                                                                                      \
        constexpr size_t bufferLen = 4096;                                                                                                 \
        char buffer[bufferLen];                                                                                                            \
        int formatted = std::snprintf(buffer, bufferLen, _message_, ##__VA_ARGS__);                                                        \
        amLogger->_level_(__FILE__, __LINE__, SparkyStudios::Audio::Amplitude::AmString(buffer).substr(0, formatted));                                                      \
    }                                                                                                                                      \
    (void)0

#define amLogDebug(_message_, ...) amLog(Debug, _message_, ##__VA_ARGS__)

#define amLogInfo(_message_, ...) amLog(Info, _message_, ##__VA_ARGS__)

#define amLogWarning(_message_, ...) amLog(Warning, _message_, ##__VA_ARGS__)

#define amLogError(_message_, ...) amLog(Error, _message_, ##__VA_ARGS__)

#define amLogCritical(_message_, ...) amLog(Critical, _message_, ##__VA_ARGS__)

#define amLogSuccess(_message_, ...) amLog(Success, _message_, ##__VA_ARGS__)

namespace SparkyStudios::Audio::Amplitude
{
    enum eLogMessageLevel : AmUInt8
    {
        eLogMessageLevel_Debug = 0,

        eLogMessageLevel_Info = 1,

        eLogMessageLevel_Warning = 2,

        eLogMessageLevel_Error = 3,

        eLogMessageLevel_Critical = 4,

        eLogMessageLevel_Success = 5,
    };

    class AM_API_PUBLIC Logger
    {
    public:
        virtual ~Logger() = default;

        static void SetLogger(Logger* loggerInstance);

        static Logger* GetLogger();

        void Debug(const char* file, int line, const AmString& message);

        void Info(const char* file, int line, const AmString& message);

        void Warning(const char* file, int line, const AmString& message);

        void Error(const char* file, int line, const AmString& message);

        void Critical(const char* file, int line, const AmString& message);

        void Success(const char* file, int line, const AmString& message);

    protected:
        virtual void Log(eLogMessageLevel level, const char* file, int line, const AmString& message) = 0;
    };
} // namespace SparkyStudios::Audio::Amplitude

#endif // _AM_IO_LOG_H