Commit 21c5717c authored by Florian Oetke's avatar Florian Oetke
Browse files

replaced logging/stacktrace with plog and backward

parent 16d26c81
[submodule "dependencies/backward-cpp"]
path = dependencies/backward-cpp
url = git@github.com:bombela/backward-cpp.git
[submodule "dependencies/plog"]
path = dependencies/plog
url = git@github.com:SergiusTheBest/plog.git
......@@ -11,6 +11,7 @@ if(MIRRAGE_BUILD_MESH_CONVERTER)
endif()
add_subdirectory(asyncplusplus)
add_subdirectory(backward-cpp)
add_subdirectory(enet)
add_subdirectory(glm)
include(gsl_interface.cmake)
......@@ -23,6 +24,7 @@ SET(PHYSFS_ARCHIVE_7Z FALSE CACHE BOOL "" FORCE)
SET(PHYSFS_BUILD_SHARED FALSE CACHE BOOL "" FORCE)
add_subdirectory(physfs)
include(plog_interface.cmake)
include(robin-map_interface.cmake)
add_subdirectory(sf2)
......
Subproject commit c25d2e0f8e69365409781846ef3be38ace8a9d06
Subproject commit b56969ec12dacc40b08daa211e336feead538e94
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project(plog)
add_library(plog INTERFACE)
target_include_directories(plog SYSTEM INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/plog/include>
$<INSTALL_INTERFACE:include>)
install(TARGETS plog EXPORT plogTargets)
export(
EXPORT plogTargets
FILE "${CMAKE_CURRENT_BINARY_DIR}/plogTargets.cmake"
)
install(
EXPORT plogTargets FILE plogTargets.cmake
NAMESPACE plog::
DESTINATION lib/cmake
)
......@@ -13,8 +13,11 @@ add_executable(demo ${HEADER_FILES}
src/main.cpp
src/meta_system.cpp
src/test_screen.cpp
${BACKWARD_ENABLE}
)
add_backward(demo)
set(MIRRAGE_DEFAULT_COMPILER_ARGS ${MIRRAGE_DEFAULT_COMPILER_ARGS})
target_compile_options(demo PRIVATE ${MIRRAGE_DEFAULT_COMPILER_ARGS})
......@@ -22,6 +25,7 @@ target_link_libraries(demo
PRIVATE
mirrage::mirrage
mirrage::renderer
plog
)
option(MIRRAGE_COMPILE_SHADERS "Compile demo shaders" OFF)
......
......@@ -15,14 +15,15 @@
namespace mirrage {
Game_engine::Game_engine(const std::string& title,
Game_engine::Game_engine(const std::string& org,
const std::string& title,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
int argc,
char** argv,
char** env)
: Engine("secondsystem", title, version_major, version_minor, debug, false, argc, argv, env)
: Engine(org, title, version_major, version_minor, debug, false, argc, argv, env)
, _renderer_factory(std::make_unique<renderer::Deferred_renderer_factory>(
graphics_context(),
window(),
......
......@@ -21,7 +21,8 @@ namespace mirrage {
class Game_engine : public Engine {
public:
Game_engine(const std::string& title,
Game_engine(const std::string& org,
const std::string& title,
std::uint32_t version_major,
std::uint32_t version_minor,
bool debug,
......
......@@ -11,10 +11,10 @@
#include <mirrage/info.hpp>
#include <mirrage/asset/asset_manager.hpp>
#include <mirrage/utils/log.hpp>
#include <mirrage/utils/stacktrace.hpp>
#include <SDL2/SDL.h>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <plog/Log.h>
#include <glm/vec2.hpp>
#include <exception>
......@@ -55,6 +55,7 @@ int main(int argc, char** argv, char** env) {
}
namespace {
constexpr auto org_name = "secondsystem";
constexpr auto app_name = "Mirrage";
int argc;
char** argv;
......@@ -62,85 +63,52 @@ namespace {
void init_env(int argc, char** argv, char** env) {
auto write_dir = asset::write_dir(argv[0], org_name, app_name);
static auto fileAppender = plog::RollingFileAppender<plog::TxtFormatter>(
(write_dir + "/mirrage.log").c_str(), 4L * 1024L, 4);
static auto consoleAppender = plog::ColorConsoleAppender<plog::TxtFormatter>();
plog::init(plog::debug, &fileAppender).addAppender(&consoleAppender);
//auto testC = glm::vec2{1,2} - glm::vec2{1,1}; (void)testC;
//MIRRAGE_INVARIANT(testC.y==(testA.y - testB.y), "XXX: "<<testC.y<<" != "<<(testA.y - testB.y));
::argc = argc;
::argv = argv;
::env = env;
MIRRAGE_INFO("Game started from: " << argv[0] << "\n"
<< "Working dir: " << asset::pwd() << "\n"
<< "Version: " << version_info::name << "\n"
<< "Version-Hash: " << version_info::hash << "\n"
<< "Version-Date: " << version_info::date << "\n"
<< "Version-Subject: " << version_info::subject << "\n");
try {
util::init_stacktrace(argv[0]);
mirrage::asset::setup_storage();
} catch(const util::Error& ex) {
MIRRAGE_CRASH_REPORT("Exception in init: " << ex.what());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Sorry :-(", "Error in init", nullptr);
shutdown();
exit(1);
}
LOG(plog::debug) << "Game started from: " << argv[0] << "\n"
<< "Working dir: " << asset::pwd() << "\n"
<< "Write dir: " << write_dir << "\n"
<< "Version: " << version_info::name << "\n"
<< "Version-Hash: " << version_info::hash << "\n"
<< "Version-Date: " << version_info::date << "\n"
<< "Version-Subject: " << version_info::subject << "\n";
}
void init_engine() {
try {
bool debug = false;
bool debug = false;
#ifndef NDEBUG
debug = true;
debug = true;
#endif
for(auto i = 1; i < argc; i++) {
if(argv[i] == "--debug"s) {
debug = true;
}
if(argv[i] == "--no-debug"s) {
debug = false;
}
for(auto i = 1; i < argc; i++) {
if(argv[i] == "--debug"s) {
debug = true;
}
if(argv[i] == "--no-debug"s) {
debug = false;
}
engine = std::make_unique<Game_engine>(app_name, 0, 1, debug, argc, argv, env);
if(argc > 1 && argv[1] == "test"s)
engine->screens().enter<Test_screen>();
else
engine->screens().enter<Test_screen>();
} catch(const util::Error& ex) {
MIRRAGE_CRASH_REPORT("Exception in init: " << ex.what());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Sorry :-(", "Error in init", nullptr);
shutdown();
exit(1);
}
}
void onFrame() {
try {
engine->on_frame();
} catch(const util::Error& ex) {
MIRRAGE_CRASH_REPORT("Exception in onFrame: " << ex.what());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Sorry :-(", "Error in onFrame", nullptr);
shutdown();
exit(2);
}
engine = std::make_unique<Game_engine>(org_name, app_name, 0, 1, debug, argc, argv, env);
if(argc > 1 && argv[1] == "test"s)
engine->screens().enter<Test_screen>();
else
engine->screens().enter<Test_screen>();
}
void shutdown() {
try {
engine.reset();
void onFrame() { engine->on_frame(); }
} catch(const util::Error& ex) {
MIRRAGE_CRASH_REPORT("Exception in shutdown: " << ex.what());
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Sorry :-(", "Error in shutdown", nullptr);
exit(3);
}
}
void shutdown() { engine.reset(); }
} // namespace
......@@ -14,6 +14,7 @@
#include <mirrage/input/events.hpp>
#include <mirrage/input/input_manager.hpp>
#include <mirrage/translations.hpp>
#include <mirrage/utils/log.hpp>
#include <mirrage/utils/units.hpp>
#include <glm/glm.hpp>
......@@ -89,7 +90,7 @@ namespace mirrage {
_engine.screens().leave();
}
break;
case "fast_quit"_strid: std::terminate(); break;
case "fast_quit"_strid: std::quick_exit(0);
case "create"_strid:
_meta_system.entities().emplace("cube").get<Transform_comp>().process(
[&](auto& transform) {
......@@ -108,13 +109,15 @@ namespace mirrage {
case "print"_strid: {
auto cam = _camera.get<Transform_comp>().get_or_throw().position();
MIRRAGE_INFO("Setup: \n"
<< " Camera position: " << cam.x << "/" << cam.y << "/" << cam.z << "\n"
<< " Camera orientation: " << _cam_yaw << "/" << _cam_pitch << "\n"
<< " Sun orientation: " << _sun_elevation << "/" << _sun_azimuth << "\n"
<< " Sun color: " << _sun_color_temperature << "\n"
<< " Disected: "
<< _meta_system.renderer().settings().debug_disect);
LOG(plog::info) << "Setup: \n"
<< " Camera position: " << cam.x << "/" << cam.y << "/" << cam.z
<< "\n"
<< " Camera orientation: " << _cam_yaw << "/" << _cam_pitch << "\n"
<< " Sun orientation: " << _sun_elevation << "/" << _sun_azimuth
<< "\n"
<< " Sun color: " << _sun_color_temperature << "\n"
<< " Disected: "
<< _meta_system.renderer().settings().debug_disect;
break;
}
......@@ -138,7 +141,7 @@ namespace mirrage {
});
break;
case "pause"_strid:
MIRRAGE_INFO("Pause/Unpause playback");
LOG(plog::debug) << "Pause/Unpause playback";
_meta_system.nims().toggle_pause();
break;
......@@ -559,7 +562,6 @@ namespace mirrage {
auto print_entry =
[&](auto&& printer, const Profiler_result& result, int depth = 0, int rank = -1) -> void {
auto color = [&] {
switch(rank) {
case 0: return nk_rgb(255, 0, 0);
......
......@@ -7,6 +7,8 @@
#include <mirrage/utils/stacktrace.hpp>
#include <mirrage/utils/string_utils.hpp>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <plog/Log.h>
#include <gsl/gsl>
#include <string>
......@@ -14,17 +16,19 @@
using namespace mirrage;
auto extract_arg(std::vector<std::string>& args, const std::string& key)
-> util::maybe<std::string>;
auto extract_arg(std::vector<std::string>& args, const std::string& key) -> util::maybe<std::string>;
// ./mesh_converter sponza.obj
// ./mesh_converter --output=/foo/bar sponza.obj
int main(int argc, char** argv) {
mirrage::util::init_stacktrace(argv[0]);
static auto fileAppender =
plog::RollingFileAppender<plog::TxtFormatter>("mesh_converter.log", 4L * 1024L, 4);
static auto consoleAppender = plog::ColorConsoleAppender<plog::TxtFormatter>();
plog::init(plog::debug, &fileAppender).addAppender(&consoleAppender);
if(argc < 1) {
std::cerr << "Too few arguments!\n"
<< "Usage ./mesh_converter [--output=DIR] INPUT [...]" << std::endl;
LOG(plog::error) << "Too few arguments!\n"
<< "Usage ./mesh_converter [--output=DIR] INPUT [...]";
return 1;
}
......@@ -42,10 +46,9 @@ int main(int argc, char** argv) {
}
}
auto extract_arg(std::vector<std::string>& args, const std::string& key)
-> util::maybe<std::string> {
auto found = std::find_if(
args.begin(), args.end(), [&](auto& str) { return util::starts_with(str, key); });
auto extract_arg(std::vector<std::string>& args, const std::string& key) -> util::maybe<std::string> {
auto found =
std::find_if(args.begin(), args.end(), [&](auto& str) { return util::starts_with(str, key); });
if(found == args.end())
return mirrage::util::nothing;
......
......@@ -26,12 +26,8 @@ namespace mirrage {
std::string material_id;
Sub_mesh_data() = default;
Sub_mesh_data(std::uint32_t index_offset,
std::uint32_t index_count,
std::string material_id)
: index_offset(index_offset)
, index_count(index_count)
, material_id(std::move(material_id)) {}
Sub_mesh_data(std::uint32_t index_offset, std::uint32_t index_count, std::string material_id)
: index_offset(index_offset), index_count(index_count), material_id(std::move(material_id)) {}
};
auto last_of(const std::string& str, char c) {
......@@ -71,7 +67,7 @@ namespace mirrage {
void convert_model(const std::string& path, const std::string& output) {
MIRRAGE_INFO("Convert model \"" << path << "\" with output directory \"" << output << "\"");
LOG(plog::info) << "Convert model \"" << path << "\" with output directory \"" << output << "\"";
auto base_dir = extract_dir(path);
auto model_name = extract_file_name(path);
......@@ -83,28 +79,25 @@ namespace mirrage {
aiProcess_JoinIdenticalVertices | aiProcess_GenSmoothNormals | aiProcess_Triangulate
| aiProcess_PreTransformVertices | aiProcess_ImproveCacheLocality
| aiProcess_RemoveRedundantMaterials | aiProcess_OptimizeMeshes
| aiProcess_ValidateDataStructure | aiProcess_FlipUVs
| aiProcess_FixInfacingNormals);
| aiProcess_ValidateDataStructure | aiProcess_FlipUVs | aiProcess_FixInfacingNormals);
MIRRAGE_INVARIANT(scene,
"Unable to load model '" << path << "': " << importer.GetErrorString());
MIRRAGE_INVARIANT(scene, "Unable to load model '" << path << "': " << importer.GetErrorString());
// load materials
auto materials =
gsl::span<const aiMaterial* const>(scene->mMaterials, scene->mNumMaterials);
auto materials = gsl::span<const aiMaterial* const>(scene->mMaterials, scene->mNumMaterials);
auto loaded_material_ids = std::vector<util::maybe<std::string>>();
loaded_material_ids.reserve(materials.size());
for(auto& mat : materials) {
aiString name;
if(mat->Get(AI_MATKEY_NAME, name) != aiReturn_SUCCESS) {
MIRRAGE_WARN("material number " << loaded_material_ids.size() << " has no name!");
LOG(plog::warning) << "material number " << loaded_material_ids.size() << " has no name!";
loaded_material_ids.emplace_back(util::nothing);
continue;
}
auto mat_id = model_name + "_" + name.C_Str();
if(!convert_material(mat_id, *mat, base_dir, output)) {
MIRRAGE_WARN("Unable to parse material \"" << name.C_Str() << "\"!");
LOG(plog::warning) << "Unable to parse material \"" << name.C_Str() << "\"!";
loaded_material_ids.emplace_back(util::nothing);
continue;
}
......@@ -144,10 +137,9 @@ namespace mirrage {
if(mesh->mNumFaces > 0) {
auto& mat = loaded_material_ids.at(mesh->mMaterialIndex);
if(mat.is_some()) {
sub_meshes.emplace_back(
indices.size(), mesh->mNumFaces * 3, mat.get_or_throw());
sub_meshes.emplace_back(indices.size(), mesh->mNumFaces * 3, mat.get_or_throw());
} else {
MIRRAGE_WARN("Required material is missing/defect!");
LOG(plog::warning) << "Required material is missing/defect!";
}
}
......@@ -161,15 +153,13 @@ namespace mirrage {
// write file
auto model_out_filename = output + "/models/" + model_name + ".mmf";
auto model_out_file =
std::ofstream(model_out_filename, std::ostream::binary | std::ostream::trunc);
auto model_out_file = std::ofstream(model_out_filename, std::ostream::binary | std::ostream::trunc);
MIRRAGE_INVARIANT(model_out_file.is_open(),
"Unable to open output file \"" << model_out_filename << "\"!");
auto header = renderer::Model_file_header();
header.vertex_count =
gsl::narrow<std::uint32_t>(vertices.size() * sizeof(renderer::Model_vertex));
auto header = renderer::Model_file_header();
header.vertex_count = gsl::narrow<std::uint32_t>(vertices.size() * sizeof(renderer::Model_vertex));
header.index_count = gsl::narrow<std::uint32_t>(indices.size() * sizeof(std::uint32_t));
header.submesh_count = gsl::narrow<std::uint32_t>(sub_meshes.size());
......@@ -195,6 +185,6 @@ namespace mirrage {
// write footer
write(model_out_file, renderer::Model_file_header::type_tag_value);
MIRRAGE_INFO("Done.");
LOG(plog::info) << "Done.";
}
} // namespace mirrage
......@@ -11,9 +11,9 @@
#include <mirrage/asset/error.hpp>
#include <mirrage/asset/stream.hpp>
#include <mirrage/utils/log.hpp>
#include <mirrage/utils/maybe.hpp>
#include <mirrage/utils/reflection.hpp>
#include <mirrage/utils/stacktrace.hpp>
#include <mirrage/utils/string_utils.hpp>
#include <mirrage/utils/template_utils.hpp>
......@@ -44,9 +44,10 @@ namespace mirrage::asset {
}
extern std::string pwd();
extern std::string write_dir(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name);
extern void setup_storage();
extern bool storage_ready();
template <class R>
class Ptr {
......
......@@ -9,8 +9,8 @@
#include <mirrage/asset/aid.hpp>
#include <mirrage/utils/log.hpp>
#include <mirrage/utils/maybe.hpp>
#include <mirrage/utils/stacktrace.hpp>
#include <mirrage/utils/template_utils.hpp>
#include <gsl/gsl>
......@@ -97,9 +97,8 @@ namespace mirrage::asset {
sf2::deserialize_json(in,
[&](auto& msg, uint32_t row, uint32_t column) {
MIRRAGE_ERROR("Error parsing JSON from " << in.aid().str() << " at "
<< row << ":" << column << ": "
<< msg);
LOG(plog::error) << "Error parsing JSON from " << in.aid().str()
<< " at " << row << ":" << column << ": " << msg;
},
r);
......
......@@ -18,9 +18,6 @@
#include <unistd.h>
#endif
#ifdef EMSCRIPTEN
#include <emscripten.h>
#endif
using namespace mirrage::util;
using namespace std::string_literals;
......@@ -79,9 +76,8 @@ namespace {
auto&& file = std::get<1>(spr);
auto wildcard = last_of(file, '*').get_or(file.length());
if(wildcard != (file.find_first_of('*') + 1)) {
MIRRAGE_WARN("More than one wildcard ist currently not supported. Found in: " << wildcard_path);
}
LOG_IF(plog::warning, wildcard != file.find_first_of('*') + 1)
<< "More than one wildcard ist currently not supported. Found in: " << wildcard_path;
auto prefix = file.substr(0, wildcard - 1);
auto suffix = wildcard < file.length() ? file.substr(0, wildcard - 1) : std::string();
......@@ -115,19 +111,26 @@ namespace {
return stat.filetype == PHYSFS_FILETYPE_DIRECTORY;
}
template <typename Stream>
void print_dir_recursiv(const std::string& dir, uint8_t depth, Stream& stream) {
template <typename Callback>
void print_dir_recursiv(const std::string& dir, uint8_t depth, Callback&& callback) {
std::string p;
for(uint8_t i = 0; i < depth; i++)
p += " ";
stream << p << dir << "\n";
callback(p + dir);
depth++;
for(auto&& f : list_files(dir, "", "")) {
if(depth >= 5)
stream << p << " " << f << "\n";
callback(p + " " + f);
else
print_dir_recursiv(f, depth, stream);
print_dir_recursiv(f, depth, callback);
}
}
void init_physicsfs(const std::string& exe_name) {
if(!PHYSFS_isInit() && !PHYSFS_init(exe_name.empty() ? nullptr : exe_name.c_str())) {
throw std::system_error(static_cast<mirrage::asset::Asset_error>(PHYSFS_getLastErrorCode()),
"Unable to initalize PhysicsFS.");
}
}
......@@ -149,53 +152,25 @@ namespace mirrage::asset {
return cCurrentPath;
}
std::string write_dir(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name) {
init_physicsfs(exe_name);
if(exists_dir("write_dir")) {
return "write_dir";
}
#ifdef EMSCRIPTEN
static bool initial_sync_done = false;
extern "C" void EMSCRIPTEN_KEEPALIVE post_sync_handler() { initial_sync_done = true; }
void setup_storage() {
EM_ASM(FS.mkdir('/persistent_data'); FS.mount(IDBFS, {}, '/persistent_data');
Module.syncdone = 0;
//populate persistent_data directory with existing persistent source data
//stored with Indexed Db
//first parameter = "true" mean synchronize from Indexed Db to
//Emscripten file system,
// "false" mean synchronize from Emscripten file system to Indexed Db
//second parameter = function called when data are synchronized
FS.syncfs(true, function(err) {
//assert(!err);
Module.print("end file sync..");
Module.syncdone = 1;
ccall('post_sync_handler', 'v');
}););
return PHYSFS_getPrefDir(org_name.c_str(), app_name.c_str());
}
bool storage_ready() { return initial_sync_done; }
#else
void setup_storage() {}
bool storage_ready() { return true; }
#endif
Asset_manager::Asset_manager(const std::string& exe_name,
const std::string& org_name,
const std::string& app_name) {
if(!PHYSFS_init(exe_name.empty() ? nullptr : exe_name.c_str()))
throw std::system_error(static_cast<Asset_error>(PHYSFS_getLastErrorCode()),
"Unable to initalize PhysicsFS.");
auto write_dir = PHYSFS_getPrefDir(org_name.c_str(), app_name.c_str());
init_physicsfs(exe_name);
#ifdef EMSCRIPTEN
MIRRAGE_INVARIANT(storage_ready(), "Storage is not ready");
write_dir = "/persistent_data";
#endif
auto write_dir = ::mirrage::asset::write_dir(exe_name, org_name, app_name);