parent
c494db695d
commit
ba63d6b1da
13 changed files with 319 additions and 0 deletions
@ -0,0 +1,52 @@ |
|||||||
|
project "bakatools" |
||||||
|
kind "StaticLib" |
||||||
|
language "C++" |
||||||
|
cppdialect "C++17" |
||||||
|
systemversion "latest" |
||||||
|
|
||||||
|
targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") |
||||||
|
objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") |
||||||
|
|
||||||
|
includedirs |
||||||
|
{ |
||||||
|
"%{prj.location}/vendor/spdlog/include", |
||||||
|
"%{prj.location}/src/" |
||||||
|
} |
||||||
|
|
||||||
|
files |
||||||
|
{ |
||||||
|
"%{prj.location}/src/bakatools/**.h", |
||||||
|
"%{prj.location}/src/bakatools/**.cpp", |
||||||
|
"%{prj.location}/src/bakatoolspch.h", |
||||||
|
} |
||||||
|
|
||||||
|
filter "system:windows" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_PLAFORM_WINDOWS" |
||||||
|
} |
||||||
|
|
||||||
|
filter "system:linux" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_PLAFORM_LINUX" |
||||||
|
} |
||||||
|
|
||||||
|
filter "configurations:Debug" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_DEBUG", |
||||||
|
"DEBUG" |
||||||
|
} |
||||||
|
runtime "Debug" |
||||||
|
symbols "on" |
||||||
|
|
||||||
|
|
||||||
|
filter "configurations:Release" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_RELEASE", |
||||||
|
"NDEBUG" |
||||||
|
} |
||||||
|
runtime "Release" |
||||||
|
optimize "on" |
@ -0,0 +1,5 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <bakatools/string/string_tools.h> |
||||||
|
#include <bakatools/looging/log.h> |
||||||
|
#include <bakatools/logging/assert.h> |
@ -0,0 +1,48 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
/*! \file assert.h
|
||||||
|
This file contains the assert macros. CORE macros musn't be used by the application. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "log.h" |
||||||
|
#include <bakara/tools/string_tools.h> |
||||||
|
|
||||||
|
#define BK_STRINGIFY(x) #x |
||||||
|
|
||||||
|
#ifdef BK_DEBUG |
||||||
|
#if defined(BK_PLATFORM_WINDOWS) |
||||||
|
#define BK_DEBUGBREAK() __debugbreak() |
||||||
|
#elif defined(BK_PLATFORM_LINUX) |
||||||
|
#include <signal.h> |
||||||
|
#define BK_DEBUGBREAK() raise(SIGTRAP) |
||||||
|
#else |
||||||
|
#error "Plaform doesn't support debug yet" |
||||||
|
#endif |
||||||
|
#define BK_ENABLE_ASSERT |
||||||
|
#else |
||||||
|
#define BK_DEBUGBREAK() |
||||||
|
#endif |
||||||
|
|
||||||
|
#ifdef BK_ENABLE_ASSERT |
||||||
|
/*! \def BK_CORE_VAMSG_ASSERT(check, msg, ...)
|
||||||
|
Assertes a condition, and throw an error with the formatted message as description |
||||||
|
@param check : Condionne to assert |
||||||
|
@param msg : format string error message |
||||||
|
@param ... : variable arguments to put in the string |
||||||
|
*/ |
||||||
|
#define BK_CORE_VAMSG_ASSERT(check, msg, ...) if(!(check)) { BK_CORE_ERROR(Tools::format("Assertion [%s] failed at %s:%d\n\tError : %s", BK_STRINGIFY(check), __FILE__, __LINE__, msg), __VA_ARGS__); BK_DEBUGBREAK(); } |
||||||
|
#define BK_CORE_MSG_ASSERT(check, msg) if(!(check)) { BK_CORE_ERROR("Assertion [{0}] failed at {1}:{2}\n\tError : {3}", BK_STRINGIFY(check), __FILE__, __LINE__, msg); BK_DEBUGBREAK(); } |
||||||
|
#define BK_CORE_ASSERT(check) if(!(check)) { BK_CORE_ERROR("Assertion [{0}] failed at {1}:{2}", BK_STRINGIFY(check), __FILE__, __LINE__); BK_DEBUGBREAK(); } |
||||||
|
|
||||||
|
#define BK_VAMSG_ASSERT(check, msg, ...) if(!(check)) { BK_ERROR(Bk::Tools::format("Assertion [%s] failed at %s:%d\n\tError : %s", BK_STRINGIFY(check), __FILE__, __LINE__, msg), __VA_ARGS__); BK_DEBUGBREAK(); } |
||||||
|
#define BK_MSG_ASSERT(check, msg) if(!(check)) { BK_ERROR("Assertion [{0}] failed at {1}:{2}\n\tError : {3}", BK_STRINGIFY(check), __FILE__, __LINE__, msg); BK_DEBUGBREAK(); } |
||||||
|
#define BK_ASSERT(check) if(!(check)) { BK_ERROR("Assertion [{0}] failed at {1}:{2}", BK_STRINGIFY(check), __FILE__, __LINE__); BK_DEBUGBREAK(); } |
||||||
|
#else |
||||||
|
#define BK_CORE_VAMSG_ASSERT(check, msg, ...) |
||||||
|
#define BK_CORE_MSG_ASSERT(check, msg) |
||||||
|
#define BK_CORE_ASSERT(check) |
||||||
|
|
||||||
|
#define BK_VAMSG_ASSERT(check, msg, ...) |
||||||
|
#define BK_MSG_ASSERT(check, msg) |
||||||
|
#define BK_ASSERT(check) |
||||||
|
#endif |
@ -0,0 +1,28 @@ |
|||||||
|
#include "log.h" |
||||||
|
|
||||||
|
namespace Bk { |
||||||
|
|
||||||
|
std::shared_ptr<spdlog::logger> Log::p_core_logger; |
||||||
|
std::shared_ptr<spdlog::logger> Log::p_app_logger; |
||||||
|
|
||||||
|
void Log::init() |
||||||
|
{ |
||||||
|
std::vector<spdlog::sink_ptr> log_sinks; |
||||||
|
log_sinks.emplace_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); |
||||||
|
log_sinks.emplace_back(std::make_shared<spdlog::sinks::basic_file_sink_mt>("bakara.log", true)); |
||||||
|
|
||||||
|
log_sinks[0]->set_pattern("%^[%T] %n: %v%$"); |
||||||
|
log_sinks[1]->set_pattern("[%T] [%l] %n: %v"); |
||||||
|
|
||||||
|
p_core_logger = std::make_shared<spdlog::logger>("BAKARA", begin(log_sinks), end(log_sinks)); |
||||||
|
spdlog::register_logger(p_core_logger); |
||||||
|
p_core_logger->set_level(spdlog::level::trace); |
||||||
|
p_core_logger->flush_on(spdlog::level::trace); |
||||||
|
|
||||||
|
p_app_logger = std::make_shared<spdlog::logger>("APP", begin(log_sinks), end(log_sinks)); |
||||||
|
spdlog::register_logger(p_app_logger); |
||||||
|
p_app_logger->set_level(spdlog::level::trace); |
||||||
|
p_app_logger->flush_on(spdlog::level::trace); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <bakatoolspch.h> |
||||||
|
#include <spdlog/spdlog.h> |
||||||
|
#include <spdlog/fmt/ostr.h> |
||||||
|
#include <spdlog/sinks/stdout_color_sinks.h> |
||||||
|
#include <spdlog/sinks/basic_file_sink.h> |
||||||
|
|
||||||
|
namespace Bk { |
||||||
|
|
||||||
|
class Log |
||||||
|
{ |
||||||
|
public: |
||||||
|
static void init(); |
||||||
|
|
||||||
|
static std::shared_ptr<spdlog::logger>& get_core_logger() { return p_core_logger; } |
||||||
|
static std::shared_ptr<spdlog::logger>& get_app_logger() { return p_app_logger; } |
||||||
|
private: |
||||||
|
static std::shared_ptr<spdlog::logger> p_core_logger; |
||||||
|
static std::shared_ptr<spdlog::logger> p_app_logger; |
||||||
|
}; |
||||||
|
#ifdef BK_DEBUG |
||||||
|
#define BK_CORE_TRACE(...) ::Bk::Log::get_core_logger()->trace(__VA_ARGS__) |
||||||
|
#define BK_CORE_INFO(...) ::Bk::Log::get_core_logger()->info(__VA_ARGS__) |
||||||
|
#define BK_CORE_WARN(...) ::Bk::Log::get_core_logger()->warn(__VA_ARGS__) |
||||||
|
#define BK_CORE_ERROR(...) ::Bk::Log::get_core_logger()->error(__VA_ARGS__) |
||||||
|
#define BK_CORE_CRITICAL(...) ::Bk::Log::get_core_logger()->critical(__VA_ARGS__) |
||||||
|
|
||||||
|
#define BK_TRACE(...) ::Bk::Log::get_app_logger()->trace(__VA_ARGS__) |
||||||
|
#define BK_INFO(...) ::Bk::Log::get_app_logger()->info(__VA_ARGS__) |
||||||
|
#define BK_WARN(...) ::Bk::Log::get_app_logger()->warn(__VA_ARGS__) |
||||||
|
#define BK_ERROR(...) ::Bk::Log::get_app_logger()->error(__VA_ARGS__) |
||||||
|
#define BK_CRITICAL(...) ::Bk::Log::get_app_logger()->critical(__VA_ARGS__) |
||||||
|
#else |
||||||
|
#define BK_CORE_TRACE(...) |
||||||
|
#define BK_CORE_INFO(...) |
||||||
|
#define BK_CORE_WARN(...) |
||||||
|
#define BK_CORE_ERROR(...) |
||||||
|
#define BK_CORE_CRITICAL(...) |
||||||
|
|
||||||
|
#define BK_TRACE(...) |
||||||
|
#define BK_INFO(...) |
||||||
|
#define BK_WARN(...) |
||||||
|
#define BK_ERROR(...) |
||||||
|
#define BK_CRITICAL(...) |
||||||
|
#endif |
||||||
|
} |
@ -0,0 +1,63 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
/*! \file format.h
|
||||||
|
This file provides functions to do string formatting. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <bakatoolspch.h> |
||||||
|
|
||||||
|
namespace Bk::Tools { |
||||||
|
inline void format_impl(std::stringstream& ss, const char* format) {
|
||||||
|
while (*format) { |
||||||
|
if (*format == '%' && *++format != '%') // %% == % (not a format directive)
|
||||||
|
throw std::invalid_argument("not enough arguments !\n"); |
||||||
|
ss << *format++; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
template <typename Arg, typename... Args> |
||||||
|
void format_impl(std::stringstream& ss, const char* format, Arg arg, Args... args) { |
||||||
|
while (*format) { |
||||||
|
if (*format == '%' && *++format != '%') { |
||||||
|
auto current_format_qualifier = *format; |
||||||
|
switch(current_format_qualifier) { |
||||||
|
case 'd' :
|
||||||
|
if (!std::is_integral<Arg>()) throw std::invalid_argument("%d introduces integral argument"); |
||||||
|
break; |
||||||
|
case 'f' :
|
||||||
|
if (!std::is_floating_point<Arg>()) throw std::invalid_argument("%f introduces floating point argument"); |
||||||
|
break; |
||||||
|
case 'b' : |
||||||
|
if(arg) ss << "true"; |
||||||
|
else ss << "false"; |
||||||
|
return format_impl(ss, ++format, args...); |
||||||
|
case 's' : |
||||||
|
break; |
||||||
|
// etc.
|
||||||
|
default:
|
||||||
|
throw std::invalid_argument("Not a standard format"); |
||||||
|
break; |
||||||
|
} |
||||||
|
// it's true you'd have to handle many more format qualifiers, but on a safer basis
|
||||||
|
ss << arg; // arg type is deduced
|
||||||
|
return format_impl(ss, ++format, args...); // one arg less
|
||||||
|
} |
||||||
|
ss << *format++; |
||||||
|
} // the format string is exhausted and we still have args : throw
|
||||||
|
throw std::invalid_argument("Too many arguments\n"); |
||||||
|
} |
||||||
|
|
||||||
|
/*! \fn std::string Bk::Tools::format(const char* fmt, Args... args)
|
||||||
|
Formats a string, printf like. Accepts integers, floating point numbers, strings, booléen. |
||||||
|
@param fmt : string to format |
||||||
|
@param args : variable arguments to put in the string |
||||||
|
@return String formatted |
||||||
|
*/ |
||||||
|
template <typename... Args> |
||||||
|
inline std::string string_format(const char* fmt, Args... args) { |
||||||
|
std::stringstream ss; |
||||||
|
format_impl(ss, fmt, args...); |
||||||
|
return ss.str(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,17 @@ |
|||||||
|
#include "string_tools.h" |
||||||
|
|
||||||
|
namespace Bk::Tools { |
||||||
|
std::unique_ptr<std::vector<std::string>> string_split(std::string& str, std::string delimiter, int cpt) |
||||||
|
{ |
||||||
|
std::string s(str); |
||||||
|
std::unique_ptr<std::vector<std::string>> splits(new std::vector<std::string>(0)); |
||||||
|
size_t pos = 0; |
||||||
|
while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0)
|
||||||
|
{ |
||||||
|
splits->push_back(s.substr(0, pos)); |
||||||
|
s.erase(0, pos + delimiter.length()); |
||||||
|
} |
||||||
|
if (s.length()) splits->push_back(s); |
||||||
|
return splits; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,10 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "format.h" |
||||||
|
|
||||||
|
namespace Bk::Tools { |
||||||
|
std::string string_to_lower(std::string& str); |
||||||
|
std::string string_to_upper(std::string& str); |
||||||
|
std::unique_ptr<std::vector<std::string>> string_split(std::string& str, std::string delimiter, int cpt = -1); |
||||||
|
void string_trim(std::string& str, const std::string& whitespace = " \b\0"); |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
#include "string_tools.h" |
||||||
|
|
||||||
|
namespace Bk::Tools { |
||||||
|
std::string string_to_lower(std::string& str) |
||||||
|
{ |
||||||
|
for (int i = 0; i < str.length(); i++) |
||||||
|
{ |
||||||
|
str[i] = std::tolower(str[i]); |
||||||
|
} |
||||||
|
return str; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
#include "string_tools.h" |
||||||
|
|
||||||
|
namespace Bk::Tools { |
||||||
|
std::string string_to_upper(std::string& str) |
||||||
|
{ |
||||||
|
for (int i = 0; i < str.length(); i++) |
||||||
|
{ |
||||||
|
str[i] = std::toupper(str[i]); |
||||||
|
} |
||||||
|
return str; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,13 @@ |
|||||||
|
#include "string_tools.h" |
||||||
|
|
||||||
|
namespace Bk::Tools { |
||||||
|
void string_trim(std::string& str, const std::string& whitespace) |
||||||
|
{ |
||||||
|
const auto strBegin = str.find_first_not_of(whitespace); |
||||||
|
const auto strEnd = str.find_last_not_of(whitespace); |
||||||
|
if (strBegin != std::string::npos) |
||||||
|
{ |
||||||
|
str.erase(0, strBegin); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,12 @@ |
|||||||
|
#pragme once |
||||||
|
|
||||||
|
#include <cstring> |
||||||
|
#include <vector> |
||||||
|
#include <string> |
||||||
|
#include <cstdint> |
||||||
|
#include <cctype> |
||||||
|
#include <exception> |
||||||
|
#include <sstream> |
||||||
|
#include <ostream> |
||||||
|
#include <utility> |
||||||
|
#include <type_traits> |
Loading…
Reference in New Issue