From ac3d1869cfe67d16767d8b3a8480a1c8be4f0fb2 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 23 Aug 2017 12:58:07 +0000 Subject: [PATCH] Fixed logging on Windows. Event log sink is still broken. --- src/client/spark_api/logging/Logger.cs | 12 ------------ src/client/spark_api/utils/BTSNativeMethods.cs | 5 ++++- src/lib/CMakeLists.txt | 1 + src/lib/config/ConfigLog.cpp | 11 +++++++++-- src/lib/config/ConfigLog.h | 3 ++- src/lib/config/ConfigurationState.cpp | 3 ++- src/lib/logging/Logger.h | 2 ++ src/lib/logging/LoggerHub.cpp | 11 +++++++++-- src/lib/logging/LoggerHub.h | 2 ++ src/lib/logging/logging.cpp | 14 +++++++++++++- src/lib/logging/logging.h | 1 + src/lib/utils/init_spark_lib.cpp | 11 +++++++++++ src/lib/utils/init_spark_lib.h | 14 ++++++++++++++ src/server/sparkd/main.cpp | 3 +++ 14 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 src/lib/utils/init_spark_lib.cpp create mode 100644 src/lib/utils/init_spark_lib.h diff --git a/src/client/spark_api/logging/Logger.cs b/src/client/spark_api/logging/Logger.cs index 89826e4e6..007c23870 100644 --- a/src/client/spark_api/logging/Logger.cs +++ b/src/client/spark_api/logging/Logger.cs @@ -28,9 +28,7 @@ namespace mbts { internal static class Logger { - static on_crash_callback native_crash_callback = system_is_crashing; static Logger() { - set_on_crash_handler(native_crash_callback); } [Conditional("DEBUG")] @@ -44,11 +42,6 @@ namespace mbts Logger_log(level, system, message, file, line); } - // Log this message at most once per run - public static void log_once_probably(Level level, Subsystem system, string message, [CallerFilePath] string file = "c#", [CallerLineNumber] int line = 0) { - Logger_log_once(level, system, $"{message} [{file}:{line}]"); - } - public static void error(Subsystem system, string message, [CallerFilePath] string file = "C#", [CallerLineNumber] int line = 0) { log(Level.Error, system, message, file, line); } @@ -85,13 +78,8 @@ namespace mbts [DllImport("spark_lib")] private static extern void Logger_log (Level level, Subsystem subsystem, string message, string file, int line); - [DllImport("spark_lib")] - private static extern void Logger_log_once(Level level, Subsystem subsystem, string message); delegate void on_crash_callback([MarshalAs(UnmanagedType.LPStr)]string msg, [MarshalAs(UnmanagedType.LPStr)] string details); - [DllImport("spark_lib")] - private static extern on_crash_callback set_on_crash_handler(on_crash_callback handler); - public static void system_is_crashing(string message, object details) { OnSystemCrashing?.Invoke(message, details); } diff --git a/src/client/spark_api/utils/BTSNativeMethods.cs b/src/client/spark_api/utils/BTSNativeMethods.cs index 0ac23d14c..3bd259f00 100644 --- a/src/client/spark_api/utils/BTSNativeMethods.cs +++ b/src/client/spark_api/utils/BTSNativeMethods.cs @@ -10,11 +10,14 @@ namespace mbts.utils { [DllImport("spark_lib")] public static extern void provide_command_line( - [MarshalAs(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPStr)] string[] argv, int argc); + [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] argv, int argc); [DllImport("spark_lib")] public static extern int get_random_port(); + [DllImport("spark_lib")] + public static extern void init_spark_lib(); + [DllImport("kernel32.dll")] public static extern bool SetConsoleTitle(string lpConsoleTitle); } diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index cd78c9c6a..65efd5dc9 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -117,6 +117,7 @@ set (SparkLib_Sources utils/SharedMemory.cpp utils/Dispatcher.cpp utils/Sqlite_utils.cpp + utils/init_spark_lib.cpp # security model security_model/SecurityHelper.cpp diff --git a/src/lib/config/ConfigLog.cpp b/src/lib/config/ConfigLog.cpp index a5da1fbc1..a5a4201e4 100755 --- a/src/lib/config/ConfigLog.cpp +++ b/src/lib/config/ConfigLog.cpp @@ -7,11 +7,13 @@ namespace config { namespace internal { struct ConfigLog::PImpl { - PImpl() : + PImpl() + : print_errors(true), logger_hub("config_logger"), logger(logger_hub.make_logger()) - {} + { + } bool print_errors; log::LoggerHub logger_hub; @@ -42,6 +44,11 @@ ConfigLog::ConfigLog() : me->logger_hub.config(cfg); } +void ConfigLog::start() +{ + me->logger_hub.start(); +} + ConfigLog::~ConfigLog() { me->logger.mark_inactive(); diff --git a/src/lib/config/ConfigLog.h b/src/lib/config/ConfigLog.h index 19510a8d3..2fb8ac87f 100755 --- a/src/lib/config/ConfigLog.h +++ b/src/lib/config/ConfigLog.h @@ -30,7 +30,8 @@ public: void info(const std::string& msg); void error(const std::string& msg); void warning(const std::string& msg); - + void start(); + bool print_errors() const ; void set_print_errors(bool value) ; diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index 9ce72096e..6866f944b 100755 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -149,7 +149,8 @@ void ConfigurationState::initialize_defaults() { std::lock_guard lock(_initialization_mutex); if (_is_initialized) return; - + + _log.start(); generate_all_config_proto(); std::shared_ptr new_message(_prototype->New()); diff --git a/src/lib/logging/Logger.h b/src/lib/logging/Logger.h index 1dd5bee74..307b69361 100644 --- a/src/lib/logging/Logger.h +++ b/src/lib/logging/Logger.h @@ -107,8 +107,10 @@ public: ~Logger() { +#ifndef WIN32 bts_assert(!_active); bts_assert(_wcursor == 0); +#endif } diff --git a/src/lib/logging/LoggerHub.cpp b/src/lib/logging/LoggerHub.cpp index 5fd5542e4..9b624dd9a 100644 --- a/src/lib/logging/LoggerHub.cpp +++ b/src/lib/logging/LoggerHub.cpp @@ -49,7 +49,8 @@ LoggerHub::LoggerHub(std::string const&logger_name) { LoggingConfig cfg ; config(cfg); - me->worker = std::thread(std::bind(&LoggerHub::do_work,this)); + // we cannot launch the worker thread here because thread creation + // is not possible during static initialization } void LoggerHub::config(const LoggingConfig &cfg) @@ -82,10 +83,16 @@ void LoggerHub::config(const LoggingConfig &cfg) update_logger_queue_limits(); } +void LoggerHub::start() { + bts_assert(!me->worker.joinable()); + me->worker = std::thread(&LoggerHub::do_work, this); +} + LoggerHub::~LoggerHub() { me->done=true; - me->worker.join(); + if (me->worker.joinable()) + me->worker.join(); } Logger &LoggerHub::make_logger() diff --git a/src/lib/logging/LoggerHub.h b/src/lib/logging/LoggerHub.h index 0ca757010..1ef80ea20 100644 --- a/src/lib/logging/LoggerHub.h +++ b/src/lib/logging/LoggerHub.h @@ -17,6 +17,7 @@ public: LoggerHub(std::string const& ); ~LoggerHub(); void config(const LoggingConfig &cfg); + void start(); Logger &make_logger(); private: @@ -32,4 +33,5 @@ private: Filter get_base_filter() const; }; +void start_logging_hub(); }} diff --git a/src/lib/logging/logging.cpp b/src/lib/logging/logging.cpp index 8dbe67e8d..5a7f6417d 100644 --- a/src/lib/logging/logging.cpp +++ b/src/lib/logging/logging.cpp @@ -30,6 +30,11 @@ typename M::const_iterator find_key(M const&m, typename M::mapped_type const &v) return m.end(); } +void start_default_logging_thread() +{ + _default_logger_hub.start(); +} + Filter get_filter (LoggingConfig &cfg, config::Logging::SinkConfig const&scfg) { @@ -143,5 +148,12 @@ LoggingConfigPoller::~LoggingConfigPoller () _poller_thread.join(); } -}} +namespace interop { +extern "C" BTS_EXPORT void Logger_log (int level, int subsystem, const char *message, const char *file, int line) +{ + bts::log::_thread_logger.logger->enqueue(log::Subsystem{(uint8_t)subsystem},log::Level{(uint8_t)level},file,line,message); +} +} + +}} diff --git a/src/lib/logging/logging.h b/src/lib/logging/logging.h index 02239fbf0..30d64826b 100644 --- a/src/lib/logging/logging.h +++ b/src/lib/logging/logging.h @@ -41,6 +41,7 @@ struct LoggerHandle { extern thread_local LoggerHandle _thread_logger; void configure_logging(LoggingConfig const&cfg); void configure_logging(bts::config::Logging const&cfg); +void start_default_logging_thread(); class LoggingConfigPoller { public: diff --git a/src/lib/utils/init_spark_lib.cpp b/src/lib/utils/init_spark_lib.cpp new file mode 100644 index 000000000..2200ed7ae --- /dev/null +++ b/src/lib/utils/init_spark_lib.cpp @@ -0,0 +1,11 @@ +#include "init_spark_lib.h" +#include "lib/logging/logging.h" + +namespace bts { +namespace utils { +void init_spark_lib() +{ + log::start_default_logging_thread(); +} + +}} diff --git a/src/lib/utils/init_spark_lib.h b/src/lib/utils/init_spark_lib.h new file mode 100644 index 000000000..e00fc3f8b --- /dev/null +++ b/src/lib/utils/init_spark_lib.h @@ -0,0 +1,14 @@ +#pragma once +#include "bts_export.h" + +// this is needed because we cannot relly on static initialization do +// work reliably in DLL's on Windows. See: +// https://msdn.microsoft.com/en-us/library/windows/desktop/dn633971(v=vs.85).aspx +// for a list of restrictions + +namespace bts { +namespace utils { + +extern "C" BTS_EXPORT void init_spark_lib(); + +}} diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 8eaaa86dc..0d52f0e44 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -4,6 +4,7 @@ #include "lib/config/config.h" #include "lib/config/protos/logging.pb.h" #include "lib/config/protos/Executor.pb.h" +#include "lib/utils/init_spark_lib.h" #include "setup/BootstrapActor.h" #include "fabric/Domain.h" #include "testing/catch_test_runner.h" @@ -17,6 +18,7 @@ int main( int argc, char*argv [] ) if (argc > 1 && std::string(argv[1]) == "test") bts::run_tests(--argc, ++argv); else { + utils::init_spark_lib(); log::LoggingConfigPoller poller; auto executor_cfg = config::get (); @@ -28,3 +30,4 @@ int main( int argc, char*argv [] ) +