-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
61 changed files
with
12,185 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,79 @@ | ||
#include "stdafx.h" | ||
#include "Logger.h" | ||
#include <sstream> // stringstream | ||
#include <shlobj.h> // for SHGetFolderPath | ||
#include <string> | ||
#include <memory> | ||
#include <locale> // wstring_convert | ||
|
||
#ifndef LOGGER_FILENAME | ||
#define LOGGER_FILENAME "cpythia.log" | ||
#endif | ||
std::shared_ptr<spdlog::logger> getFallbackLogger() | ||
{ | ||
constexpr char *fallbackLoggerName = "Fallback_stderr"; | ||
spdlog::drop(fallbackLoggerName); | ||
return spdlog::stderr_logger_mt(fallbackLoggerName); | ||
} | ||
|
||
std::string Logger::makeFilename() | ||
void createLogger(std::string loggerName, spdlog::filename_t loggerFile) | ||
{ | ||
CHAR buffer[MAX_PATH]; | ||
|
||
if (SUCCEEDED(SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, buffer))) | ||
try | ||
{ | ||
spdlog::set_level(spdlog::level::debug); | ||
spdlog::set_sync_mode(); | ||
Logger::logfile = spdlog::rotating_logger_mt(loggerName, loggerFile, 1024 * 1024 * 10, 3); | ||
} | ||
catch (const spdlog::spdlog_ex& ex) | ||
{ | ||
// Append product-specific path | ||
// TODO: Implement a better way of initializing the logger | ||
return std::string(buffer) + "\\Arma 3\\" + LOGGER_FILENAME; | ||
Logger::logfile = getFallbackLogger(); | ||
LOG_ERROR(std::string("Could not create regular logger! ") + ex.what()); | ||
} | ||
} | ||
|
||
return LOGGER_FILENAME; | ||
// Do NOT call this function in dllmain.cpp!!! | ||
// It creates new threads which is forbidden while attaching the dll | ||
void switchToAsyncLogger(std::string loggerName, spdlog::filename_t loggerFile) | ||
{ | ||
LOG_INFO("Switching to asynchronous logger..."); | ||
LOG_FLUSH(); | ||
Logger::logfile = nullptr; | ||
spdlog::drop(loggerName); | ||
|
||
try | ||
{ | ||
spdlog::set_level(spdlog::level::debug); | ||
spdlog::set_async_mode(131072, spdlog::async_overflow_policy::block_retry, | ||
nullptr, | ||
std::chrono::milliseconds(500)); | ||
|
||
Logger::logfile = spdlog::rotating_logger_mt(loggerName, loggerFile, 1024 * 1024 * 10, 3); | ||
LOG_INFO("Switching to asynchronous logger done!"); | ||
} | ||
catch (const spdlog::spdlog_ex& ex) | ||
{ | ||
// Try creating the regular logger again. Might work. | ||
try | ||
{ | ||
createLogger(loggerName, loggerFile); | ||
} | ||
catch (const spdlog::spdlog_ex& ex) | ||
{ | ||
Logger::logfile = getFallbackLogger(); | ||
LOG_ERROR(std::string("Could not create regular logger! ") + ex.what()); | ||
} | ||
|
||
LOG_ERROR(std::string("Could not create asynchronous logger! ") + ex.what()); | ||
} | ||
} | ||
|
||
namespace Logger | ||
{ | ||
std::string w2s(const std::wstring &var) | ||
{ | ||
static std::locale loc(""); | ||
auto &facet = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc); | ||
return std::wstring_convert<std::remove_reference<decltype(facet)>::type, wchar_t>(&facet).to_bytes(var); | ||
} | ||
|
||
std::wstring s2w(const std::string &var) | ||
{ | ||
static std::locale loc(""); | ||
auto &facet = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc); | ||
return std::wstring_convert<std::remove_reference<decltype(facet)>::type, wchar_t>(&facet).from_bytes(var); | ||
} | ||
} |
Oops, something went wrong.