Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / examples / Log_Msg / test_log_msg.cpp
blob5eb33e13d29a9ab4e049f44969d70546aba686aa
2 //=============================================================================
3 /**
4 * @file test_log_msg.cpp
6 * This program tests the ACE_Log_Msg abstraction and demontrates
7 * several common use cases.
9 * @author Douglas Schmidt <d.schmidt@vanderbilt.edu>
11 //=============================================================================
14 #include "ace/OS_main.h"
16 // FUZZ: disable check_for_streams_include
17 #include "ace/streams.h"
19 #include "ace/Log_Msg.h"
20 #include "ace/OS_NS_unistd.h"
21 #include "ace/OS_NS_stdlib.h"
24 static void
25 cleanup ()
27 ACE_DEBUG ((LM_INFO,
28 "leaving (%P)!\n"));
31 static void
32 cause_error ()
34 errno = EWOULDBLOCK;
35 ACE_ERROR ((LM_DEBUG,
36 "would block\n"));
39 int
40 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
42 int counter = 1;
44 if (argc > 1) // Just give a dummy command-line argument to trigger this path.
46 if (ACE_LOG_MSG->open (argv[0],
47 ACE_Log_Msg::OSTREAM) == -1)
48 ACE_ERROR ((LM_ERROR,
49 "cannot open logger!!!\n"));
51 cause_error ();
52 // Check to see what happened.
53 if (ACE_LOG_MSG->op_status () == -1
54 && ACE_LOG_MSG->errnum () == EWOULDBLOCK)
55 ACE_DEBUG ((LM_DEBUG,
56 "op_status and errnum work!\n"));
57 else
58 ACE_ERROR ((LM_ERROR,
59 "op_status and errnum failed!\n"));
61 else // The default behavior is to log to STDERR...
63 if (ACE_LOG_MSG->open (argv[0]) == -1)
64 ACE_ERROR ((LM_ERROR,
65 "cannot open logger!!!\n"));
67 cause_error ();
69 // Check to see what happened.
70 if (ACE_LOG_MSG->op_status () == -1
71 && ACE_LOG_MSG->errnum () == EWOULDBLOCK)
72 ACE_DEBUG ((LM_DEBUG,
73 "op_status and errnum work!\n"));
74 else
75 ACE_ERROR ((LM_ERROR,
76 "op_status and errnum failed!\n"));
78 // Exercise many different combinations of STDERR and OSTREAM.
80 double f = 3.1416 * counter++;
81 int i = 10000;
83 ACE_DEBUG ((LM_INFO,
84 "%10f, %*s%s = %d\n",
87 "",
88 "hello",
89 i));
91 #if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
93 ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
94 ACE_LOG_MSG->msg_ostream (&cout);
96 f = 3.1416 * counter;
97 i = 10000 * counter++;
99 // This message will print twice - once for OSTREAM and once for
100 // STDERR.
102 ACE_DEBUG ((LM_INFO,
103 "%10f, %*s%s = %d\n",
107 "world",
108 i));
110 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR);
112 f = 3.1416 * counter;
113 i = 10000 * counter++;
115 ACE_DEBUG ((LM_INFO,
116 "%10f, %*s%s = %d\n",
120 "world",
121 i));
123 ACE_LOG_MSG->msg_ostream (0);
125 ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
127 f = 3.1416 * counter;
128 i = 10000 * counter++;
130 ACE_DEBUG ((LM_INFO,
131 "%10f, %*s%s = %d\n",
135 "world",
136 i));
138 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::OSTREAM);
139 ACE_LOG_MSG->msg_ostream (&cerr);
141 f = 3.1416 * counter;
142 i = 10000 * counter++;
144 ACE_DEBUG ((LM_INFO,
145 "%10f, %*s%s = %d\n",
149 "world",
150 i));
152 #endif /* !defined (ACE_LACKS_IOSTREAM_TOTALLY) */
154 static int array[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048};
156 // Print out the binary bytes of the array in hex form.
157 ACE_LOG_MSG->log_hexdump (LM_DEBUG,
158 (char *) array,
159 sizeof array);
161 // Disable the LM_DEBUG and LM_INFO messages at the process level.
162 u_long priority_mask =
163 ACE_LOG_MSG->priority_mask (ACE_Log_Msg::PROCESS);
164 ACE_CLR_BITS (priority_mask,
165 LM_DEBUG | LM_INFO);
166 ACE_LOG_MSG->priority_mask (priority_mask,
167 ACE_Log_Msg::PROCESS);
169 ACE_DEBUG ((LM_INFO,
170 "This LM_INFO message should not print!\n"));
171 ACE_DEBUG ((LM_DEBUG,
172 "This LM_DEBUG message should not print!\n"));
174 ACE_SET_BITS (priority_mask,
175 LM_INFO);
176 ACE_LOG_MSG->priority_mask (priority_mask,
177 ACE_Log_Msg::PROCESS);
179 ACE_DEBUG ((LM_INFO,
180 "This LM_INFO message should print!\n"));
181 ACE_DEBUG ((LM_DEBUG,
182 "This LM_DEBUG message should not print!\n"));
184 ACE_CLR_BITS (priority_mask, LM_INFO);
185 ACE_LOG_MSG->priority_mask (priority_mask,
186 ACE_Log_Msg::PROCESS);
188 ACE_DEBUG ((LM_INFO,
189 "This LM_INFO message should not print!\n"));
190 ACE_DEBUG ((LM_DEBUG,
191 "This LM_DEBUG message should not print!\n"));
193 char badname[] = "badname";
195 char *l_argv[2];
196 l_argv[0] = badname;
197 l_argv[1] = 0;
199 if (ACE_OS::execv (badname,
200 l_argv) == -1)
202 ACE_ERROR ((LM_ERROR,
203 "%n: (%x), %p%r\n",
204 10000,
205 badname,
206 cleanup));
207 ACE_OS::_exit ();
210 return 0;