Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / ACE / tests / Logging_Strategy_Test.cpp
blob9132100a912bf6890e8f001b92457a7970537d18
2 //=============================================================================
3 /**
4 * @file Logging_Strategy_Test.cpp
6 * This program tests the <ACE_Logging_Strategy> class in various
7 * ways and also illustrates many of the features of the
8 * <ACE_Log_Msg>. The test works as follows:
9 * -Load the inserted arguments;
10 * -Remove existent log_files with the file_name specified by the
11 * user;
12 * -Generate 1000 messages to create the DEBUG statements to be
13 * stored in the files;
14 * -Counts the created log_files and if it was specified a maximum
15 * number of log_files, compare and verify if they are the same.
16 * -Verify the order of the files with the order argument.
18 * When Dlls are used, we utilize the dynamic service
19 * configuration mechanism to activate the logging strategy. This
20 * is not a must though, and you may activate the logging strategy
21 * as described in the non-DLL section below under DLL
22 * environments as well.
24 * @author Orlando Ribeiro <oribeiro@inescporto.pt>
26 //=============================================================================
28 #include "test_config.h"
29 #include "ace/OS_NS_stdio.h"
30 #include "ace/OS_NS_string.h"
31 #include "ace/OS_NS_unistd.h"
32 #include "ace/OS_NS_sys_stat.h"
33 #include <memory>
34 #include "ace/Service_Config.h"
35 #include "ace/Reactor.h"
36 #include "ace/Thread_Manager.h"
38 #if defined (ACE_AS_STATIC_LIBS) || \
39 (!defined (ACE_WIN32) && !defined (ACE_HAS_SVR4_DYNAMIC_LINKING))
40 #include "ace/Logging_Strategy.h"
41 #endif
43 #include "ace/Auto_Ptr.cpp"
44 #include "ace/Get_Opt.h"
45 #include "ace/OS_NS_time.h"
47 // Considering UNIX OS to be default. On Win32 platforms, the symbols
48 // are got form the .exe as one cant have .exe and .dll for the same
49 // .cpp. Also, on Win32 platforms one cant use the .obj to obtain
50 // symbols dynamically at runtime.
52 #if defined (ACE_WIN32)
53 # define OBJ_SUFFIX ACE_TEXT (".exe")
54 # define OBJ_PREFIX ACE_TEXT ("")
55 #else
56 # define OBJ_SUFFIX ACE_DLL_SUFFIX
57 # define OBJ_PREFIX "./" ACE_DLL_PREFIX
58 #endif /*ACE_WIN32*/
60 ACE_TCHAR const *
61 cdecl_decoration (ACE_TCHAR const *func_name)
63 #if defined(ACE_NEEDS_DL_UNDERSCORE)
64 static ACE_TCHAR decorated_func_name[10*1024];
65 ACE_OS::snprintf (decorated_func_name, 10*1024, ACE_TEXT ("_%s"), func_name);
66 return decorated_func_name;
67 #else
68 return func_name;
69 #endif /* ACE_NEEDS_DL_UNDERSCORE */
72 // Global variables.
73 static ACE_TCHAR *file_name = 0;
74 static int max_size_files = 0;
75 static int max_num_files = 0;
76 static int interval_time = 0;
77 static bool order_state = false;
78 static int num_files = 0;
79 static bool wipeout_logfile = false;
81 // This adapter function runs the Reactor's event loop in a separate
82 // thread of control.
84 static void *
85 run_reactor (void *)
87 ACE_Reactor::instance ()->owner
88 (ACE_Thread_Manager::instance ()->thr_self ());
89 ACE_Reactor::instance ()->run_reactor_event_loop ();
90 return 0;
93 // Initiate the cycle of messages.
95 static
96 void print_till_death ()
98 ACE_DEBUG ((LM_DEBUG,
99 "\n-> start generating messages...\n"));
101 for (int i = 0; i < 1000; i++)
103 if (i % 50 == 0)
104 ACE_OS::sleep (1);
106 if (i == 0)
107 ACE_DEBUG ((LM_DEBUG,
108 " (%t) (%D) message\n"));
109 else
110 ACE_DEBUG ((LM_DEBUG,
111 " (%t) %d message\n",
112 i));
115 if (ACE_Reactor::instance ()->end_reactor_event_loop () == -1)
116 ACE_ERROR ((LM_ERROR,
117 "Error ending reactor.\n"));
119 ACE_DEBUG ((LM_DEBUG,
120 "-< generating messages finished \n\n"));
123 // Count the generated files.
125 static void
126 count_files ()
128 int i = 0;
129 int error = 0;
130 FILE *stream;
131 ACE_TCHAR backup_ct[MAXPATHLEN+1];
132 ACE_DEBUG ((LM_DEBUG,
133 "-> start counting...\n"));
137 if (i == 0)
138 ACE_OS::snprintf (backup_ct, MAXPATHLEN + 1,
139 ACE_TEXT ("%s"),
140 file_name);
141 else
142 ACE_OS::snprintf (backup_ct, MAXPATHLEN + 1,
143 ACE_TEXT ("%s.%d"),
144 file_name,
147 stream = ACE_OS::fopen (backup_ct, ACE_TEXT ("r"));
148 if (stream == 0)
149 error = 1;
150 else
152 i++;
153 ACE_OS::fclose (stream);
157 while (error != 1);
159 num_files = i;
161 if (max_num_files !=0)
163 if (max_num_files != num_files)
164 ACE_DEBUG ((LM_DEBUG,
165 ACE_TEXT ("Creating files...Failed!")
166 ACE_TEXT (" Input value=%d, Checked value=%d"),
167 max_num_files,
168 num_files));
169 else
170 ACE_DEBUG ((LM_DEBUG,
171 ACE_TEXT (" Creating files...OK!")
172 ACE_TEXT (" Input value=%d, Checked value=%d"),
173 max_num_files,
174 num_files));
176 else
177 ACE_DEBUG ((LM_DEBUG,
178 ACE_TEXT (" The number of files generated is: %d"),
179 num_files));
181 ACE_DEBUG ((LM_DEBUG,
182 "\n-< counting finished...\n"));
185 // get the file statistics
187 static time_t
188 get_statistics (ACE_TCHAR *f_name)
190 ACE_stat buf;
192 // Get data associated with "file_name":
193 int result = ACE_OS::stat (f_name, &buf);
195 // Check if statistics are valid:
196 if (result != 0)
197 ACE_OS::perror (ACE_TEXT ("\nProblem getting information"));
198 else
200 // Output some of the statistics:
201 ACE_DEBUG ((LM_DEBUG,
202 ACE_TEXT (" File name : %s\n"),
203 f_name));
204 ACE_DEBUG ((LM_DEBUG,
205 ACE_TEXT (" File size (B): %d\n"),
206 buf.st_size));
208 ACE_DEBUG ((LM_DEBUG,
209 ACE_TEXT (" Time modified : %s\n"),
210 ACE_OS::ctime (&buf.st_mtime)));
213 return buf.st_mtime;
216 // analyse the file order
217 static void
218 order ()
220 ACE_DEBUG ((LM_DEBUG,
221 ACE_TEXT ("\n-> start testing order...\n")));
223 if (num_files <= 2)
225 if (num_files == 1)
226 get_statistics (file_name);
227 ACE_DEBUG ((LM_DEBUG,
228 ACE_TEXT (" Ordering...OK! - ")
229 ACE_TEXT (" Only %d file (s) was (were) generated"),
230 num_files));
232 else
234 time_t tm_bk_1, tm_bk_2;
235 ACE_TCHAR backup_1[MAXPATHLEN+1];
236 ACE_TCHAR backup_2[MAXPATHLEN+1];
237 ACE_OS::snprintf (backup_1, MAXPATHLEN + 1,
238 ACE_TEXT ("%s.%d"),
239 file_name,
241 ACE_OS::snprintf (backup_2, MAXPATHLEN + 1,
242 ACE_TEXT ("%s.%d"),
243 file_name,
244 num_files - 1);
246 tm_bk_1 = get_statistics (backup_1);
247 tm_bk_2 = get_statistics (backup_2);
249 if (tm_bk_1 > tm_bk_2 && !order_state)
251 ACE_DEBUG ((LM_DEBUG,
252 ACE_TEXT (" %s (newest) ; %s (oldest)\n"),
253 backup_1,
254 backup_2));
255 ACE_DEBUG ((LM_DEBUG,
256 ACE_TEXT (" Ordering...OK!")));
258 else
260 if (tm_bk_1 < tm_bk_2 && order_state)
262 ACE_DEBUG ((LM_DEBUG,
263 ACE_TEXT (" %s (newest);")
264 ACE_TEXT ("%s (oldest)\n"),
265 backup_2,
266 backup_1));
267 ACE_DEBUG ((LM_DEBUG,
268 ACE_TEXT (" Ordering...OK!")));
270 else
271 ACE_DEBUG ((LM_DEBUG,
272 ACE_TEXT (" Ordering...FAILED!")
273 ACE_TEXT ("- The files are disorderly")));
277 ACE_DEBUG ((LM_DEBUG,
278 ACE_TEXT ("\n-< testing order finished...\n\n")));
281 // remove log_files
283 static void
284 remove_files ()
286 ACE_DEBUG ((LM_DEBUG,
287 ACE_TEXT ("-> removing existent files...\n")));
289 int error = 0;
290 int i = 0;
294 ++i;
295 ACE_TCHAR backup[MAXPATHLEN+1];
296 ACE_OS::snprintf (backup, MAXPATHLEN + 1,
297 ACE_TEXT ("%s.%d"),
298 file_name,
300 if (ACE_OS::unlink (backup) != 0)
301 error = 1;
303 while (error != 1);
305 ACE_DEBUG ((LM_DEBUG,
306 ACE_TEXT ("-< removing existing files...\n\n")));
309 static int
310 parse_args (int argc, ACE_TCHAR *argv[])
312 ACE_DEBUG ((LM_DEBUG,
313 ACE_TEXT ("Specifications:\n")));
314 ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("s:i:m:f:N:ow"));
315 int c;
317 while ((c = get_opt ()) != EOF)
319 switch (c)
321 case 's':
322 file_name = get_opt.opt_arg ();
323 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("File name: %s\n"),
324 file_name));
325 break;
326 case 'i':
327 interval_time = ACE_OS::atoi (get_opt.opt_arg ());
329 //FUZZ: disable check_for_lack_ACE_OS
330 ACE_DEBUG ((LM_DEBUG,
331 ACE_TEXT ("Interval time (s): %d\n"),
332 interval_time));
333 //FUZZ: enable check_for_lack_ACE_OS
334 break;
335 case 'm':
336 max_size_files = ACE_OS::atoi (get_opt.opt_arg ());
337 ACE_DEBUG ((LM_DEBUG,
338 ACE_TEXT ("Maximum size (KB): %d\n"),
339 max_size_files));
340 break;
341 case 'f':
342 ACE_DEBUG ((LM_DEBUG,
343 ACE_TEXT ("Modes: %s\n"),
344 get_opt.opt_arg ()));
345 break;
346 case 'N':
347 max_num_files = ACE_OS::atoi (get_opt.opt_arg ());
348 ACE_DEBUG ((LM_DEBUG,
349 ACE_TEXT ("Maximum files number: %d\n"),
350 max_num_files));
351 break;
352 case 'o':
353 ACE_DEBUG ((LM_DEBUG,
354 ACE_TEXT ("Ordering files activated\n")));
355 order_state = true;
356 break;
357 case 'w':
358 ACE_DEBUG ((LM_DEBUG,
359 ACE_TEXT ("Wipeout logfile activated\n")));
360 wipeout_logfile = true;
361 break;
362 default:
363 ACE_ERROR_RETURN
364 ((LM_ERROR,
365 ACE_TEXT ("usage: [-s]<file_name>")
366 ACE_TEXT ("[-i]<sample_interval> ")
367 ACE_TEXT ("[-m]<max_size> [-f]<msg_flags> ")
368 ACE_TEXT ("[-n]<num_files> [-o]\n")
369 ACE_TEXT ("\t-s: Specify the name of the log files.\n")
370 ACE_TEXT ("\t-i: Define the sample interval in secs.\n")
371 ACE_TEXT ("\t-m: Define the max size for the log_files in KB.\n")
372 ACE_TEXT ("\t-f: Indicates the Log_Msg flags.\n")
373 ACE_TEXT ("\t-N: Define the maximum number of log_files.\n")
374 ACE_TEXT ("\t-o: If activated puts the log_files ordered.\n"),
375 ACE_TEXT ("\t-w: If activated cause the logfile to be wiped out,")
376 ACE_TEXT (" both on startup and on reconfigure.\n")),
377 -1);
378 /* NOTREACHED */
379 break;
383 ACE_UNUSED_ARG (wipeout_logfile);
384 return 0;
387 int run_main (int argc, ACE_TCHAR *argv [])
389 ACE_START_TEST (ACE_TEXT ("Logging_Strategy_Test"));
391 ACE_TCHAR *l_argv[4];
393 if (argc > 1)
395 if (parse_args (argc, argv) == -1)
396 ACE_ERROR_RETURN ((LM_ERROR,
397 "Invalid command-line parameters.\n"),
400 else
402 l_argv[0] = (ACE_TCHAR *)ACE_TEXT ("Logging_Strategy_Test");
403 l_argv[1] =
404 (ACE_TCHAR *) ACE_TEXT ("-s")
405 ACE_DEFAULT_TEST_DIR
406 ACE_TEXT ("log/Logging_Strategy_Test")
407 ACE_LOG_FILE_EXT_NAME;
408 l_argv[2] = (ACE_TCHAR *) ACE_TEXT ("-o");
409 l_argv[3] = 0;
411 if (parse_args (3, l_argv) == -1)
412 ACE_ERROR_RETURN ((LM_ERROR,
413 "Invalid command-line parameters.\n"),
415 argv = l_argv;
416 argc = 3;
419 // Remove existing files.
420 remove_files ();
422 // This is necessary only if the provided logfile name is the same
423 // as the default name. If so, nothing will be written as the
424 // previous ofstream is closed only at the end (ACE_END_TEST)
425 ACE_CLOSE_TEST_LOG;
427 // When Dlls are used, we utilize the dynamic service configuration
428 // mechanism to activate the logging strategy. This is not a must
429 // though, and you may activate the logging strategy as described in
430 // the non-DLL section below under DLL environments as well.
432 #if !defined (ACE_AS_STATIC_LIBS) && (defined (ACE_WIN32) || defined (ACE_HAS_SVR4_DYNAMIC_LINKING))
434 // Platform support DLLs, and not configured to link statically
435 ACE_TCHAR arg_str[250];
436 ACE_OS::snprintf (arg_str, 250,
437 ACE_TEXT ("dynamic Logger Service_Object ")
438 ACE_TEXT ("*ACE:_make_ACE_Logging_Strategy()")
439 ACE_TEXT ("\""));
441 for (int i = 1; i < argc; i++)
443 ACE_OS::strcat (arg_str, argv[i]);
444 ACE_OS::strcat (arg_str, ACE_TEXT (" "));
447 ACE_OS::strcat (arg_str, ACE_TEXT ("\""));
449 if (ACE_Service_Config::process_directive (arg_str) == -1)
450 ACE_ERROR_RETURN ((LM_ERROR,
451 "Error opening _make_ACE_Log_Strategy.\n"),
453 #else // Platform doesn't support DLLs, or configured to link
454 // statically
455 ACE_Logging_Strategy logging_strategy;
456 unsigned char ls_argc = argc - 1;
457 std::unique_ptr<ACE_TCHAR *> ls_argv (new ACE_TCHAR *[ls_argc]);
459 for (unsigned char c = 0; c < ls_argc; c++)
460 (ls_argv.get ())[c] = argv[c+1];
462 if (logging_strategy.init (ls_argc, ls_argv.get ()) == -1)
463 ACE_ERROR_RETURN
464 ((LM_ERROR,
465 "Error initializing the ACE_Logging_Strategy.\n"),
467 #endif /* !ACE_AS_STATIC_LIBS && (ACE_WIN32 ACE_HAS_SVR4_DYNAMIC_LINKING) */
469 // launch a new Thread
470 if (ACE_Thread_Manager::instance ()->spawn
471 (ACE_THR_FUNC (run_reactor)) == -1)
472 ACE_ERROR_RETURN ((LM_ERROR,
473 "Spawning Reactor.\n"),
476 // Function to print the message
477 print_till_death ();
479 // Counts the generated files
480 count_files ();
482 // Get the file order
483 order ();
485 // Wait for the thread to exit before we exit.
486 ACE_Thread_Manager::instance ()->wait ();
487 ACE_END_TEST;
488 return 0;