-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlog_example.cpp
More file actions
147 lines (123 loc) · 5.15 KB
/
log_example.cpp
File metadata and controls
147 lines (123 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2012 Aldebaran Robotics. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the COPYING file.
*/
#include <iostream>
#include <qi/log.hpp>
#include <boost/program_options.hpp>
namespace po = boost::program_options;
int main(int argc, char **argv)
{
po::options_description desc("Allowed options");
int globalVerbosity;
// Used to parse options, as well as to put help message
desc.add_options()
("help,h", "Produces help message")
("version", "Output NAOqi version.")
("verbose,v", "Set verbose verbosity.")
("debug,d", "Set debug verbosity.")
("quiet,q", "Do not show logs on console.")
("context,c", po::value<int>(), "Show context logs: [0-7] (0: none, 1: categories, 2: date, 3: file+line, 4: date+categories, 5: date+line+file, 6: categories+line+file, 7: all (date+categories+line+file+function)).")
("synchronous-log", "Activate synchronous logs.")
("log-level,L", po::value<int>(&globalVerbosity), "Change the log minimum level: [0-6] (0: silent, 1: fatal, 2: error, 3: warning, 4: info, 5: verbose, 6: debug). Default: 4 (info)")
;
// Map containing all the options with their values
po::variables_map vm;
// program option library throws all kind of errors, we just catch them
// all, print usage and exit
try
{
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error &e)
{
std::cerr << e.what() << std::endl;
std::cout << desc << std::endl;
exit(1);
}
if (vm.count("help")) {
std::cout << desc << std::endl;
return 0;
}
// set default log verbosity
// set default log context
if (vm.count("log-level"))
{
if (globalVerbosity > 0 && globalVerbosity <= 6)
qi::log::setLogLevel((qi::LogLevel)globalVerbosity);
if (globalVerbosity > 6)
qi::log::setLogLevel(qi::LogLevel_Debug);
if (globalVerbosity <= 0)
qi::log::setLogLevel(qi::LogLevel_Silent);
}
// Remove consoleloghandler (default log handler)
if (vm.count("quiet"))
qi::log::removeHandler("consoleloghandler");
if (vm.count("debug"))
qi::log::setLogLevel(qi::LogLevel_Debug);
if (vm.count("verbose"))
qi::log::setLogLevel(qi::LogLevel_Verbose);
if (vm.count("context"))
{
int globalContext = vm["context"].as<int>();
if (globalContext < 0)
{
qi::log::setContext(0);
}
else
{
qi::log::setContext(globalContext);
}
}
if (vm.count("synchronous-log"))
qi::log::setSynchronousLog(true);
qiLogFatal("core.log.example.1", "%d\n", 41);
qiLogError("core.log.example.1", "%d\n", 42);
qiLogWarning("core.log.example.1", "%d\n", 43);
qiLogInfo("core.log.example.1", "%d\n", 44);
qiLogVerbose("core.log.example.1", "%d\n", 45);
qiLogDebug("core.log.example.1", "%d\n", 46);
qiLogFatal("core.log.example.2") << "f" << 4 << std::endl;
qiLogError("core.log.example.2") << "e" << 4 << std::endl;
qiLogWarning("core.log.example.2") << "w" << 4 << std::endl;
qiLogInfo("core.log.example.2") << "i" << 4 << std::endl;
qiLogVerbose("core.log.example.2") << "v" << 4 << std::endl;
qiLogDebug("core.log.example.2") << "d" << 4 << std::endl;
qiLogFatal("core.log.example.1", "without '\\n': %d", 41);
qiLogError("core.log.example.1", "without '\\n': %d", 42);
qiLogWarning("core.log.example.1", "without '\\n': %d", 43);
qiLogInfo("core.log.example.1", "without '\\n': %d", 44);
qiLogVerbose("core.log.example.1", "without '\\n': %d", 45);
qiLogDebug("core.log.example.1", "without '\\n': %d", 46);
qiLogFatal("core.log.example.1", "");
qiLogFatal("core.log.example.1", "\n");
qiLogFatal("core.log.example.2") << "f " << "without '\\n'";
qiLogError("core.log.example.2") << "e " << "without '\\n'";
qiLogWarning("core.log.example.2") << "w " << "without '\\n'";
qiLogInfo("core.log.example.2") << "i " << "without '\\n'";
qiLogVerbose("core.log.example.2") << "v " << "without '\\n'";
qiLogDebug("core.log.example.2") << "d " << "without '\\n'";
qiLogFatal("core.log.example.2") << "";
qiLogFatal("core.log.example.2") << std::endl;
qiLogFatal("core.log.example.3", "%d", 21) << "f" << 4 << std::endl;
qiLogError("core.log.example.3", "%d", 21) << "e" << 4 << std::endl;
qiLogWarning("core.log.example.3", "%d", 21) << "w" << 4 << std::endl;
qiLogInfo("core.log.example.3", "%d", 21) << "i" << 4 << std::endl;
qiLogVerbose("core.log.example.3", "%d", 21) << "v" << 4 << std::endl;
qiLogDebug("core.log.example.3", "%d", 21) << "d" << 4 << std::endl;
//c style
qiLogWarning("core.log.example.4",
"Oups my buffer is too bad: %x\n",
0x0BADCAFE);
//c++ style
qiLogError("core.log.example.4") << "Where is nao?"
<< " - Nao is in the kitchen."
<< " - How many are they? "
<< 42 << std::endl;
//mixup style
qiLogInfo("core.log.example.4", "%d %d ", 41, 42) << 43 << " " << 44
<< std::endl;
std::cout << "I've just finished to log!" << std::endl;
}