2 //=============================================================================
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
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"
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"
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 ("")
56 # define OBJ_SUFFIX ACE_DLL_SUFFIX
57 # define OBJ_PREFIX "./" ACE_DLL_PREFIX
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
;
69 #endif /* ACE_NEEDS_DL_UNDERSCORE */
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
87 ACE_Reactor::instance ()->owner
88 (ACE_Thread_Manager::instance ()->thr_self ());
89 ACE_Reactor::instance ()->run_reactor_event_loop ();
93 // Initiate the cycle of messages.
96 void print_till_death ()
99 "\n-> start generating messages...\n"));
101 for (int i
= 0; i
< 1000; i
++)
107 ACE_DEBUG ((LM_DEBUG
,
108 " (%t) (%D) message\n"));
110 ACE_DEBUG ((LM_DEBUG
,
111 " (%t) %d message\n",
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.
131 ACE_TCHAR backup_ct
[MAXPATHLEN
+1];
132 ACE_DEBUG ((LM_DEBUG
,
133 "-> start counting...\n"));
138 ACE_OS::snprintf (backup_ct
, MAXPATHLEN
+ 1,
142 ACE_OS::snprintf (backup_ct
, MAXPATHLEN
+ 1,
147 stream
= ACE_OS::fopen (backup_ct
, ACE_TEXT ("r"));
153 ACE_OS::fclose (stream
);
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"),
170 ACE_DEBUG ((LM_DEBUG
,
171 ACE_TEXT (" Creating files...OK!")
172 ACE_TEXT (" Input value=%d, Checked value=%d"),
177 ACE_DEBUG ((LM_DEBUG
,
178 ACE_TEXT (" The number of files generated is: %d"),
181 ACE_DEBUG ((LM_DEBUG
,
182 "\n-< counting finished...\n"));
185 // get the file statistics
188 get_statistics (ACE_TCHAR
*f_name
)
192 // Get data associated with "file_name":
193 int result
= ACE_OS::stat (f_name
, &buf
);
195 // Check if statistics are valid:
197 ACE_OS::perror (ACE_TEXT ("\nProblem getting information"));
200 // Output some of the statistics:
201 ACE_DEBUG ((LM_DEBUG
,
202 ACE_TEXT (" File name : %s\n"),
204 ACE_DEBUG ((LM_DEBUG
,
205 ACE_TEXT (" File size (B): %d\n"),
208 ACE_DEBUG ((LM_DEBUG
,
209 ACE_TEXT (" Time modified : %s\n"),
210 ACE_OS::ctime (&buf
.st_mtime
)));
216 // analyse the file order
220 ACE_DEBUG ((LM_DEBUG
,
221 ACE_TEXT ("\n-> start testing order...\n")));
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"),
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,
241 ACE_OS::snprintf (backup_2
, MAXPATHLEN
+ 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"),
255 ACE_DEBUG ((LM_DEBUG
,
256 ACE_TEXT (" Ordering...OK!")));
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"),
267 ACE_DEBUG ((LM_DEBUG
,
268 ACE_TEXT (" Ordering...OK!")));
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")));
286 ACE_DEBUG ((LM_DEBUG
,
287 ACE_TEXT ("-> removing existent files...\n")));
295 ACE_TCHAR backup
[MAXPATHLEN
+1];
296 ACE_OS::snprintf (backup
, MAXPATHLEN
+ 1,
300 if (ACE_OS::unlink (backup
) != 0)
305 ACE_DEBUG ((LM_DEBUG
,
306 ACE_TEXT ("-< removing existing files...\n\n")));
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"));
317 while ((c
= get_opt ()) != EOF
)
322 file_name
= get_opt
.opt_arg ();
323 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("File name: %s\n"),
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"),
333 //FUZZ: enable check_for_lack_ACE_OS
336 max_size_files
= ACE_OS::atoi (get_opt
.opt_arg ());
337 ACE_DEBUG ((LM_DEBUG
,
338 ACE_TEXT ("Maximum size (KB): %d\n"),
342 ACE_DEBUG ((LM_DEBUG
,
343 ACE_TEXT ("Modes: %s\n"),
344 get_opt
.opt_arg ()));
347 max_num_files
= ACE_OS::atoi (get_opt
.opt_arg ());
348 ACE_DEBUG ((LM_DEBUG
,
349 ACE_TEXT ("Maximum files number: %d\n"),
353 ACE_DEBUG ((LM_DEBUG
,
354 ACE_TEXT ("Ordering files activated\n")));
358 ACE_DEBUG ((LM_DEBUG
,
359 ACE_TEXT ("Wipeout logfile activated\n")));
360 wipeout_logfile
= true;
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")),
383 ACE_UNUSED_ARG (wipeout_logfile
);
387 int run_main (int argc
, ACE_TCHAR
*argv
[])
389 ACE_START_TEST (ACE_TEXT ("Logging_Strategy_Test"));
391 ACE_TCHAR
*l_argv
[4];
395 if (parse_args (argc
, argv
) == -1)
396 ACE_ERROR_RETURN ((LM_ERROR
,
397 "Invalid command-line parameters.\n"),
402 l_argv
[0] = (ACE_TCHAR
*)ACE_TEXT ("Logging_Strategy_Test");
404 (ACE_TCHAR
*) ACE_TEXT ("-s")
406 ACE_TEXT ("log/Logging_Strategy_Test")
407 ACE_LOG_FILE_EXT_NAME
;
408 l_argv
[2] = (ACE_TCHAR
*) ACE_TEXT ("-o");
411 if (parse_args (3, l_argv
) == -1)
412 ACE_ERROR_RETURN ((LM_ERROR
,
413 "Invalid command-line parameters.\n"),
419 // Remove existing 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)
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()")
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
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)
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
479 // Counts the generated files
482 // Get the file order
485 // Wait for the thread to exit before we exit.
486 ACE_Thread_Manager::instance ()->wait ();