2 #include "Logging_Test_i.h"
3 #include "orbsvcs/CosNamingC.h"
5 #include "ace/INET_Addr.h"
6 #include "ace/SOCK_Dgram_Mcast.h"
7 #include "ace/OS_NS_netdb.h"
8 #include "ace/OS_NS_unistd.h"
9 #include "ace/OS_NS_arpa_inet.h"
10 #include "ace/OS_NS_sys_time.h"
13 Logger_Client::Logger_Client (void)
19 Logger_Client::~Logger_Client (void)
25 Logger_Client::init (int argc
, ACE_TCHAR
*argv
[])
32 if (TAO_debug_level
> 0)
34 "\nTrying to initialize orb\n"));
36 orb_
= CORBA::ORB_init (argc
,
40 if (TAO_debug_level
> 0)
42 "\nOrb initialized successfully\n"));
44 // Parse command line and verify parameters.
45 if (this->parse_args () == -1)
48 // Initialize the naming service
49 int ret
= this->init_naming_service ();
51 ACE_ERROR_RETURN ((LM_ERROR
,
52 " (%P|%t) Unable to initialize naming"
55 // Create the logger instances
56 ret
= this->init_loggers ();
58 ACE_ERROR_RETURN ((LM_ERROR
,
59 " (%P|%t) Unable to initialize logger"
63 catch (const CORBA::Exception
& ex
)
65 ex
._tao_print_exception ("init");
74 Logger_Client::init_naming_service (void)
76 // Initialize the naming services
77 if (my_name_client_
.init (orb_
.in ()) != 0)
78 ACE_ERROR_RETURN ((LM_ERROR
,
79 " (%P|%t) Unable to initialize "
80 "the TAO_Naming_Client.\n"),
83 // Resolve an instance of the Logger_Factory
84 CosNaming::Name
factory_name (1);
85 factory_name
.length (1);
86 factory_name
[0].id
= CORBA::string_dup ("Logger_Factory");
88 CORBA::Object_var factory_ref
=
89 my_name_client_
->resolve (factory_name
);
90 if (TAO_debug_level
> 0)
92 "\nFactory_ref resolved\n"));
94 if (CORBA::is_nil (factory_ref
.in ()))
95 ACE_ERROR_RETURN ((LM_ERROR
,
96 "resolved to nil object"),
98 if (TAO_debug_level
> 0)
100 "\nLogger_Factory resolved\n"));
102 // Narrow the factory and check the success
104 Logger_Factory::_narrow (factory_ref
.in ());
106 if (TAO_debug_level
> 0)
107 ACE_DEBUG ((LM_DEBUG
,
108 "\nFactory narrowed\n"));
109 if (CORBA::is_nil (factory_
.in ()))
110 ACE_ERROR_RETURN ((LM_ERROR
,
111 "narrow returned nil"),
113 if (TAO_debug_level
> 0)
114 ACE_DEBUG ((LM_DEBUG
,
115 "\nLogger_Factory narrowed\n"));
117 // If debugging, get the factory's IOR
118 CORBA::String_var str
=
119 orb_
->object_to_string (factory_
.in ());
120 if (TAO_debug_level
> 0)
121 ACE_DEBUG ((LM_DEBUG
,
122 "The factory IOR is <%s>\n",
128 Logger_Client::init_loggers (void)
130 // Retrieve the Logger obj ref corresponding to key1 and
134 this->logger_1_
= factory_
->make_logger ("key1");
136 this->logger_2_
= factory_
->make_logger ("key2");
138 if (CORBA::is_nil (this->logger_1_
.in ()))
139 ACE_ERROR_RETURN ((LM_ERROR
,
143 if (CORBA::is_nil (this->logger_2_
.in ()))
144 ACE_ERROR_RETURN ((LM_ERROR
,
148 if (TAO_debug_level
> 0)
149 ACE_DEBUG ((LM_DEBUG
,
150 "Created two loggers\n"));
152 if (TAO_debug_level
> 0)
154 ACE_DEBUG ((LM_DEBUG
,
155 "\nTrying to resolve already created logger..."));
156 Logger_var logger_3
= factory_
->make_logger ("key1");
158 if (CORBA::is_nil (logger_3
.in ()))
159 ACE_DEBUG ((LM_DEBUG
,
160 "\nResolution failed."));
162 ACE_DEBUG ((LM_DEBUG
,
163 "\nResolution succeeded."));
166 catch (const CORBA::Exception
& ex
)
168 ex
._tao_print_exception ("init_loggers");
175 // Execute client example code.
178 Logger_Client::run (void)
182 // Create 3 Log_Records for the test
183 Logger::Log_Record rec1
;
184 Logger::Log_Record rec2
;
185 Logger::Log_Record rec3
;
186 Logger::Log_Record rec4
;
188 // Setup the first log record
189 this->init_record (rec1
,
193 // Setup the second log record
194 this->init_record (rec2
,
198 // Setup the third log record
199 this->init_record (rec3
,
201 "logv() test (3)\n");
203 // Setup the fourth log record
204 this->init_record (rec4
,
205 Logger::LM_EMERGENCY
,
206 "log_twoway() test (4)\n");
208 // If debugging, output the new log records
209 if (TAO_debug_level
> 0)
211 ACE_DEBUG ((LM_DEBUG
,
212 "\nFirst Log_Record created. Contents:\n"));
213 this->show_record (rec1
);
215 ACE_DEBUG ((LM_DEBUG
,
216 "\nSecond Log_Record created. Contents:\n"));
217 this->show_record (rec2
);
219 ACE_DEBUG ((LM_DEBUG
,
220 "\nThird log record created. Contents:\n"));
221 this->show_record (rec3
);
223 ACE_DEBUG ((LM_DEBUG
,
224 "\nFourth log record created. Contents:\n"));
225 this->show_record (rec4
);
228 // Change the verbosity.
229 this->logger_1_
->verbosity (Logger::VERBOSE_LITE
);
231 // Log the first Log_Record (VERBOSE_LITE)
232 this->logger_1_
->log (rec1
);
234 // Change the verbosity again.
235 this->logger_2_
->verbosity (Logger::VERBOSE
);
237 // Log the second Log_Record (VERBOSE)
238 this->logger_2_
->log (rec2
);
240 // Change the verbosity again
241 this->logger_2_
->verbosity (Logger::SILENT
);
243 // Log the third log record using logv() (this shows if the
244 // verbosity level overrides the logger's verbosity level)
245 this->logger_2_
->logv (rec3
, Logger::VERBOSE
);
247 // Change the verbosity again (so that regular log msgs can be
249 this->logger_2_
->verbosity (Logger::VERBOSE
);
251 // Log the fourth record using log_twoway()
252 this->logger_2_
->log_twoway (rec4
);
255 catch (const CORBA::Exception
& ex
)
257 ex
._tao_print_exception ("run");
262 // Parses the command line arguments and returns an error status.
265 Logger_Client::parse_args (void)
267 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("d"));
270 while ((c
= get_opts ()) != -1)
273 case 'd': // debug flag
278 ACE_ERROR_RETURN ((LM_ERROR
,
282 " -d: increase debug level\n",
287 // Indicates successful parsing of command line.
293 Logger_Client::init_record (Logger::Log_Record
&newrec
,
294 Logger::Log_Priority lp
,
297 // Copy the message data into newrec.
298 newrec
.msg_data
= CORBA::string_dup (msg
);
300 // Assign the log priority.
303 //FUZZ: disable check_for_lack_ACE_OS
304 // Create and assign the timestamp.
305 ACE_Time_Value
time (ACE_OS::gettimeofday ());
306 //FUZZ: enable check_for_lack_ACE_OS
308 newrec
.time
= static_cast<CORBA::Long
> (time
.sec ());
310 // Get and store the PID of the calling process.
311 pid_t pid
= ACE_OS::getpid ();
314 // Get and store the IP of the local host .
315 char name
[MAXHOSTNAMELEN
];
316 ACE_OS::hostname (name
, MAXHOSTNAMELEN
);
317 hostent
*he
= ACE_OS::gethostbyname (name
);
319 (reinterpret_cast<in_addr
*> (he
->h_addr_list
[0])->s_addr
);
323 Logger_Client::show_record (Logger::Log_Record
&newrec
)
326 address
.s_addr
= newrec
.host_addr
;
328 //FUZZ: disable check_for_lack_ACE_OS
329 ACE_DEBUG ((LM_DEBUG
,
330 " Log Priority: %d\n"
333 " Host Address: %s\n"
339 newrec
.msg_data
.in ()));
340 //FUZZ: enable check_for_lack_ACE_OS