Skip to content

Commit

Permalink
instantiate log once instead of every time a browser is created. Allo…
Browse files Browse the repository at this point in the history
…w to disable logs from bling.ini
  • Loading branch information
lurume84 committed Apr 24, 2020
1 parent 72ffddc commit f3030e4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
18 changes: 11 additions & 7 deletions src/DesktopApp/main.cc
Expand Up @@ -31,6 +31,7 @@
#include "DesktopCore\Blink\Agents\LiveViewAgent.h"
#include "DesktopCore\Blink\Agents\ActivityAgent.h"
#include "DesktopCore\System\Agents\LogAgent.h"
#include "DesktopCore\System\Services\LogService.h"
#include "Services\DownloadViewerService.h"
#include "Services\DownloadDesktopService.h"

Expand Down Expand Up @@ -147,21 +148,24 @@ int RunMain(HINSTANCE hInstance, int nCmdShow)

desktop::core::DesktopCore core;

core.initialize();

core.addAgent(std::make_unique<desktop::core::agent::LogAgent>());
core.addAgent(std::make_unique<desktop::core::agent::SyncVideoAgent>());
core.addAgent(std::make_unique<desktop::core::agent::SyncThumbnailAgent>());
core.addAgent(std::make_unique<desktop::core::agent::LiveViewAgent>());
core.addAgent(std::make_unique<desktop::core::agent::FileServerAgent>());

desktop::core::utils::patterns::Subscriber subscriber;
subscriber.subscribe([&core, &subscriber](const desktop::core::utils::patterns::Event& rawEvt)
{
const auto& evt = static_cast<const desktop::ui::events::BrowserCreatedEvent&>(rawEvt);

auto &browser = evt.m_browser;

desktop::core::service::LogService::info("Browser instance created");

core.initialize();

core.addAgent(std::make_unique<desktop::core::agent::LogAgent>());
core.addAgent(std::make_unique<desktop::core::agent::UpgradeViewerAgent>(std::make_unique<desktop::ui::service::DownloadViewerService>(browser)));
core.addAgent(std::make_unique<desktop::core::agent::SyncVideoAgent>());
core.addAgent(std::make_unique<desktop::core::agent::SyncThumbnailAgent>());
core.addAgent(std::make_unique<desktop::core::agent::LiveViewAgent>());
core.addAgent(std::make_unique<desktop::core::agent::FileServerAgent>());
core.addAgent(std::make_unique<desktop::core::agent::UpgradeDesktopAgent>(std::make_unique<desktop::ui::service::DownloadDesktopService>(browser)));

subscriber.unsubscribe(desktop::ui::events::BROWSER_CREATED_EVENT);
Expand Down
22 changes: 14 additions & 8 deletions src/DesktopCore/System/Agents/LogAgent.cpp
Expand Up @@ -9,20 +9,26 @@

namespace desktop { namespace core { namespace agent {

LogAgent::LogAgent(std::unique_ptr<service::ApplicationDataService> applicationService)
LogAgent::LogAgent(std::unique_ptr<service::ApplicationDataService> applicationService, std::unique_ptr<core::service::IniFileService> iniFileService)
: m_applicationService(std::move(applicationService))
, m_iniFileService(std::move(iniFileService))
{
auto documents = m_applicationService->getMyDocuments();

boost::filesystem::create_directories(documents + "Logs");
auto logFiles = m_iniFileService->get<bool>(documents + "Bling.ini", "Log", "Enabled", true);

spdlog::init_thread_pool(8192, 1);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(documents + "Logs\\Bling.log", 1024 * 1024 * 10, 1, true);
std::vector<spdlog::sink_ptr> sinks{ stdout_sink, rotating_sink };
if (logFiles)
{
boost::filesystem::create_directories(documents + "Logs");

m_logger = std::make_shared<spdlog::async_logger>("BlingLog", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
spdlog::init_thread_pool(8192, 1);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(documents + "Logs\\Bling.log", 1024 * 1024 * 10, 1, true);
std::vector<spdlog::sink_ptr> sinks{ stdout_sink, rotating_sink };

spdlog::register_logger(m_logger);
m_logger = std::make_shared<spdlog::async_logger>("BlingLog", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);

spdlog::register_logger(m_logger);
}
}
}}}
6 changes: 5 additions & 1 deletion src/DesktopCore/System/Agents/LogAgent.h
Expand Up @@ -3,6 +3,8 @@
#include "../../Model/IAgent.h"
#include "../../System/Services/ApplicationDataService.h"

#include "DesktopCore\System\Services\IniFileService.h"

namespace spdlog
{
class logger;
Expand All @@ -12,10 +14,12 @@ namespace desktop { namespace core { namespace agent {
class LogAgent : public model::IAgent
{
public:
LogAgent(std::unique_ptr<service::ApplicationDataService> applicationService = std::make_unique<service::ApplicationDataService>());
LogAgent(std::unique_ptr<service::ApplicationDataService> applicationService = std::make_unique<service::ApplicationDataService>(),
std::unique_ptr<core::service::IniFileService> iniFileService = std::make_unique<core::service::IniFileService>());
~LogAgent() = default;
private:
std::unique_ptr<service::ApplicationDataService> m_applicationService;
std::shared_ptr<spdlog::logger> m_logger;
std::unique_ptr<core::service::IniFileService> m_iniFileService;
};
}}}
20 changes: 16 additions & 4 deletions src/DesktopCore/System/Services/LogService.h
Expand Up @@ -13,25 +13,37 @@ namespace desktop { namespace core { namespace service {
template<typename... Args>
static inline void info(const std::string& fmt, const Args &... args)
{
spdlog::get("BlingLog")->info(fmt, args...);
if(spdlog::get("BlingLog") != nullptr)
{
spdlog::get("BlingLog")->info(fmt, args...);
}
}

template<typename... Args>
static inline void error(const std::string& fmt, const Args &... args)
{
spdlog::get("BlingLog")->error(fmt, args...);
if (spdlog::get("BlingLog") != nullptr)
{
spdlog::get("BlingLog")->error(fmt, args...);
}
}

template<typename... Args>
static inline void warn(const std::string& fmt, const Args &... args)
{
spdlog::get("BlingLog")->warn(fmt, args...);
if (spdlog::get("BlingLog") != nullptr)
{
spdlog::get("BlingLog")->warn(fmt, args...);
}
}

template<typename... Args>
static inline void critical(const std::string& fmt, const Args &... args)
{
spdlog::get("BlingLog")->critical(fmt, args...);
if (spdlog::get("BlingLog") != nullptr)
{
spdlog::get("BlingLog")->critical(fmt, args...);
}
}
};
}}}

0 comments on commit f3030e4

Please sign in to comment.