extension_system_uptime_attr.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <string>
  8. #include <iostream>
  9. #include <stdexcept>
  10. #include <boost/config.hpp>
  11. #include <boost/smart_ptr/shared_ptr.hpp>
  12. #include <boost/core/null_deleter.hpp>
  13. #include <boost/log/core.hpp>
  14. #include <boost/log/expressions.hpp>
  15. #include <boost/log/sinks/sync_frontend.hpp>
  16. #include <boost/log/sinks/text_ostream_backend.hpp>
  17. #include <boost/log/sources/logger.hpp>
  18. #include <boost/log/sources/record_ostream.hpp>
  19. #include <boost/log/attributes/attribute.hpp>
  20. #include <boost/log/attributes/attribute_value.hpp>
  21. #include <boost/log/attributes/attribute_value_impl.hpp>
  22. #include <boost/log/attributes/attribute_cast.hpp>
  23. // Includes for get_uptime()
  24. #include <boost/throw_exception.hpp>
  25. #if defined(BOOST_WINDOWS)
  26. #include <windows.h>
  27. #elif defined(__linux__) || defined(__linux) || defined(linux)
  28. #include <sys/sysinfo.h>
  29. #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
  30. #include <time.h>
  31. #include <errno.h>
  32. #include <sys/sysctl.h>
  33. #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
  34. #include <time.h>
  35. #endif
  36. namespace logging = boost::log;
  37. namespace attrs = boost::log::attributes;
  38. namespace src = boost::log::sources;
  39. namespace expr = boost::log::expressions;
  40. namespace sinks = boost::log::sinks;
  41. namespace keywords = boost::log::keywords;
  42. //[ example_extension_system_uptime_attr_impl
  43. // The function returns the system uptime, in seconds
  44. unsigned int get_uptime();
  45. // Attribute implementation class
  46. class system_uptime_impl :
  47. public logging::attribute::impl
  48. {
  49. public:
  50. // The method generates a new attribute value
  51. logging::attribute_value get_value()
  52. {
  53. return attrs::make_attribute_value(get_uptime());
  54. }
  55. };
  56. //]
  57. //[ example_extension_system_uptime_attr
  58. // Attribute interface class
  59. class system_uptime :
  60. public logging::attribute
  61. {
  62. public:
  63. system_uptime() : logging::attribute(new system_uptime_impl())
  64. {
  65. }
  66. // Attribute casting support
  67. explicit system_uptime(attrs::cast_source const& source) : logging::attribute(source.as< system_uptime_impl >())
  68. {
  69. }
  70. };
  71. //]
  72. //[ example_extension_system_uptime_use
  73. void init_logging()
  74. {
  75. boost::shared_ptr< logging::core > core = logging::core::get();
  76. //<-
  77. // Initialize the sink
  78. typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_t;
  79. boost::shared_ptr< sink_t > sink(new sink_t());
  80. sink->locked_backend()->add_stream(boost::shared_ptr< std::ostream >(&std::clog, boost::null_deleter()));
  81. sink->set_formatter(expr::stream << expr::attr< unsigned int >("SystemUptime") << ": " << expr::smessage);
  82. core->add_sink(sink);
  83. //->
  84. // ...
  85. // Add the uptime attribute to the core
  86. core->add_global_attribute("SystemUptime", system_uptime());
  87. }
  88. //]
  89. unsigned int get_uptime()
  90. {
  91. #if defined(BOOST_WINDOWS)
  92. return GetTickCount() / 1000u;
  93. #elif defined(__linux__) || defined(__linux) || defined(linux)
  94. struct sysinfo info;
  95. if (sysinfo(&info) != 0)
  96. BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
  97. return info.uptime;
  98. #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
  99. struct timeval boottime;
  100. std::size_t len = sizeof(boottime);
  101. int mib[2] = { CTL_KERN, KERN_BOOTTIME };
  102. if (sysctl(mib, 2, &boottime, &len, NULL, 0) < 0)
  103. BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
  104. return time(NULL) - boottime.tv_sec;
  105. #elif (defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) && defined(CLOCK_UPTIME)
  106. struct timespec ts;
  107. if (clock_gettime(CLOCK_UPTIME, &ts) != 0)
  108. BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
  109. return ts.tv_sec;
  110. #else
  111. return 0;
  112. #endif
  113. }
  114. int main(int, char*[])
  115. {
  116. init_logging();
  117. src::logger lg;
  118. BOOST_LOG(lg) << "Hello, world with uptime!";
  119. return 0;
  120. }