NVIDIA Index example code nvidia_logo_transpbg.gif Up
configuration.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright 2023 NVIDIA Corporation. All rights reserved.
3 *****************************************************************************/
6
7#include <mi/dice.h>
8
9// Include code shared by examples
10#include "utility/example_shared.h"
11
12#include <sstream>
13#include <string>
14#include <stdio.h>
15#include <iostream>
16
17#include <mi/dice.h>
18#include <mi/base/handle.h>
19#include <mi/base/ilogger.h>
20
21#include <nv/index/iindex.h>
22#include <nv/index/iindex_debug_configuration.h>
23
24#include <nv/index/app/string_dict.h>
25#include <nv/index/app/index_connect.h>
26
28class My_receiving_logger : public mi::base::Interface_implement<mi::base::ILogger>
29{
30public:
31 // constructor
33 :
35 {
36 // empty
37 }
38
39 // constructor with is_show_destruct_message argument.
40 // \param[in] is_show_destruct_message when true show a message when destruct
41 My_receiving_logger(bool is_show_destruct_message) :
42 m_is_show_destruct_message(is_show_destruct_message)
43 {
44 // empty
45 }
46
47 // destructor
49 {
51 fprintf(stdout, "info: My_receiving_logger is destructed.\n");
52 }
53 }
54
55 // message out method
56 virtual void message(
57 mi::base::Message_severity level,
58 const char* module_category,
59 const mi::base::Message_details&,
60 const char* message)
61 {
62 const char *log_level = get_log_level( level);
63 fprintf(stdout, "info: Log level = '%s', module:category = '%s', message = '%s'\n",
64 log_level, module_category, message);
65 }
66
67public:
68 // message switch when destructed.
70
71private:
72 // get log level char*.
73 const char* get_log_level(mi::base::Message_severity level)
74 {
75 switch (level)
76 {
77 case mi::base::MESSAGE_SEVERITY_FATAL:
78 return "My_receiving_logger: FATAL";
79 case mi::base::MESSAGE_SEVERITY_ERROR:
80 return "My_receiving_logger: ERROR";
81 case mi::base::MESSAGE_SEVERITY_WARNING:
82 return "My_receiving_logger: WARNING";
83 case mi::base::MESSAGE_SEVERITY_INFO:
84 return "My_receiving_logger: INFO";
85 case mi::base::MESSAGE_SEVERITY_VERBOSE:
86 return "My_receiving_logger: VERBOSE";
87 case mi::base::MESSAGE_SEVERITY_DEBUG:
88 return "My_receiving_logger: DEBUG";
89 default:
90 return "";
91 }
92 }
93};
94
95//----------------------------------------------------------------------
96// Configuration application
98 public nv::index::app::Index_connect
99{
100public:
102 :
103 Index_connect(),
104 m_is_unittest_mode(false)
105 {
106 // INFO_LOG << "DEBUG: Configuration() ctor";
107 }
108
110 {
111 // Note: Index_connect::~Index_connect() will be called after here.
112 // INFO_LOG << "DEBUG: ~Configuration() dtor";
113 }
114
115 // launch application
116 mi::Sint32 launch();
117
118private:
119 bool m_is_unittest_mode;
120
121protected:
122 virtual bool evaluate_options(nv::index::app::String_dict& options) CPP11_OVERRIDE
123 {
124 const std::string com_name = options.get("command:", "<unknown_command>");
125 m_is_unittest_mode = nv::index::app::get_bool(options.get("unittest", "false"));
126
127 // print help and exit if -h
128 if(options.is_defined("h"))
129 {
130 std::cout << "info: Usage: " << com_name << " [option]\n"
131 << "Option: [-h]\n"
132 << " printout this message\n"
133 << " [-unittest {0|1}\n"
134 << " when true, unit test mode.\n"
135 << " [-dice::verbose severity_level]\n"
136 << " verbose severity level (3 is info.). (default: "
137 << options.get("dice::verbose")
138 << ")"
139 << std::endl;
140 exit(1);
141 }
142 return true;
143 }
144
145 // Logger configuration
147 mi::neuraylib::ILogging_configuration* logging_configuration,
148 nv::index::app::String_dict& option) CPP11_OVERRIDE
149 {
150 assert(logging_configuration != 0);
151 // This is for devsl=1 build to make severity level works for components and plugins.
152 // Otherwise you will see 'PLUGIN init' messages.
153 nv::index::app::Forwarding_logger::set_index_instance(get_index_interface());
154 set_forwarding_logger_installed(true);
155
156 mi::base::Handle<mi::base::ILogger> nvindex_forwarding_logger(
157 get_index_interface()->get_forwarding_logger());
158
159 // Log level configuration
160 // Note: experimental logger is the test for dice logger
161 // filter functionality. Currently this is not fully supported by IndeX.
162 info_cout("current log level = " + nv::index::app::to_string(logging_configuration->get_log_level()), option);
163 logging_configuration->set_log_level(mi::base::MESSAGE_SEVERITY_ERROR);
164 info_cout("set log level to " + nv::index::app::to_string(logging_configuration->get_log_level()), option);
165 nvindex_forwarding_logger->message(mi::base::MESSAGE_SEVERITY_INFO,
166 "APP:MAIN", "This info is filered out.\n");
167 logging_configuration->set_log_level(mi::base::MESSAGE_SEVERITY_INFO);
168
169 if(m_is_unittest_mode)
170 {
171 // unit test mode. Set log level warn
172 logging_configuration->set_log_level(mi::base::MESSAGE_SEVERITY_WARNING);
173 }
174 else
175 {
176 info_cout("set log level to " + nv::index::app::to_string(logging_configuration->get_log_level()), option);
177 nvindex_forwarding_logger->message(mi::base::MESSAGE_SEVERITY_INFO,
178 "APP:MAIN", "This should be seen.\n");
179 }
180
181 // Create and set my logger
182 mi::base::Handle<mi::base::ILogger> logger(new My_receiving_logger(!m_is_unittest_mode));
183 logging_configuration->set_receiving_logger(logger.get());
184
185 return true;
186 }
187
189 mi::neuraylib::INetwork_configuration* network_configuration,
190 nv::index::app::String_dict& options) CPP11_OVERRIDE
191 {
192 check_success(network_configuration != 0);
193
194 check_success(options.is_defined("unittest"));
195 const bool is_unittest = nv::index::app::get_bool(options.get("unittest"));
196 if (is_unittest)
197 {
198 info_cout("NETWORK: disabled networking mode.", options);
199 network_configuration->set_mode(mi::neuraylib::INetwork_configuration::MODE_OFF);
200 return true;
201 }
202
203 return initialize_networking_as_default_udp(network_configuration, options);
204 }
205
207 nv::index::IIndex_debug_configuration* index_debug_configuration,
208 nv::index::app::String_dict& options)
209 {
210 check_success(index_debug_configuration != 0);
211 index_debug_configuration->set_option("disable_sse=1");
212 info_cout("IIndex_debug_configuration: disabled sse.", options);
213
214 return true;
215 }
216};
217
218//----------------------------------------------------------------------
220{
221 nv::index::app::String_dict option = get_options();
222 info_cout(std::string("NVIDIA IndeX version: ") + get_index_interface()->get_version(), option);
223
224 // Remove the user defined receiving logger at runtime
225 mi::base::Handle<mi::neuraylib::ILogging_configuration> logging_configuration(
226 get_index_interface()->get_api_component<mi::neuraylib::ILogging_configuration>());
227 check_success(logging_configuration.is_valid_interface());
228 logging_configuration->set_receiving_logger(0);
229 info_cout("The receiving logger has been reset.", option);
230
231 return 0;
232}
233
234//----------------------------------------------------------------------
235// This example shows how to configure the IndeX library.
236int main(int argc, const char* argv[])
237{
238 nv::index::app::String_dict sdict;
239 sdict.insert("unittest", "0"); // default mode
240 sdict.insert("dice::verbose", "3");
241
242 // Initialize configure app
243 Configuration configuration;
244 configuration.initialize(argc, argv, sdict);
245 check_success(configuration.is_initialized());
246
247 // Launch the application. creating the scene and rendering.
248 const mi::Sint32 exit_code = configuration.launch();
249 INFO_LOG << "Shutting down ...";
250
251 return exit_code;
252}
virtual ~Configuration()
virtual bool evaluate_options(nv::index::app::String_dict &options) CPP11_OVERRIDE
virtual bool initialize_index_debug_configuration(nv::index::IIndex_debug_configuration *index_debug_configuration, nv::index::app::String_dict &options)
virtual bool initialize_networking(mi::neuraylib::INetwork_configuration *network_configuration, nv::index::app::String_dict &options) CPP11_OVERRIDE
mi::Sint32 launch()
bool initialize_logging(mi::neuraylib::ILogging_configuration *logging_configuration, nv::index::app::String_dict &option) CPP11_OVERRIDE
receiving logger example.
My_receiving_logger(bool is_show_destruct_message)
virtual ~My_receiving_logger()
virtual void message(mi::base::Message_severity level, const char *module_category, const mi::base::Message_details &, const char *message)
int main(int argc, const char *argv[])
#define check_success(expr)