Skip to content

Commit

Permalink
Add ability to set log flush level to help debug crashes. Some tidyin…
Browse files Browse the repository at this point in the history
…g up of settings reading code. Should help with Github #97
  • Loading branch information
cunnane committed Jun 9, 2024
1 parent a6ac24e commit 92fdeb4
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 61 deletions.
12 changes: 4 additions & 8 deletions src/xlOil-XLL/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace fs = std::filesystem;
namespace xloil
{
std::shared_ptr<spdlog::logger> loggerInitialise(
const char* debugStringLevel,
const std::string_view& debugLevel,
bool makeDefault)
{
const auto debugWriterLevel = spdlog::level::from_str(debugStringLevel);
const auto debugWriterLevel = spdlog::level::from_str(string(debugLevel));

auto logger = make_shared<spdlog::logger>("logger");

Expand All @@ -44,14 +44,10 @@ namespace xloil

void loggerSetFlush(
const std::shared_ptr<spdlog::logger>& logger,
const char* flushLevel,
bool flushAfterCalc)
const std::string_view& flushLevel)
{
const auto flushSpdLevel = spdlog::level::from_str(flushLevel);
const auto flushSpdLevel = spdlog::level::from_str(string(flushLevel));
logger->flush_on(flushSpdLevel);

if (flushAfterCalc)
Event::AfterCalculate() += [logger]() { logger->flush(); };
}

void loggerAddPopupWindowSink(
Expand Down
11 changes: 8 additions & 3 deletions src/xlOil-XLL/LogWindowSink.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include "LogWindowSink.h"
#include <xloil/WindowsSlim.h>
#include <xloil/StringUtils.h>
#include <xlOilHelpers/Environment.h>
#include <xlOilHelpers/Exception.h>
#include <xlOil/LogWindow.h>
#include <spdlog/sinks/base_sink.h>
#include <spdlog/details/pattern_formatter.h>
#include <spdlog/spdlog.h>
#include <mutex>
#include <regex>


using std::wstring;
using std::string;
using std::shared_ptr;
Expand Down Expand Up @@ -165,8 +169,9 @@ namespace xloil
theLogWindow->openWindow();
}

void setLogWindowPopupLevel(spdlog::level::level_enum popupLevel)
void setLogWindowPopupLevel(const char* popupLevel)
{
MainLogWindow::setPopupLevel(popupLevel);
const auto spdLevel = spdlog::level::from_str(popupLevel);
MainLogWindow::setPopupLevel(spdLevel);
}
}
7 changes: 5 additions & 2 deletions src/xlOil-XLL/LogWindowSink.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once
#include <spdlog/spdlog.h>
#include <memory>
#include <xloil/WindowsSlim.h>
namespace spdlog { namespace sinks { class sink; } }

namespace xloil
{
std::shared_ptr<spdlog::sinks::sink>
Expand All @@ -8,5 +11,5 @@ namespace xloil
HINSTANCE parentInstance);

void openLogWindow();
void setLogWindowPopupLevel(spdlog::level::level_enum popupLevel);
void setLogWindowPopupLevel(const char* popupLevel);
}
23 changes: 12 additions & 11 deletions src/xlOil/Loaders/AddinLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ namespace xloil
}
else
{
auto addinRoot = (*settings)[XLOIL_SETTINGS_ADDIN_SECTION];

// Log file settings
logFile = Settings::logFilePath(*settings);
auto logLevel = Settings::logLevel(addinRoot);
auto [logMaxSize, logNumFiles] = Settings::logRotation(addinRoot);
auto logLevel = Settings::logLevel(*settings);
auto [logMaxSize, logNumFiles] = Settings::logRotation(*settings);

logFile = loggerAddRotatingFileSink(spdlog::default_logger(),
logFile.c_str(), logLevel.c_str(),
logMaxSize, logNumFiles);

logFile = loggerAddRotatingFileSink(
spdlog::default_logger(),
logFile.c_str(), logLevel.c_str(),
logMaxSize, logNumFiles);
loggerSetFlush(spdlog::default_logger(),
Settings::logFlushLevel(*settings));

// Write the log message *after* we set up the log file!
XLO_INFO(L"Found core settings file '{}' for '{}'",
Expand All @@ -63,11 +63,10 @@ namespace xloil
// If this is specified in multiple addins and/or the core,
// the last value overrides: not easy to workaround
setLogWindowPopupLevel(
spdlog::level::from_str(
Settings::logPopupLevel(addinRoot).c_str()));
Settings::logPopupLevel(*settings).c_str());

// Add any requested date formats
auto dateFormats = Settings::dateFormats(addinRoot);
auto dateFormats = Settings::dateFormats(*settings);
for (auto& form : dateFormats)
theDateTimeFormats().push_back(form);
}
Expand Down Expand Up @@ -107,6 +106,8 @@ namespace xloil
coreDll,
lastSlash ? lastSlash + 1 : pathName,
wcslen(coreDll) - 3);

XLO_DEBUG(L"Creating addin context for '{}'", pathName);
if (isCore)
{
auto context = createCoreAddinContext();
Expand Down
60 changes: 36 additions & 24 deletions src/xlOil/Loaders/CoreEntryPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ using std::wstring;
using std::string;
using std::vector;
using std::shared_ptr;
using std::tuple;

namespace
{
Expand All @@ -45,18 +46,21 @@ namespace xloil
if (!theCoreIsLoaded)
{
// There's no log file until createAddinContext figures out our
// settings, so any logging goes to the debug output.
auto logger = loggerInitialise("debug");
loggerSetFlush(logger, "warning", true);

Environment::setCoreHandle(theCoreModuleHandle);

// settings, so any logging goes to the debug output. We also flush
// on trace level so we don't miss any crashes during startup. This
// has a minimal performance impact vs flushing during sheet calc.
auto logger = loggerInitialise("trace");
loggerSetFlush(logger, "trace");
initMessageQueue(Environment::excelProcess().hInstance);

XLO_DEBUG(L"Loaded xlOil core from: {}", Environment::coreDllPath());

loggerAddPopupWindowSink(logger);

// Flush logger after sheet calculates
Event::AfterCalculate() += [logger]() { logger->flush(); };

// Run before staticSource so the function registration gets picked up
registerIntellisenseHook(xllPath);

Expand All @@ -79,27 +83,35 @@ namespace xloil
retVal = 1;
}

// Check if we should process the settings for a non-core addin first
// and/or we need to load the core addin. We also check we don't call
// loadPluginsForAddin twice (although it would be harmless)
const bool loadBeforeCore = Settings::loadBeforeCore(*addinContext->settings());
const auto loadCoreContext = theCoreIsLoaded || addinContext == coreContext
? nullptr
: coreContext;

auto firstLoad = loadBeforeCore ? addinContext : loadCoreContext;
auto secondLoad = !loadBeforeCore ? addinContext : loadCoreContext;

// Although we are on the main thread, Excel's COM interface may not
// be ready yet. Plugins may use that interface so we delay load them.
runComSetupOnXllOpen([=]()
{
if (firstLoad)
firstLoad->loadPlugins();
if (secondLoad)
secondLoad->loadPlugins();
});

if (addinContext == coreContext || theCoreIsLoaded)
{
runComSetupOnXllOpen([=]()
{
addinContext->loadPlugins();
});
}
else
{
// Check if we should process the settings for a non-core addin first
// and/or we need to load the core addin. We also check we don't call
// loadPluginsForAddin twice (although it would be harmless)
const bool loadBeforeCore = Settings::loadBeforeCore(*addinContext->settings());

const auto [firstLoad, secondLoad] = loadBeforeCore
? tuple(addinContext, coreContext)
: tuple(coreContext, addinContext);

runComSetupOnXllOpen([=]()
{
if (firstLoad)
firstLoad->loadPlugins();
if (secondLoad)
secondLoad->loadPlugins();
});
}
theCoreIsLoaded = true;
return retVal;
}
Expand Down
27 changes: 18 additions & 9 deletions src/xlOilHelpers/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,34 @@ namespace xloil
? utf8ToUtf16(found)
: fs::path(utf8ToUtf16(*root.source().path)).replace_extension("log").wstring();
}
std::string logLevel(const toml::view_node& root)
std::string logLevel(const toml::table& root)
{
return findStr(root, "LogLevel", "warn");
auto addinRoot = root[XLOIL_SETTINGS_ADDIN_SECTION];
return findStr(addinRoot, "LogLevel", "warn");
}
std::string logPopupLevel(const toml::view_node& root)
std::string logPopupLevel(const toml::table& root)
{
return findStr(root, "LogPopupLevel", "error");
auto addinRoot = root[XLOIL_SETTINGS_ADDIN_SECTION];
return findStr(addinRoot, "LogPopupLevel", "error");
}
std::pair<size_t, size_t> Settings::logRotation(const toml::view_node& root)
std::string logFlushLevel(const toml::table& root)
{
auto addinRoot = root[XLOIL_SETTINGS_ADDIN_SECTION];
return findStr(addinRoot, "LogFlushLevel", "warning");
}
std::pair<size_t, size_t> Settings::logRotation(const toml::table& root)
{
auto addinRoot = root[XLOIL_SETTINGS_ADDIN_SECTION];
// (size_t) cast needed for 32-bit as TOML lib is hard-coded to
// return int64 for all integer types
return std::make_pair(
(size_t)root["LogMaxSize"].value_or(1024u),
(size_t)root["LogNumberOfFiles"].value_or(2u));
(size_t)addinRoot["LogMaxSize"].value_or(1024u),
(size_t)addinRoot["LogNumberOfFiles"].value_or(2u));
}
std::vector<std::wstring> dateFormats(const toml::view_node& root)
std::vector<std::wstring> dateFormats(const toml::table& root)
{
return findVecStr(root, "DateFormats");
auto addinRoot = root[XLOIL_SETTINGS_ADDIN_SECTION];
return findVecStr(addinRoot, "DateFormats");
}

namespace
Expand Down
10 changes: 6 additions & 4 deletions src/xlOilHelpers/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ namespace xloil
{
std::wstring logFilePath(const toml::table& root);

std::string logLevel(const toml::view_node& root);
std::string logLevel(const toml::table& root);

std::string logPopupLevel(const toml::view_node& root);
std::string logPopupLevel(const toml::table& root);

std::pair<size_t, size_t> logRotation(const toml::view_node& root);
std::string logFlushLevel(const toml::table& root);

std::pair<size_t, size_t> logRotation(const toml::table& root);

std::vector<std::wstring> plugins(const toml::view_node& root);

std::wstring pluginSearchPattern(const toml::view_node& root);

std::vector<std::wstring> dateFormats(const toml::view_node& root);
std::vector<std::wstring> dateFormats(const toml::table& root);

std::vector<std::pair<std::wstring, std::wstring>>
environmentVariables(const toml::view_node& root);
Expand Down

0 comments on commit 92fdeb4

Please sign in to comment.