Elevate Engine 1
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1#pragma once
2
3#include <format>
4#include <string>
5#include <memory>
6#include <spdlog/spdlog.h>
7#include <spdlog/sinks/stdout_color_sinks.h>
8
9#include "Core.h"
10
11// Create a simple system to tell the .js what we are trying to log if building for the web
12#ifdef EE_PLATFORM_WEB
13 #define EE_TRACE_PREFIX "[TRACE] "
14 #define EE_INFO_PREFIX "[INFO] "
15 #define EE_WARN_PREFIX "[WARN] "
16 #define EE_ERROR_PREFIX "[ERROR] "
17 #define EE_FATAL_PREFIX "[FATAL] "
18#else
19 #define EE_TRACE_PREFIX ""
20 #define EE_INFO_PREFIX ""
21 #define EE_WARN_PREFIX ""
22 #define EE_ERROR_PREFIX ""
23 #define EE_FATAL_PREFIX ""
24#endif
25
26namespace Elevate {
27 class LogImpl {
28 public:
29 LogImpl(std::shared_ptr<spdlog::logger> logger) : m_logger(logger) { }
30 inline void Trace(const std::string& text) { m_logger->trace(EE_TRACE_PREFIX "{}", text); }
31 inline void Info(const std::string& text) { m_logger->info(EE_INFO_PREFIX "{}", text); }
32 inline void Warn(const std::string& text) { m_logger->warn(EE_WARN_PREFIX "{}", text); }
33 inline void Error(const std::string& text) { m_logger->error(EE_ERROR_PREFIX "{}", text); }
34 inline void Fatal(const std::string& text) { m_logger->critical(EE_FATAL_PREFIX "{}", text); }
35 private:
36 std::shared_ptr<spdlog::logger> m_logger;
37 };
38
39 class Log {
40 public:
41 static inline void Trace(LogImpl* logger, const std::string& text) { if(logger) logger->Trace(text); }
42 static inline void Info(LogImpl* logger, const std::string& text) { if(logger) logger->Info(text); }
43 static inline void Warn(LogImpl* logger, const std::string& text) { if(logger) logger->Warn(text); }
44 static inline void Error(LogImpl* logger, const std::string& text) { if(logger) logger->Error(text); }
45 static inline void Fatal(LogImpl* logger, const std::string& text) { if(logger) logger->Fatal(text); }
46
47 static LogImpl* GetCoreLogger();
48 static LogImpl* GetClientLogger();
49 };
50}
51
52#if defined(EE_ENGINE_BUILD) || defined(EE_ENGINE_INTERNAL)
53#define EE_CORE_TRACE(...) ::Elevate::Log::Trace(::Elevate::Log::GetCoreLogger(), std::format(__VA_ARGS__))
54#define EE_CORE_INFO(...) ::Elevate::Log::Info(::Elevate::Log::GetCoreLogger(), std::format(__VA_ARGS__))
55#define EE_CORE_WARN(...) ::Elevate::Log::Warn(::Elevate::Log::GetCoreLogger(), std::format(__VA_ARGS__))
56#define EE_CORE_ERROR(...) ::Elevate::Log::Error(::Elevate::Log::GetCoreLogger(), std::format(__VA_ARGS__))
57#define EE_CORE_FATAL(...) ::Elevate::Log::Fatal(::Elevate::Log::GetCoreLogger(), std::format(__VA_ARGS__))
58
59#define EE_CORE_CTRACE(condition, ...) if(condition) { EE_CORE_TRACE(__VA_ARGS__); }
60#define EE_CORE_CINFO(condition, ...) if(condition) { EE_CORE_INFO(__VA_ARGS__); }
61#define EE_CORE_CWARN(condition, ...) if(condition) { EE_CORE_WARN(__VA_ARGS__); }
62#define EE_CORE_CERROR(condition, ...) if(condition) { EE_CORE_ERROR(__VA_ARGS__); }
63#define EE_CORE_CFATAL(condition, ...) if(condition) { EE_CORE_FATAL(__VA_ARGS__); }
64#endif
65
66#define EE_TRACE(...) ::Elevate::Log::Trace(::Elevate::Log::GetClientLogger(), std::format(__VA_ARGS__))
67#define EE_INFO(...) ::Elevate::Log::Info(::Elevate::Log::GetClientLogger(), std::format(__VA_ARGS__))
68#define EE_WARN(...) ::Elevate::Log::Warn(::Elevate::Log::GetClientLogger(), std::format(__VA_ARGS__))
69#define EE_ERROR(...) ::Elevate::Log::Error(::Elevate::Log::GetClientLogger(), std::format(__VA_ARGS__))
70#define EE_FATAL(...) ::Elevate::Log::Fatal(::Elevate::Log::GetClientLogger(), std::format(__VA_ARGS__))
71
72#define EE_CTRACE(condition, ...) if(condition) { EE_TRACE(__VA_ARGS__); }
73#define EE_CINFO(condition, ...) if(condition) { EE_INFO(__VA_ARGS__); }
74#define EE_CWARN(condition, ...) if(condition) { EE_WARN(__VA_ARGS__); }
75#define EE_CERROR(condition, ...) if(condition) { EE_ERROR(__VA_ARGS__); }
76#define EE_CFATAL(condition, ...) if(condition) { EE_FATAL(__VA_ARGS__); }
#define EE_FATAL_PREFIX
Definition Log.h:23
#define EE_TRACE_PREFIX
Definition Log.h:19
#define EE_INFO_PREFIX
Definition Log.h:20
#define EE_WARN_PREFIX
Definition Log.h:21
#define EE_ERROR_PREFIX
Definition Log.h:22
void Fatal(const std::string &text)
Definition Log.h:34
void Warn(const std::string &text)
Definition Log.h:32
void Info(const std::string &text)
Definition Log.h:31
LogImpl(std::shared_ptr< spdlog::logger > logger)
Definition Log.h:29
void Error(const std::string &text)
Definition Log.h:33
void Trace(const std::string &text)
Definition Log.h:30
static LogImpl * GetCoreLogger()
Definition Log.cpp:11
static void Error(LogImpl *logger, const std::string &text)
Definition Log.h:44
static void Trace(LogImpl *logger, const std::string &text)
Definition Log.h:41
static void Info(LogImpl *logger, const std::string &text)
Definition Log.h:42
static void Warn(LogImpl *logger, const std::string &text)
Definition Log.h:43
static void Fatal(LogImpl *logger, const std::string &text)
Definition Log.h:45
static LogImpl * GetClientLogger()
Definition Log.cpp:22