iotivity 0.9.0
[platform/upstream/iotivity.git] / resource / oc_logger / examples / test_logging.cpp
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "oc_logger.hpp"
22
23 #include <iosfwd>
24 #include <memory>
25 #include <cassert>
26 #include <sstream>
27 #include <iostream>
28
29 #include <boost/iostreams/stream.hpp>
30 #include <boost/iostreams/categories.hpp>
31
32 #include <boost/config.hpp>
33 #include <boost/iostreams/categories.hpp>
34 #include <boost/iostreams/detail/ios.hpp>
35
36 void basic_demo()
37 {
38  using OC::oc_log_stream;
39
40  oc_log_stream ols(oc_make_ostream_logger);
41
42  boost::iostreams::stream<oc_log_stream> os(ols);
43
44  os << "Greetings from the nifty world of logging!" << std::flush;
45
46  ols.set_level(OC_LOG_ALL);
47  ols.set_module("TheHappyModule");
48  ols.set_module("TheModule");
49  os << "Whee!" << std::flush;
50
51  // Setting the module name by getting the device from the stream itself:
52  (*os).set_module("TheHappiestModuleEver");
53  os << "Whee! Again!" << std::flush;
54 }
55
56 /* Show that we can use a C logger from C++: */
57 void c_demo()
58 {
59  using OC::oc_log_stream;
60
61  oc_log_stream ols(oc_make_console_logger);
62
63  boost::iostreams::stream<oc_log_stream> os(ols);
64
65  os << "Greetings from the nifty world of logging!" << std::flush;
66
67  ols.set_level(OC_LOG_ALL);
68  ols.set_module("TheHappyModule");
69  os << "Whee!" << std::flush;
70
71  (*os).set_module("TheHappiestModuleEver");
72  os << "Whee!" << std::flush;
73 }
74
75 void alternative_demo()
76 {
77  /* Annother way to create a context: */
78  auto logger = []() -> boost::iostreams::stream<OC::oc_log_stream>&
79   {
80     static OC::oc_log_stream ols(oc_make_ostream_logger);
81     static boost::iostreams::stream<OC::oc_log_stream> os(ols);
82
83     return os;
84   };
85
86  logger()->set_module("FantasticModule");
87  logger() << "Hello, logging world!" << std::flush;
88 }
89
90 int main()
91 {
92  basic_demo();
93  c_demo();
94  alternative_demo();
95  return 0;
96 }
97