diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/log/example/event_log | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/log/example/event_log')
-rw-r--r-- | src/boost/libs/log/example/event_log/Jamfile.v2 | 102 | ||||
-rw-r--r-- | src/boost/libs/log/example/event_log/event_log_messages.mc | 58 | ||||
-rw-r--r-- | src/boost/libs/log/example/event_log/main.cpp | 190 |
3 files changed, 350 insertions, 0 deletions
diff --git a/src/boost/libs/log/example/event_log/Jamfile.v2 b/src/boost/libs/log/example/event_log/Jamfile.v2 new file mode 100644 index 00000000..bcc17f46 --- /dev/null +++ b/src/boost/libs/log/example/event_log/Jamfile.v2 @@ -0,0 +1,102 @@ +# +# Copyright Andrey Semashev 2007 - 2016. +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) +# + +import os ; +import configure ; +import ../../build/log-platform-config ; + +rule has-config-flag ( flag : properties * ) +{ + if ( "<define>$(flag)" in $(properties) || "<define>$(flag)=1" in $(properties) ) + { + return 1 ; + } + else + { + return ; + } +} + +rule check-message-compiler ( properties * ) +{ + local result ; + + if <target-os>windows in $(properties) + { + if ! [ has-config-flag BOOST_LOG_WITHOUT_EVENT_LOG : $(properties) ] + { + local has_mc = [ configure.builds /boost/log/message-compiler//test-availability : $(properties) : message-compiler ] ; + if ! $(has_mc) + { + result += <build>no ; + } + } + } + else + { + result += <build>no ; + } + + return $(result) ; +} + +project + : requirements + <conditional>@log-platform-config.set-platform-defines + <conditional>@check-message-compiler + + <link>shared + <toolset>msvc:<define>_SCL_SECURE_NO_WARNINGS + <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE + <toolset>msvc:<define>_CRT_SECURE_NO_WARNINGS + <toolset>msvc:<define>_CRT_SECURE_NO_DEPRECATE + <toolset>msvc:<cxxflags>/bigobj + <toolset>msvc:<cxxflags>/wd4503 # decorated name length exceeded, name was truncated + <toolset>msvc:<cxxflags>/wd4456 # declaration of 'A' hides previous local declaration + <toolset>msvc:<cxxflags>/wd4459 # declaration of 'A' hides global declaration + <toolset>msvc:<cxxflags>/wd4003 # not enough actual parameters for macro 'X' - caused by BOOST_PP_IS_EMPTY and BOOST_PP_IS_BEGIN_PARENS which are used by Fusion + <toolset>intel-win:<define>_SCL_SECURE_NO_WARNINGS + <toolset>intel-win:<define>_SCL_SECURE_NO_DEPRECATE + <toolset>intel-win:<define>_CRT_SECURE_NO_WARNINGS + <toolset>intel-win:<define>_CRT_SECURE_NO_DEPRECATE + <toolset>darwin:<cxxflags>-ftemplate-depth-1024 + <toolset>gcc:<cxxflags>-ftemplate-depth-1024 + <toolset>gcc:<cxxflags>-fno-strict-aliasing # avoids strict aliasing violations in other Boost components + + # Disable Intel warnings: + # warning #177: function "X" was declared but never referenced + # warning #780: using-declaration ignored -- it refers to the current namespace + # warning #2196: routine is both "inline" and "noinline" + # remark #1782: #pragma once is obsolete. Use #ifndef guard instead. + # remark #193: zero used for undefined preprocessing identifier "X" + # remark #304: access control not specified ("public" by default) + # remark #981: operands are evaluated in unspecified order + # remark #1418: external function definition with no prior declaration + # Mostly comes from Boost.Phoenix: warning #411: class "X" defines no constructor to initialize the following: reference member "Y"... + # warning #734: "X" (declared at line N of "file.hpp"), required for copy that was eliminated, is inaccessible + # warning #279: controlling expression is constant + <toolset>intel-win:<cxxflags>"/Qwd177,780,2196,1782,193,304,981,1418,411,734,279" + <toolset>intel-linux:<cxxflags>"-wd177,780,2196,1782,193,304,981,1418,411,734,279" + <toolset>intel-darwin:<cxxflags>"-wd177,780,2196,1782,193,304,981,1418,411,734,279" + + <library>/boost/log//boost_log + <library>/boost/date_time//boost_date_time + <library>/boost/filesystem//boost_filesystem + <library>/boost/thread//boost_thread + <threading>multi + ; + +lib event_log_messages + : event_log_messages.mc + : <linkflags>-noentry +# <name>event_log_messages + ; + +exe event_log + : main.cpp + : <implicit-dependency>event_log_messages + ; diff --git a/src/boost/libs/log/example/event_log/event_log_messages.mc b/src/boost/libs/log/example/event_log/event_log_messages.mc new file mode 100644 index 00000000..d3e10790 --- /dev/null +++ b/src/boost/libs/log/example/event_log/event_log_messages.mc @@ -0,0 +1,58 @@ +; /* -------------------------------------------------------- +; HEADER SECTION +; */ +SeverityNames=(Debug=0x0:MY_SEVERITY_DEBUG + Info=0x1:MY_SEVERITY_INFO + Warning=0x2:MY_SEVERITY_WARNING + Error=0x3:MY_SEVERITY_ERROR + ) + +; /* -------------------------------------------------------- +; MESSAGE DEFINITION SECTION +; */ + +MessageIdTypedef=WORD + +MessageId=0x1 +SymbolicName=MY_CATEGORY_1 +Language=English +Category 1 +. + +MessageId=0x2 +SymbolicName=MY_CATEGORY_2 +Language=English +Category 2 +. + +MessageId=0x3 +SymbolicName=MY_CATEGORY_3 +Language=English +Category 3 +. + +MessageIdTypedef=DWORD + +MessageId=0x100 +Severity=Warning +Facility=Application +SymbolicName=LOW_DISK_SPACE_MSG +Language=English +The drive %1 has low free disk space. At least %2 Mb of free space is recommended. +. + +MessageId=0x101 +Severity=Error +Facility=Application +SymbolicName=DEVICE_INACCESSIBLE_MSG +Language=English +The drive %1 is not accessible. +. + +MessageId=0x102 +Severity=Info +Facility=Application +SymbolicName=SUCCEEDED_MSG +Language=English +Operation finished successfully in %1 seconds. +. diff --git a/src/boost/libs/log/example/event_log/main.cpp b/src/boost/libs/log/example/event_log/main.cpp new file mode 100644 index 00000000..ca02a188 --- /dev/null +++ b/src/boost/libs/log/example/event_log/main.cpp @@ -0,0 +1,190 @@ +/* + * Copyright Andrey Semashev 2007 - 2015. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +/*! + * \file main.cpp + * \author Andrey Semashev + * \date 16.11.2008 + * + * \brief An example of logging into Windows event log. + * + * The example shows the basic usage of the Windows NT event log backend. + * The code defines custom severity levels, initializes the sink and a couple of + * attributes to test with, and writes several records at different levels. + * As a result the written records should appear in the Application log, and + * should be displayed correctly with the Windows event log viewer. + */ + +#include <stdexcept> +#include <string> +#include <iostream> +#include <boost/smart_ptr/shared_ptr.hpp> +#include <boost/date_time/posix_time/posix_time_types.hpp> + +#include <boost/log/common.hpp> +#include <boost/log/attributes.hpp> +#include <boost/log/expressions.hpp> +#include <boost/log/sinks/sync_frontend.hpp> +#include <boost/log/sinks/event_log_backend.hpp> + +#if !defined(WIN32_LEAN_AND_MEAN) +#define WIN32_LEAN_AND_MEAN +#endif + +#include <windows.h> +#include "event_log_messages.h" + +namespace logging = boost::log; +namespace attrs = boost::log::attributes; +namespace src = boost::log::sources; +namespace sinks = boost::log::sinks; +namespace expr = boost::log::expressions; +namespace keywords = boost::log::keywords; + +//[ example_sinks_event_log_severity +// Define application-specific severity levels +enum severity_level +{ + normal, + warning, + error +}; +//] + +void init_logging() +{ + //[ example_sinks_event_log_create_backend + // Create an event log sink + boost::shared_ptr< sinks::event_log_backend > backend( + new sinks::event_log_backend(( + keywords::message_file = "%SystemDir%\\event_log_messages.dll", + keywords::log_name = "My Application", + keywords::log_source = "My Source" + )) + ); + //] + + //[ example_sinks_event_log_event_composer + // Create an event composer. It is initialized with the event identifier mapping. + sinks::event_log::event_composer composer( + sinks::event_log::direct_event_id_mapping< int >("EventID")); + + // For each event described in the message file, set up the insertion string formatters + composer[LOW_DISK_SPACE_MSG] + // the first placeholder in the message + // will be replaced with contents of the "Drive" attribute + % expr::attr< std::string >("Drive") + // the second placeholder in the message + // will be replaced with contents of the "Size" attribute + % expr::attr< boost::uintmax_t >("Size"); + + composer[DEVICE_INACCESSIBLE_MSG] + % expr::attr< std::string >("Drive"); + + composer[SUCCEEDED_MSG] + % expr::attr< unsigned int >("Duration"); + + // Then put the composer to the backend + backend->set_event_composer(composer); + //] + + //[ example_sinks_event_log_mappings + // We'll have to map our custom levels to the event log event types + sinks::event_log::custom_event_type_mapping< severity_level > type_mapping("Severity"); + type_mapping[normal] = sinks::event_log::make_event_type(MY_SEVERITY_INFO); + type_mapping[warning] = sinks::event_log::make_event_type(MY_SEVERITY_WARNING); + type_mapping[error] = sinks::event_log::make_event_type(MY_SEVERITY_ERROR); + + backend->set_event_type_mapper(type_mapping); + + // Same for event categories. + // Usually event categories can be restored by the event identifier. + sinks::event_log::custom_event_category_mapping< int > cat_mapping("EventID"); + cat_mapping[LOW_DISK_SPACE_MSG] = sinks::event_log::make_event_category(MY_CATEGORY_1); + cat_mapping[DEVICE_INACCESSIBLE_MSG] = sinks::event_log::make_event_category(MY_CATEGORY_2); + cat_mapping[SUCCEEDED_MSG] = sinks::event_log::make_event_category(MY_CATEGORY_3); + + backend->set_event_category_mapper(cat_mapping); + //] + + //[ example_sinks_event_log_register_sink + // Create the frontend for the sink + boost::shared_ptr< sinks::synchronous_sink< sinks::event_log_backend > > sink( + new sinks::synchronous_sink< sinks::event_log_backend >(backend)); + + // Set up filter to pass only records that have the necessary attribute + sink->set_filter(expr::has_attr< int >("EventID")); + + logging::core::get()->add_sink(sink); + //] +} + +//[ example_sinks_event_log_facilities +BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(event_logger, src::severity_logger_mt< severity_level >) + +// The function raises an event of the disk space depletion +void announce_low_disk_space(std::string const& drive, boost::uintmax_t size) +{ + BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)LOW_DISK_SPACE_MSG); + BOOST_LOG_SCOPED_THREAD_TAG("Drive", drive); + BOOST_LOG_SCOPED_THREAD_TAG("Size", size); + // Since this record may get accepted by other sinks, + // this message is not completely useless + BOOST_LOG_SEV(event_logger::get(), warning) << "Low disk " << drive + << " space, " << size << " Mb is recommended"; +} + +// The function raises an event of inaccessible disk drive +void announce_device_inaccessible(std::string const& drive) +{ + BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)DEVICE_INACCESSIBLE_MSG); + BOOST_LOG_SCOPED_THREAD_TAG("Drive", drive); + BOOST_LOG_SEV(event_logger::get(), error) << "Cannot access drive " << drive; +} + +// The structure is an activity guard that will emit an event upon the activity completion +struct activity_guard +{ + activity_guard() + { + // Add a stop watch attribute to measure the activity duration + m_it = event_logger::get().add_attribute("Duration", attrs::timer()).first; + } + ~activity_guard() + { + BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)SUCCEEDED_MSG); + BOOST_LOG_SEV(event_logger::get(), normal) << "Activity ended"; + event_logger::get().remove_attribute(m_it); + } + +private: + logging::attribute_set::iterator m_it; +}; +//] + +int main(int argc, char* argv[]) +{ + try + { + // Initialize the library + init_logging(); + + // Make some events + { + activity_guard activity; + + announce_low_disk_space("C:", 2 * 1024 * 1024); + announce_device_inaccessible("D:"); + } + + return 0; + } + catch (std::exception& e) + { + std::cout << "FAILURE: " << e.what() << std::endl; + return 1; + } +} |