c-project/include/log.h

73 lines
1.2 KiB
C
Raw Normal View History

2024-05-15 11:30:34 +02:00
#pragma once
#include <sstream>
#include <iostream>
#include <fstream>
class LogType
{
public:
2024-05-15 12:29:16 +02:00
virtual void log(std::stringstream &ss) const = 0;
2024-05-15 11:30:34 +02:00
};
class DefaultLog : public LogType
{
public:
2024-05-15 12:29:16 +02:00
void log(std::stringstream &ss) const override
2024-05-15 11:30:34 +02:00
{
std::cout << ss.str() << "\n";
};
};
2024-05-15 12:29:16 +02:00
class ErrorLog: public LogType
2024-05-15 11:30:34 +02:00
{
public:
2024-05-15 12:29:16 +02:00
void log(std::stringstream &ss) const override
2024-05-15 11:30:34 +02:00
{
2024-05-15 12:29:16 +02:00
#ifdef PLATFORM_WIN32
2024-05-15 11:30:34 +02:00
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
std::cout << ss.str() << "\n";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
2024-05-15 12:29:16 +02:00
#else
2024-05-15 11:30:34 +02:00
std::cout << "Error: " << ss << "\n";
2024-05-15 12:29:16 +02:00
#endif
2024-05-15 11:30:34 +02:00
};
};
2024-05-15 12:29:16 +02:00
class LogToAFile: public LogType
2024-05-15 11:30:34 +02:00
{
std::string name;
public:
2024-05-15 12:29:16 +02:00
LogToAFile(std::string name):name(name)
2024-05-15 11:30:34 +02:00
{}
2024-05-15 12:29:16 +02:00
void log(std::stringstream &ss) const override
2024-05-15 11:30:34 +02:00
{
std::ofstream f(name);
f << ss.str() << "\n";
f.close();
};
};
2024-05-15 12:29:16 +02:00
inline void llog(const LogType &l, std::stringstream &ss)
2024-05-15 11:30:34 +02:00
{
l.log(ss);
}
template<class T, class ...T2>
2024-05-15 12:29:16 +02:00
inline void llog(const LogType &l, std::stringstream &ss, const T &t, const T2 &...t2)
2024-05-15 11:30:34 +02:00
{
ss << t << " ";
llog(l, ss, t2...);
}
template<class T, class ...T2>
2024-05-15 12:29:16 +02:00
inline void llog(const LogType &l, const T &t, const T2 &...t2)
2024-05-15 11:30:34 +02:00
{
std::stringstream ss;
ss << t << " ";
llog(l, ss, t2...);
}