Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / Logging / Logging_Test_i.cpp
blob60597175a72fdf147aaf2cb621cfdc2bd0059115
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 ()
15 // Do nothing
18 // Destructor
19 Logger_Client::~Logger_Client ()
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;
72 int
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"),
80 -1);
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)
90 ACE_DEBUG ((LM_DEBUG,
91 "\nFactory_ref resolved\n"));
93 if (CORBA::is_nil (factory_ref.in ()))
94 ACE_ERROR_RETURN ((LM_ERROR,
95 "resolved to nil object"),
96 -1);
97 if (TAO_debug_level > 0)
98 ACE_DEBUG ((LM_DEBUG,
99 "\nLogger_Factory resolved\n"));
101 // Narrow the factory and check the success
102 factory_ =
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"),
111 -1);
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",
122 str.in ()));
123 return 0;
127 Logger_Client::init_loggers ()
129 // Retrieve the Logger obj ref corresponding to key1 and
130 // key2.
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,
139 "nil logger1"),
140 -1);
142 if (CORBA::is_nil (this->logger_2_.in ()))
143 ACE_ERROR_RETURN ((LM_ERROR,
144 "nil logger2"),
145 -1);
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."));
160 else
161 ACE_DEBUG ((LM_DEBUG,
162 "\nResolution succeeded."));
165 catch (const CORBA::Exception& ex)
167 ex._tao_print_exception ("init_loggers");
168 return -1;
170 return 0;
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,
189 Logger::LM_DEBUG,
190 "log() test (1)\n");
192 // Setup the second log record
193 this->init_record (rec2,
194 Logger::LM_MAX,
195 "log() test (2)\n");
197 // Setup the third log record
198 this->init_record (rec3,
199 Logger::LM_INFO,
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
247 // seen again)
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");
258 return 0;
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"));
267 int c;
269 while ((c = get_opts ()) != -1)
270 switch (c)
272 case 'd': // debug flag
273 TAO_debug_level++;
274 break;
275 case '?':
276 default:
277 ACE_ERROR_RETURN ((LM_ERROR,
278 "usage: %s"
279 " [-d]"
280 "\n"
281 " -d: increase debug level\n",
282 this->argv_ [0]),
283 -1);
286 // Indicates successful parsing of command line.
287 return 0;
291 void
292 Logger_Client::init_record (Logger::Log_Record &newrec,
293 Logger::Log_Priority lp,
294 const char *msg)
296 // Copy the message data into newrec.
297 newrec.msg_data = CORBA::string_dup (msg);
299 // Assign the log priority.
300 newrec.type = lp;
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 ();
311 newrec.app_id = pid;
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);
317 newrec.host_addr =
318 (reinterpret_cast<in_addr *> (he->h_addr_list[0])->s_addr);
321 void
322 Logger_Client::show_record (Logger::Log_Record &newrec)
324 in_addr address;
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"
330 " Time: %d\n"
331 " PID: %ld\n"
332 " Host Address: %s\n"
333 " Message: %s\n",
334 newrec.type,
335 newrec.time,
336 newrec.app_id,
337 inet_ntoa (address),
338 newrec.msg_data.in ()));
339 //FUZZ: enable check_for_lack_ACE_OS