23 #include <engine/config.hpp>
34 int main(
int argc,
char* argv[])
37 const auto launch_time = std::chrono::system_clock::now();
46 [&launch_time](
const auto&
event)
48 static const char* severities[] =
58 static const std::string colors[] =
70 "[{:8.03f}] {}{}: {}:{}:{}: {}{}\n",
71 std::chrono::duration<float>(
event.time - launch_time).count(),
72 colors[
static_cast<int>(
event.severity)],
74 static_cast<int>(
event.severity),
75 std::filesystem::path(
event.location.file_name()).filename().string(),
76 event.location.line(),
77 event.location.column(),
85 const std::filesystem::path log_archive_path =
get_shared_config_path() / config::application_name /
"logs";
88 bool log_archive_exists =
false;
92 if (std::filesystem::create_directories(log_archive_path))
102 std::set<std::filesystem::path> log_archive;
103 for (
const auto& entry: std::filesystem::directory_iterator{log_archive_path})
105 if (entry.is_regular_file() &&
106 entry.path().extension() ==
".log")
108 log_archive.emplace(entry.path());
112 debug::log_debug(
"Detected {} archived log{} at \"{}\"", log_archive.size(), log_archive.size() != 1 ?
"s" :
"", log_archive_path.string());
115 if (!log_archive.empty())
117 for (std::size_t i = log_archive.size() + 1; i > config::debug_log_archive_capacity; --i)
119 std::filesystem::remove(*log_archive.begin());
120 debug::log_debug(
"Deleted expired log file \"{}\"", log_archive.begin()->string());
121 log_archive.erase(log_archive.begin());
125 catch (
const std::filesystem::filesystem_error&
e)
127 debug::log_error(
"An error occured while cleaning the log archive \"{}\": {}", log_archive_path.string(),
e.what());
131 log_archive_exists =
true;
133 catch (
const std::filesystem::filesystem_error&
e)
135 debug::log_error(
"Failed to create log archive at \"{}\": {}", log_archive_path.string(),
e.what());
139 std::shared_ptr<event::subscription> log_to_file_subscription;
140 std::filesystem::path log_filepath;
141 if (config::debug_log_archive_capacity && log_archive_exists)
144 const auto time = std::chrono::floor<std::chrono::seconds>(launch_time);
145 const std::string log_filename =
std::format(
"{0}-{1:%Y%m%d}T{1:%H%M%S}Z.log", config::application_slug, time);
148 log_filepath = log_archive_path / log_filename;
149 const std::string log_filepath_string = log_filepath.string();
150 auto log_filestream = std::make_shared<std::ofstream>(log_filepath);
152 if (log_filestream->is_open())
157 (*log_filestream) <<
"time\tfile\tline\tcolumn\tseverity\tmessage";
159 if (log_filestream->good())
164 [&launch_time, log_filestream](
const auto&
event)
168 "\n{:.03f}\t{}\t{}\t{}\t{}\t{}",
169 std::chrono::duration<float>(
event.time - launch_time).count(),
170 std::filesystem::path(
event.location.file_name()).filename().string(),
171 event.location.line(),
172 event.location.column(),
173 static_cast<int>(
event.severity),
181 log_to_cout_subscription->unsubscribe();
186 debug::log_error(
"Failed to write to log file \"{}\"", log_filepath_string);
196 debug::log_info(
"{0} {1}; {2:%Y%m%d}T{2:%H%M%S}Z", config::application_name, config::application_version_string, std::chrono::floor<std::chrono::milliseconds>(launch_time));
206 catch (
const std::exception&
e)
209 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Error",
std::format(
"Unhandled exception: {}",
e.what()).c_str(),
nullptr);
::event::channel< message_logged_event > & get_message_logged_channel() noexcept
Returns the channel through which message logged events are published.
void execute()
Executes the game.
int main(int argc, char *argv[])
const char * fg_bright_blue
const char * fg_bright_green
const char * bg_bright_red
void enable_vt100()
Enables VT100 virtual terminal sequences.
void enable_utf8()
Enables UTF-8 output.
log_message< log_message_severity::fatal, Args... > log_fatal
Formats and logs a fatal error message.
log_message< log_message_severity::debug, Args... > log_debug
Formats and logs a debug message.
log_message< log_message_severity::error, Args... > log_error
Formats and logs an error message.
logger & default_logger() noexcept
Returns the default logger.
log_message< log_message_severity::info, Args... > log_info
Formats and logs an info message.
Publish-subscribe messaging.
format
Image and vertex formats.
std::filesystem::path get_shared_config_path()
Returns the absolute path to the directory containing user-specific application data that may be shar...