]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/example/doc/extension_system_uptime_attr.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / log / example / doc / extension_system_uptime_attr.cpp
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 #include <string>
9 #include <iostream>
10 #include <stdexcept>
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>
24
25 // Includes for get_uptime()
26 #include <boost/throw_exception.hpp>
27 #if defined(BOOST_WINDOWS)
28 #include <windows.h>
29 #elif defined(__linux__) || defined(__linux) || defined(linux)
30 #include <sys/sysinfo.h>
31 #elif defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__)
32 #include <time.h>
33 #include <errno.h>
34 #include <sys/sysctl.h>
35 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
36 #include <time.h>
37 #endif
38
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;
45
46 //[ example_extension_system_uptime_attr_impl
47 // The function returns the system uptime, in seconds
48 unsigned int get_uptime();
49
50 // Attribute implementation class
51 class system_uptime_impl :
52 public logging::attribute::impl
53 {
54 public:
55 // The method generates a new attribute value
56 logging::attribute_value get_value()
57 {
58 return attrs::make_attribute_value(get_uptime());
59 }
60 };
61 //]
62
63 //[ example_extension_system_uptime_attr
64 // Attribute interface class
65 class system_uptime :
66 public logging::attribute
67 {
68 public:
69 system_uptime() : logging::attribute(new system_uptime_impl())
70 {
71 }
72 // Attribute casting support
73 explicit system_uptime(attrs::cast_source const& source) : logging::attribute(source.as< system_uptime_impl >())
74 {
75 }
76 };
77 //]
78
79 //[ example_extension_system_uptime_use
80 void init_logging()
81 {
82 boost::shared_ptr< logging::core > core = logging::core::get();
83
84 //<-
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);
90 core->add_sink(sink);
91 //->
92 // ...
93
94 // Add the uptime attribute to the core
95 core->add_global_attribute("SystemUptime", system_uptime());
96 }
97 //]
98
99 unsigned int get_uptime()
100 {
101 #if defined(BOOST_WINDOWS)
102 return GetTickCount() / 1000u;
103 #elif defined(__linux__) || defined(__linux) || defined(linux)
104 struct sysinfo info;
105 if (sysinfo(&info) != 0)
106 BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
107 return info.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)
116 struct timespec ts;
117 if (clock_gettime(CLOCK_UPTIME, &ts) != 0)
118 BOOST_THROW_EXCEPTION(std::runtime_error("Could not acquire uptime"));
119 return ts.tv_sec;
120 #else
121 return 0;
122 #endif
123 }
124
125 int main(int, char*[])
126 {
127 init_logging();
128
129 src::logger lg;
130 BOOST_LOG(lg) << "Hello, world with uptime!";
131
132 return 0;
133 }