Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Logging / Logging_Test_i.cpp
blob865adcf7d552680f5542e18713f3ab2581fbda41
1 #include "LoggerC.h"
2 #include "Logging_Test_i.h"
3 #include "orbsvcs/CosNamingC.h"
4 #include "tao/debug.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"
12 // Constructor
13 Logger_Client::Logger_Client (void)
15 // Do nothing
18 // Destructor
19 Logger_Client::~Logger_Client (void)
21 // Do nothing
24 int
25 Logger_Client::init (int argc, ACE_TCHAR *argv[])
27 this->argc_ = argc;
28 this->argv_ = argv;
30 try
32 if (TAO_debug_level > 0)
33 ACE_DEBUG ((LM_DEBUG,
34 "\nTrying to initialize orb\n"));
35 // Initialize the ORB
36 orb_ = CORBA::ORB_init (argc,
37 argv,
38 "internet");
40 if (TAO_debug_level > 0)
41 ACE_DEBUG ((LM_DEBUG,
42 "\nOrb initialized successfully\n"));
44 // Parse command line and verify parameters.
45 if (this->parse_args () == -1)
46 return -1;
48 // Initialize the naming service
49 int ret = this->init_naming_service ();
50 if (ret != 0)
51 ACE_ERROR_RETURN ((LM_ERROR,
52 " (%P|%t) Unable to initialize naming"
53 "services.\n"),
54 -1);
55 // Create the logger instances
56 ret = this->init_loggers ();
57 if (ret != 0)
58 ACE_ERROR_RETURN ((LM_ERROR,
59 " (%P|%t) Unable to initialize logger"
60 "instances.\n"),
61 -1);
63 catch (const CORBA::Exception& ex)
65 ex._tao_print_exception ("init");
66 return -1;
69 return 0;
73 int
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"),
81 -1);
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)
91 ACE_DEBUG ((LM_DEBUG,
92 "\nFactory_ref resolved\n"));
94 if (CORBA::is_nil (factory_ref.in ()))
95 ACE_ERROR_RETURN ((LM_ERROR,
96 "resolved to nil object"),
97 -1);
98 if (TAO_debug_level > 0)
99 ACE_DEBUG ((LM_DEBUG,
100 "\nLogger_Factory resolved\n"));
102 // Narrow the factory and check the success
103 factory_ =
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"),
112 -1);
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",
123 str.in ()));
124 return 0;
128 Logger_Client::init_loggers (void)
130 // Retrieve the Logger obj ref corresponding to key1 and
131 // key2.
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,
140 "nil logger1"),
141 -1);
143 if (CORBA::is_nil (this->logger_2_.in ()))
144 ACE_ERROR_RETURN ((LM_ERROR,
145 "nil logger2"),
146 -1);
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."));
161 else
162 ACE_DEBUG ((LM_DEBUG,
163 "\nResolution succeeded."));
166 catch (const CORBA::Exception& ex)
168 ex._tao_print_exception ("init_loggers");
169 return -1;
171 return 0;
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,
190 Logger::LM_DEBUG,
191 "log() test (1)\n");
193 // Setup the second log record
194 this->init_record (rec2,
195 Logger::LM_MAX,
196 "log() test (2)\n");
198 // Setup the third log record
199 this->init_record (rec3,
200 Logger::LM_INFO,
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
248 // seen again)
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");
259 return 0;
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"));
268 int c;
270 while ((c = get_opts ()) != -1)
271 switch (c)
273 case 'd': // debug flag
274 TAO_debug_level++;
275 break;
276 case '?':
277 default:
278 ACE_ERROR_RETURN ((LM_ERROR,
279 "usage: %s"
280 " [-d]"
281 "\n"
282 " -d: increase debug level\n",
283 this->argv_ [0]),
284 -1);
287 // Indicates successful parsing of command line.
288 return 0;
292 void
293 Logger_Client::init_record (Logger::Log_Record &newrec,
294 Logger::Log_Priority lp,
295 const char *msg)
297 // Copy the message data into newrec.
298 newrec.msg_data = CORBA::string_dup (msg);
300 // Assign the log priority.
301 newrec.type = lp;
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 ();
312 newrec.app_id = pid;
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);
318 newrec.host_addr =
319 (reinterpret_cast<in_addr *> (he->h_addr_list[0])->s_addr);
322 void
323 Logger_Client::show_record (Logger::Log_Record &newrec)
325 in_addr address;
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"
331 " Time: %d\n"
332 " PID: %ld\n"
333 " Host Address: %s\n"
334 " Message: %s\n",
335 newrec.type,
336 newrec.time,
337 newrec.app_id,
338 inet_ntoa (address),
339 newrec.msg_data.in ()));
340 //FUZZ: enable check_for_lack_ACE_OS