main.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file main.cpp
  9. * \author Andrey Semashev
  10. * \date 16.11.2008
  11. *
  12. * \brief An example of logging into Windows event log.
  13. *
  14. * The example shows the basic usage of the Windows NT event log backend.
  15. * The code defines custom severity levels, initializes the sink and a couple of
  16. * attributes to test with, and writes several records at different levels.
  17. * As a result the written records should appear in the Application log, and
  18. * should be displayed correctly with the Windows event log viewer.
  19. */
  20. #include <stdexcept>
  21. #include <string>
  22. #include <iostream>
  23. #include <boost/smart_ptr/shared_ptr.hpp>
  24. #include <boost/date_time/posix_time/posix_time_types.hpp>
  25. #include <boost/log/common.hpp>
  26. #include <boost/log/attributes.hpp>
  27. #include <boost/log/expressions.hpp>
  28. #include <boost/log/sinks/sync_frontend.hpp>
  29. #include <boost/log/sinks/event_log_backend.hpp>
  30. #if !defined(WIN32_LEAN_AND_MEAN)
  31. #define WIN32_LEAN_AND_MEAN
  32. #endif
  33. #include <windows.h>
  34. #include "event_log_messages.h"
  35. namespace logging = boost::log;
  36. namespace attrs = boost::log::attributes;
  37. namespace src = boost::log::sources;
  38. namespace sinks = boost::log::sinks;
  39. namespace expr = boost::log::expressions;
  40. namespace keywords = boost::log::keywords;
  41. //[ example_sinks_event_log_severity
  42. // Define application-specific severity levels
  43. enum severity_level
  44. {
  45. normal,
  46. warning,
  47. error
  48. };
  49. //]
  50. void init_logging()
  51. {
  52. //[ example_sinks_event_log_create_backend
  53. // Create an event log sink
  54. boost::shared_ptr< sinks::event_log_backend > backend(
  55. new sinks::event_log_backend((
  56. keywords::message_file = "%SystemDir%\\event_log_messages.dll",
  57. keywords::log_name = "My Application",
  58. keywords::log_source = "My Source"
  59. ))
  60. );
  61. //]
  62. //[ example_sinks_event_log_event_composer
  63. // Create an event composer. It is initialized with the event identifier mapping.
  64. sinks::event_log::event_composer composer(
  65. sinks::event_log::direct_event_id_mapping< int >("EventID"));
  66. // For each event described in the message file, set up the insertion string formatters
  67. composer[LOW_DISK_SPACE_MSG]
  68. // the first placeholder in the message
  69. // will be replaced with contents of the "Drive" attribute
  70. % expr::attr< std::string >("Drive")
  71. // the second placeholder in the message
  72. // will be replaced with contents of the "Size" attribute
  73. % expr::attr< boost::uintmax_t >("Size");
  74. composer[DEVICE_INACCESSIBLE_MSG]
  75. % expr::attr< std::string >("Drive");
  76. composer[SUCCEEDED_MSG]
  77. % expr::attr< unsigned int >("Duration");
  78. // Then put the composer to the backend
  79. backend->set_event_composer(composer);
  80. //]
  81. //[ example_sinks_event_log_mappings
  82. // We'll have to map our custom levels to the event log event types
  83. sinks::event_log::custom_event_type_mapping< severity_level > type_mapping("Severity");
  84. type_mapping[normal] = sinks::event_log::make_event_type(MY_SEVERITY_INFO);
  85. type_mapping[warning] = sinks::event_log::make_event_type(MY_SEVERITY_WARNING);
  86. type_mapping[error] = sinks::event_log::make_event_type(MY_SEVERITY_ERROR);
  87. backend->set_event_type_mapper(type_mapping);
  88. // Same for event categories.
  89. // Usually event categories can be restored by the event identifier.
  90. sinks::event_log::custom_event_category_mapping< int > cat_mapping("EventID");
  91. cat_mapping[LOW_DISK_SPACE_MSG] = sinks::event_log::make_event_category(MY_CATEGORY_1);
  92. cat_mapping[DEVICE_INACCESSIBLE_MSG] = sinks::event_log::make_event_category(MY_CATEGORY_2);
  93. cat_mapping[SUCCEEDED_MSG] = sinks::event_log::make_event_category(MY_CATEGORY_3);
  94. backend->set_event_category_mapper(cat_mapping);
  95. //]
  96. //[ example_sinks_event_log_register_sink
  97. // Create the frontend for the sink
  98. boost::shared_ptr< sinks::synchronous_sink< sinks::event_log_backend > > sink(
  99. new sinks::synchronous_sink< sinks::event_log_backend >(backend));
  100. // Set up filter to pass only records that have the necessary attribute
  101. sink->set_filter(expr::has_attr< int >("EventID"));
  102. logging::core::get()->add_sink(sink);
  103. //]
  104. }
  105. //[ example_sinks_event_log_facilities
  106. BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(event_logger, src::severity_logger_mt< severity_level >)
  107. // The function raises an event of the disk space depletion
  108. void announce_low_disk_space(std::string const& drive, boost::uintmax_t size)
  109. {
  110. BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)LOW_DISK_SPACE_MSG);
  111. BOOST_LOG_SCOPED_THREAD_TAG("Drive", drive);
  112. BOOST_LOG_SCOPED_THREAD_TAG("Size", size);
  113. // Since this record may get accepted by other sinks,
  114. // this message is not completely useless
  115. BOOST_LOG_SEV(event_logger::get(), warning) << "Low disk " << drive
  116. << " space, " << size << " Mb is recommended";
  117. }
  118. // The function raises an event of inaccessible disk drive
  119. void announce_device_inaccessible(std::string const& drive)
  120. {
  121. BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)DEVICE_INACCESSIBLE_MSG);
  122. BOOST_LOG_SCOPED_THREAD_TAG("Drive", drive);
  123. BOOST_LOG_SEV(event_logger::get(), error) << "Cannot access drive " << drive;
  124. }
  125. // The structure is an activity guard that will emit an event upon the activity completion
  126. struct activity_guard
  127. {
  128. activity_guard()
  129. {
  130. // Add a stop watch attribute to measure the activity duration
  131. m_it = event_logger::get().add_attribute("Duration", attrs::timer()).first;
  132. }
  133. ~activity_guard()
  134. {
  135. BOOST_LOG_SCOPED_THREAD_TAG("EventID", (int)SUCCEEDED_MSG);
  136. BOOST_LOG_SEV(event_logger::get(), normal) << "Activity ended";
  137. event_logger::get().remove_attribute(m_it);
  138. }
  139. private:
  140. logging::attribute_set::iterator m_it;
  141. };
  142. //]
  143. int main(int argc, char* argv[])
  144. {
  145. try
  146. {
  147. // Initialize the library
  148. init_logging();
  149. // Make some events
  150. {
  151. activity_guard activity;
  152. announce_low_disk_space("C:", 2 * 1024 * 1024);
  153. announce_device_inaccessible("D:");
  154. }
  155. return 0;
  156. }
  157. catch (std::exception& e)
  158. {
  159. std::cout << "FAILURE: " << e.what() << std::endl;
  160. return 1;
  161. }
  162. }