]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/extension_system_uptime_attr.cpp
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)
11 #include <boost/config.hpp>
12 #include <boost/smart_ptr/shared_ptr.hpp>
13 #include <boost/core/null_deleter.hpp>
14 #include <boost/log/core.hpp>
15 #include <boost/log/expressions.hpp>
16 #include <boost/log/sinks/sync_frontend.hpp>
17 #include <boost/log/sinks/text_ostream_backend.hpp>
18 #include <boost/log/sources/logger.hpp>
19 #include <boost/log/sources/record_ostream.hpp>
20 #include <boost/log/attributes/attribute.hpp>
21 #include <boost/log/attributes/attribute_value.hpp>
22 #include <boost/log/attributes/attribute_value_impl.hpp>
23 #include <boost/log/attributes/attribute_cast.hpp>
25 // Includes for get_uptime()
26 #include <boost/throw_exception.hpp>
27 #if defined(BOOST_WINDOWS)
29 #elif defined(__linux__) || defined(__linux) || defined(linux)
30 #include <sys/sysinfo.h>
31 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
34 #include <sys/sysctl.h>
35 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
39 namespace logging
= boost::log
;
40 namespace attrs
= boost::log::attributes
;
41 namespace src
= boost::log::sources
;
42 namespace expr
= boost::log::expressions
;
43 namespace sinks
= boost::log::sinks
;
44 namespace keywords
= boost::log::keywords
;
46 //[ example_extension_system_uptime_attr_impl
47 // The function returns the system uptime, in seconds
48 unsigned int get_uptime();
50 // Attribute implementation class
51 class system_uptime_impl
:
52 public logging::attribute::impl
55 // The method generates a new attribute value
56 logging::attribute_value
get_value()
58 return attrs::make_attribute_value(get_uptime());
63 //[ example_extension_system_uptime_attr
64 // Attribute interface class
66 public logging::attribute
69 system_uptime() : logging::attribute(new system_uptime_impl())
72 // Attribute casting support
73 explicit system_uptime(attrs::cast_source
const& source
) : logging::attribute(source
.as
< system_uptime_impl
>())
79 //[ example_extension_system_uptime_use
82 boost::shared_ptr
< logging::core
> core
= logging::core::get();
85 // Initialize the sink
86 typedef sinks::synchronous_sink
< sinks::text_ostream_backend
> sink_t
;
87 boost::shared_ptr
< sink_t
> sink(new sink_t());
88 sink
->locked_backend()->add_stream(boost::shared_ptr
< std::ostream
>(&std::clog
, boost::null_deleter()));
89 sink
->set_formatter(expr::stream
<< expr::attr
< unsigned int >("SystemUptime") << ": " << expr::smessage
);
94 // Add the uptime attribute to the core
95 core
->add_global_attribute("SystemUptime", system_uptime());
99 unsigned int get_uptime()
101 #if defined(BOOST_WINDOWS)
102 return GetTickCount() / 1000u;
103 #elif defined(__linux__) || defined(__linux) || defined(linux)
105 if (sysinfo(&info
) != 0)
106 BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
108 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
109 struct timeval boottime
;
110 std::size_t len
= sizeof(boottime
);
111 int mib
[2] = { CTL_KERN
, KERN_BOOTTIME
};
112 if (sysctl(mib
, 2, &boottime
, &len
, NULL
, 0) < 0)
113 BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
114 return time(NULL
) - boottime
.tv_sec
;
115 #elif (defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) && defined(CLOCK_UPTIME)
117 if (clock_gettime(CLOCK_UPTIME
, &ts
) != 0)
118 BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
125 int main(int, char*[])
130 BOOST_LOG(lg
) << "Hello, world with uptime!";