Add logger
This commit is contained in:
parent
281ad37728
commit
e5c522af7a
|
@ -18,7 +18,7 @@ add_executable("${CMAKE_PROJECT_NAME}")
|
|||
set_property(TARGET "${CMAKE_PROJECT_NAME}" PROPERTY CXX_STANDARD 17)
|
||||
target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC _CRT_SECURE_NO_WARNINGS)
|
||||
target_compile_definitions("${CMAKE_PROJECT_NAME}" PUBLIC RESOURCES_PATH="${CMAKE_CURRENT_SOURCE_DIR}/resources/")
|
||||
target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES})
|
||||
target_sources("${CMAKE_PROJECT_NAME}" PRIVATE ${MY_SOURCES} "include/log.h")
|
||||
if(MSVC)
|
||||
set_target_properties("${CMAKE_PROJECT_NAME}" PROPERTIES LINK_FLAGS_DEBUG "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
|
||||
set_target_properties("${CMAKE_PROJECT_NAME}" PROPERTIES LINK_FLAGS_RELEASE "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
#pragma once
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
class LogType
|
||||
{
|
||||
public:
|
||||
virtual void log(std::stringstream& ss) const = 0;
|
||||
};
|
||||
|
||||
|
||||
class DefaultLog : public LogType
|
||||
{
|
||||
public:
|
||||
void log(std::stringstream& ss) const override
|
||||
{
|
||||
std::cout << ss.str() << "\n";
|
||||
};
|
||||
};
|
||||
|
||||
class ErrorLog : public LogType
|
||||
{
|
||||
public:
|
||||
void log(std::stringstream& ss) const override
|
||||
{
|
||||
#ifdef PLATFORM_WIN32
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
|
||||
std::cout << ss.str() << "\n";
|
||||
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
|
||||
#else
|
||||
std::cout << "Error: " << ss << "\n";
|
||||
#endif
|
||||
};
|
||||
};
|
||||
|
||||
class LogToAFile : public LogType
|
||||
{
|
||||
std::string name;
|
||||
public:
|
||||
LogToAFile(std::string name) :name(name)
|
||||
{}
|
||||
|
||||
void log(std::stringstream& ss) const override
|
||||
{
|
||||
std::ofstream f(name);
|
||||
f << ss.str() << "\n";
|
||||
f.close();
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
|
||||
inline void llog(const LogType& l, std::stringstream& ss)
|
||||
{
|
||||
l.log(ss);
|
||||
}
|
||||
|
||||
template<class T, class ...T2>
|
||||
inline void llog(const LogType& l, std::stringstream& ss, const T& t, const T2 &...t2)
|
||||
{
|
||||
ss << t << " ";
|
||||
llog(l, ss, t2...);
|
||||
}
|
||||
|
||||
template<class T, class ...T2>
|
||||
inline void llog(const LogType& l, const T& t, const T2 &...t2)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << t << " ";
|
||||
llog(l, ss, t2...);
|
||||
}
|
Loading…
Reference in New Issue