Start working on build system.
This commit is contained in:
parent
4b308f6c32
commit
521486be62
88 changed files with 1065 additions and 349 deletions
24
src/base/core/logging/ConsoleLogger.cpp
Normal file
24
src/base/core/logging/ConsoleLogger.cpp
Normal file
|
@ -0,0 +1,24 @@
|
|||
#include "ConsoleLogger.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void ConsoleLogger::log_line(Level level,
|
||||
const String& msg,
|
||||
const String& fileName,
|
||||
const String& functionName,
|
||||
int lineNumber)
|
||||
{
|
||||
const auto log_msg = build_log_message(level,
|
||||
msg,
|
||||
fileName,
|
||||
functionName,
|
||||
lineNumber);
|
||||
if (level == Level::INFO)
|
||||
{
|
||||
printf("%s\n", log_msg.raw());
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "%s\n", log_msg.raw());
|
||||
}
|
||||
}
|
13
src/base/core/logging/ConsoleLogger.h
Normal file
13
src/base/core/logging/ConsoleLogger.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "Logger.h"
|
||||
|
||||
class ConsoleLogger : public Logger
|
||||
{
|
||||
public:
|
||||
void log_line(Level level,
|
||||
const String& line,
|
||||
const String& fileName,
|
||||
const String& functionName,
|
||||
int lineNumber) override;
|
||||
};
|
84
src/base/core/logging/FileLogger.cpp
Normal file
84
src/base/core/logging/FileLogger.cpp
Normal file
|
@ -0,0 +1,84 @@
|
|||
#include "FileLogger.h"
|
||||
|
||||
#include <time.h>
|
||||
|
||||
FileLogger::FileLogger()
|
||||
:mWorkDirectory(),
|
||||
mFileName("MT_Log.txt")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
FileLogger& FileLogger::GetInstance()
|
||||
{
|
||||
static FileLogger instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
FileLogger::~FileLogger()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void FileLogger::SetWorkDirectory(const std::string& workDir)
|
||||
{
|
||||
mWorkDirectory = workDir;
|
||||
}
|
||||
|
||||
void FileLogger::SetFileName(const std::string& fileName)
|
||||
{
|
||||
mFileName = fileName;
|
||||
}
|
||||
|
||||
void FileLogger::Open()
|
||||
{
|
||||
if (mWorkDirectory.empty())
|
||||
{
|
||||
mWorkDirectory = std::filesystem::current_path().string();
|
||||
}
|
||||
mFileStream.open(mWorkDirectory + "/" + mFileName);
|
||||
}
|
||||
|
||||
void FileLogger::Close()
|
||||
{
|
||||
mFileStream.close();
|
||||
}
|
||||
|
||||
void FileLogger::LogLine(const std::ostringstream& line)
|
||||
{
|
||||
if (mDisabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!mFileStream.is_open())
|
||||
{
|
||||
Open();
|
||||
}
|
||||
mFileStream << line.str() << std::endl;
|
||||
}
|
||||
|
||||
void FileLogger::disable()
|
||||
{
|
||||
mDisabled = true;
|
||||
}
|
||||
|
||||
void FileLogger::LogLine(const std::string& logType, const std::ostringstream& line, const std::string& fileName, const std::string& functionName, int lineNumber)
|
||||
{
|
||||
if (mDisabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::time_t t = std::time(nullptr);
|
||||
const std::string cleanedFileName = fileName.substr(fileName.find_last_of("/\\") + 1);
|
||||
std::tm time_buf{ };
|
||||
#ifdef WIN32
|
||||
gmtime_s(&time_buf, &t);
|
||||
#else
|
||||
gmtime_r(&t, &time_buf);
|
||||
#endif
|
||||
std::cout << logType << "|" << std::put_time(&time_buf, "%T") << "|" << cleanedFileName << "::" << functionName << "::" << lineNumber << "|" << line.str() << std::endl;
|
||||
mFileStream << logType << "|" << std::put_time(&time_buf, "%T") << "|" << cleanedFileName << "::" << functionName << "::" << lineNumber << "|" << line.str() << std::endl;
|
||||
}
|
35
src/base/core/logging/FileLogger.h
Normal file
35
src/base/core/logging/FileLogger.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
#pragma once
|
||||
|
||||
#include "Pointer.h"
|
||||
#include "String.h"
|
||||
|
||||
class FileLogger
|
||||
{
|
||||
FileLogger();
|
||||
|
||||
public:
|
||||
static FileLogger& GetInstance();
|
||||
|
||||
FileLogger(FileLogger const&) = delete;
|
||||
void operator=(FileLogger const&) = delete;
|
||||
|
||||
~FileLogger();
|
||||
|
||||
void disable();
|
||||
|
||||
void SetWorkDirectory(const String& workDir);
|
||||
|
||||
void SetFileName(const String& fileName);
|
||||
|
||||
void Open();
|
||||
|
||||
void Close();
|
||||
|
||||
void LogLine(const String& line);
|
||||
|
||||
void LogLine(const String& logType, const String& line, const String& fileName = "", const String& functionName = "", int lineNumber=-1);
|
||||
private:
|
||||
bool mDisabled{false};
|
||||
String mWorkDirectory;
|
||||
String mFileName;
|
||||
};
|
48
src/base/core/logging/Logger.cpp
Normal file
48
src/base/core/logging/Logger.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "Logger.h"
|
||||
|
||||
#include "Time.h"
|
||||
#include "ConsoleLogger.h"
|
||||
|
||||
static Ptr<Logger> s_logger;
|
||||
|
||||
Logger* Logger::get_instance()
|
||||
{
|
||||
if (s_logger.get() == nullptr)
|
||||
{
|
||||
s_logger = Ptr<ConsoleLogger>::create();
|
||||
}
|
||||
return s_logger.get();
|
||||
}
|
||||
|
||||
String Logger::build_log_message(Level level,
|
||||
const String& msg,
|
||||
const String& fileName,
|
||||
const String& functionName,
|
||||
int lineNumber)
|
||||
{
|
||||
String log_msg;
|
||||
if (level == Level::INFO)
|
||||
{
|
||||
log_msg += "Info|";
|
||||
}
|
||||
else if (level == Level::ERROR)
|
||||
{
|
||||
log_msg += "Error|";
|
||||
}
|
||||
log_msg += Time::get_now_str() + "|";
|
||||
|
||||
String cleaned_filename;
|
||||
if (auto index = fileName.rindex('/'); index.valid())
|
||||
{
|
||||
fileName.slice(index.value()+1, fileName.size(), cleaned_filename);
|
||||
}
|
||||
else
|
||||
{
|
||||
cleaned_filename = fileName;
|
||||
}
|
||||
log_msg += cleaned_filename + "::";
|
||||
log_msg += functionName + "::";
|
||||
log_msg += String::to_string(lineNumber) + "|";
|
||||
log_msg += msg;
|
||||
return log_msg;
|
||||
}
|
36
src/base/core/logging/Logger.h
Normal file
36
src/base/core/logging/Logger.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include "String.h"
|
||||
#include "Pointer.h"
|
||||
|
||||
#define LOG_ALL(msg, level) {String mt_logstream;\
|
||||
mt_logstream << msg; \
|
||||
Logger::get_instance()->log_line(level, mt_logstream, __FILE__, __FUNCTION__, __LINE__);};
|
||||
#define LOG_INFO(msg) LOG_ALL(msg, Logger::Level::INFO);
|
||||
#define LOG_ERROR(msg) LOG_ALL(msg, Logger::Level::ERROR);
|
||||
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
enum class Level
|
||||
{
|
||||
INFO,
|
||||
ERROR
|
||||
};
|
||||
|
||||
virtual ~Logger() = default;
|
||||
virtual void log_line(Level level,
|
||||
const String& line,
|
||||
const String& fileName,
|
||||
const String& functionName,
|
||||
int lineNumber){};
|
||||
|
||||
static Logger* get_instance();
|
||||
|
||||
protected:
|
||||
static String build_log_message(Level level,
|
||||
const String& line,
|
||||
const String& fileName,
|
||||
const String& functionName,
|
||||
int lineNumber);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue