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 ()
19 Logger_Client::~Logger_Client ()
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");
73 Logger_Client::init_naming_service ()
75 // Initialize the naming services
76 if (my_name_client_
.init (orb_
.in ()) != 0)
77 ACE_ERROR_RETURN ((LM_ERROR
,
78 " (%P|%t) Unable to initialize "
79 "the TAO_Naming_Client.\n"),
82 // Resolve an instance of the Logger_Factory
83 CosNaming::Name
factory_name (1);
84 factory_name
.length (1);
85 factory_name
[0].id
= CORBA::string_dup ("Logger_Factory");
87 CORBA::Object_var factory_ref
=
88 my_name_client_
->resolve (factory_name
);
89 if (TAO_debug_level
> 0)
91 "\nFactory_ref resolved\n"));
93 if (CORBA::is_nil (factory_ref
.in ()))
94 ACE_ERROR_RETURN ((LM_ERROR
,
95 "resolved to nil object"),
97 if (TAO_debug_level
> 0)
99 "\nLogger_Factory resolved\n"));
101 // Narrow the factory and check the success
103 Logger_Factory::_narrow (factory_ref
.in ());
105 if (TAO_debug_level
> 0)
106 ACE_DEBUG ((LM_DEBUG
,
107 "\nFactory narrowed\n"));
108 if (CORBA::is_nil (factory_
.in ()))
109 ACE_ERROR_RETURN ((LM_ERROR
,
110 "narrow returned nil"),
112 if (TAO_debug_level
> 0)
113 ACE_DEBUG ((LM_DEBUG
,
114 "\nLogger_Factory narrowed\n"));
116 // If debugging, get the factory's IOR
117 CORBA::String_var str
=
118 orb_
->object_to_string (factory_
.in ());
119 if (TAO_debug_level
> 0)
120 ACE_DEBUG ((LM_DEBUG
,
121 "The factory IOR is <%s>\n",
127 Logger_Client::init_loggers ()
129 // Retrieve the Logger obj ref corresponding to key1 and
133 this->logger_1_
= factory_
->make_logger ("key1");
135 this->logger_2_
= factory_
->make_logger ("key2");
137 if (CORBA::is_nil (this->logger_1_
.in ()))
138 ACE_ERROR_RETURN ((LM_ERROR
,
142 if (CORBA::is_nil (this->logger_2_
.in ()))
143 ACE_ERROR_RETURN ((LM_ERROR
,
147 if (TAO_debug_level
> 0)
148 ACE_DEBUG ((LM_DEBUG
,
149 "Created two loggers\n"));
151 if (TAO_debug_level
> 0)
153 ACE_DEBUG ((LM_DEBUG
,
154 "\nTrying to resolve already created logger..."));
155 Logger_var logger_3
= factory_
->make_logger ("key1");
157 if (CORBA::is_nil (logger_3
.in ()))
158 ACE_DEBUG ((LM_DEBUG
,
159 "\nResolution failed."));
161 ACE_DEBUG ((LM_DEBUG
,
162 "\nResolution succeeded."));
165 catch (const CORBA::Exception
& ex
)
167 ex
._tao_print_exception ("init_loggers");
174 // Execute client example code.
177 Logger_Client::run ()
181 // Create 3 Log_Records for the test
182 Logger::Log_Record rec1
;
183 Logger::Log_Record rec2
;
184 Logger::Log_Record rec3
;
185 Logger::Log_Record rec4
;
187 // Setup the first log record
188 this->init_record (rec1
,
192 // Setup the second log record
193 this->init_record (rec2
,
197 // Setup the third log record
198 this->init_record (rec3
,
200 "logv() test (3)\n");
202 // Setup the fourth log record
203 this->init_record (rec4
,
204 Logger::LM_EMERGENCY
,
205 "log_twoway() test (4)\n");
207 // If debugging, output the new log records
208 if (TAO_debug_level
> 0)
210 ACE_DEBUG ((LM_DEBUG
,
211 "\nFirst Log_Record created. Contents:\n"));
212 this->show_record (rec1
);
214 ACE_DEBUG ((LM_DEBUG
,
215 "\nSecond Log_Record created. Contents:\n"));
216 this->show_record (rec2
);
218 ACE_DEBUG ((LM_DEBUG
,
219 "\nThird log record created. Contents:\n"));
220 this->show_record (rec3
);
222 ACE_DEBUG ((LM_DEBUG
,
223 "\nFourth log record created. Contents:\n"));
224 this->show_record (rec4
);
227 // Change the verbosity.
228 this->logger_1_
->verbosity (Logger::VERBOSE_LITE
);
230 // Log the first Log_Record (VERBOSE_LITE)
231 this->logger_1_
->log (rec1
);
233 // Change the verbosity again.
234 this->logger_2_
->verbosity (Logger::VERBOSE
);
236 // Log the second Log_Record (VERBOSE)
237 this->logger_2_
->log (rec2
);
239 // Change the verbosity again
240 this->logger_2_
->verbosity (Logger::SILENT
);
242 // Log the third log record using logv() (this shows if the
243 // verbosity level overrides the logger's verbosity level)
244 this->logger_2_
->logv (rec3
, Logger::VERBOSE
);
246 // Change the verbosity again (so that regular log msgs can be
248 this->logger_2_
->verbosity (Logger::VERBOSE
);
250 // Log the fourth record using log_twoway()
251 this->logger_2_
->log_twoway (rec4
);
254 catch (const CORBA::Exception
& ex
)
256 ex
._tao_print_exception ("run");
261 // Parses the command line arguments and returns an error status.
264 Logger_Client::parse_args ()
266 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("d"));
269 while ((c
= get_opts ()) != -1)
272 case 'd': // debug flag
277 ACE_ERROR_RETURN ((LM_ERROR
,
281 " -d: increase debug level\n",
286 // Indicates successful parsing of command line.
292 Logger_Client::init_record (Logger::Log_Record
&newrec
,
293 Logger::Log_Priority lp
,
296 // Copy the message data into newrec.
297 newrec
.msg_data
= CORBA::string_dup (msg
);
299 // Assign the log priority.
302 //FUZZ: disable check_for_lack_ACE_OS
303 // Create and assign the timestamp.
304 ACE_Time_Value
time (ACE_OS::gettimeofday ());
305 //FUZZ: enable check_for_lack_ACE_OS
307 newrec
.time
= static_cast<CORBA::Long
> (time
.sec ());
309 // Get and store the PID of the calling process.
310 pid_t pid
= ACE_OS::getpid ();
313 // Get and store the IP of the local host .
314 char name
[MAXHOSTNAMELEN
];
315 ACE_OS::hostname (name
, MAXHOSTNAMELEN
);
316 hostent
*he
= ACE_OS::gethostbyname (name
);
318 (reinterpret_cast<in_addr
*> (he
->h_addr_list
[0])->s_addr
);
322 Logger_Client::show_record (Logger::Log_Record
&newrec
)
325 address
.s_addr
= newrec
.host_addr
;
327 //FUZZ: disable check_for_lack_ACE_OS
328 ACE_DEBUG ((LM_DEBUG
,
329 " Log Priority: %d\n"
332 " Host Address: %s\n"
338 newrec
.msg_data
.in ()));
339 //FUZZ: enable check_for_lack_ACE_OS