summaryrefslogtreecommitdiff
path: root/TAO/examples/Logging/Logging_Test_i.cpp
blob: d04a9f713e72f2dab06a9caa5bce80da0d4048d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// $Id$

#include "LoggerC.h"
#include "Logging_Test_i.h"
#include "orbsvcs/CosNamingC.h"
#include "tao/debug.h"
#include "ace/INET_Addr.h"
#include "ace/SOCK_Dgram_Mcast.h"
#include "ace/OS_NS_netdb.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_arpa_inet.h"
#include "ace/OS_NS_sys_time.h"

ACE_RCSID (Logger, 
           Logging_Test_i, 
           "$Id$")

  // Constructor
  Logger_Client::Logger_Client (void)
{
  // Do nothing
}

// Destructor
Logger_Client::~Logger_Client (void)
{
  // Do nothing
}

int
Logger_Client::init (int argc, char *argv[])
{
  this->argc_ = argc;
  this->argv_ = argv;

  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "\nTrying to initialize orb\n"));
      // Initialize the ORB
      orb_ = CORBA::ORB_init (argc,
                              argv,
                              "internet"
                              ACE_ENV_ARG_PARAMETER);

      ACE_TRY_CHECK;
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "\nOrb initialized successfully\n"));

      // Parse command line and verify parameters.
      if (this->parse_args () == -1)
        return -1;

      // Initialize the naming service
      int ret = this->init_naming_service (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
      if (ret != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize naming"
                           "services.\n"),
                          -1);
      // Create the logger instances
      ret = this->init_loggers (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_TRY_CHECK;
      if (ret != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize logger"
                           "instances.\n"),
                          -1);
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "init");
      return -1;
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);

  return 0;

}

int
Logger_Client::init_naming_service (ACE_ENV_SINGLE_ARG_DECL)
{
  // Initialize the naming services
  if (my_name_client_.init (orb_.in ()) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       " (%P|%t) Unable to initialize "
                       "the TAO_Naming_Client. \n"),
                      -1);

  // Resolve an instance of the Logger_Factory
  CosNaming::Name factory_name (1);
  factory_name.length (1);
  factory_name[0].id = CORBA::string_dup ("Logger_Factory");

  CORBA::Object_var factory_ref =
    my_name_client_->resolve (factory_name
                              ACE_ENV_ARG_PARAMETER);
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "\nFactory_ref resolved\n"));

  if (CORBA::is_nil (factory_ref.in ()))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "resolved to nil object"),
                      -1);
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "\nLogger_Factory resolved\n"));

  // Narrow the factory and check the success
  factory_ =
    Logger_Factory::_narrow (factory_ref.in ()
                             ACE_ENV_ARG_PARAMETER);

  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "\nFactory narrowed\n"));
  if (CORBA::is_nil (factory_.in ()))
    ACE_ERROR_RETURN ((LM_ERROR,
                       "narrow returned nil"),
                      -1);
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "\nLogger_Factory narrowed\n"));

  // If debugging, get the factory's IOR
  CORBA::String_var str =
    orb_->object_to_string (factory_.in ()
                            ACE_ENV_ARG_PARAMETER);
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "The factory IOR is <%s>\n",
                str.in ()));
  return 0;
}

int
Logger_Client::init_loggers (ACE_ENV_SINGLE_ARG_DECL)
{
  // Retrieve the Logger obj ref corresponding to key1 and
  // key2.
  ACE_TRY
    {
      this->logger_1_ = factory_->make_logger ("key1"
                                               ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      this->logger_2_ = factory_->make_logger ("key2"
                                               ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (this->logger_1_.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "nil logger1"),
                          -1);

      if (CORBA::is_nil (this->logger_2_.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           "nil logger2"),
                          -1);

      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "Created two loggers\n"));

      if (TAO_debug_level > 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "\nTrying to resolve already created logger..."));
          Logger_var logger_3 = factory_->make_logger ("key1"
                                                       ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          if (CORBA::is_nil (logger_3.in ()))
            ACE_DEBUG ((LM_DEBUG,
                        "\nResolution failed."));
          else
            ACE_DEBUG ((LM_DEBUG,
                        "\nResolution succeeded."));
        }
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "init_loggers");
      return -1;
    }
  ACE_ENDTRY;
  return 0;
}


// Execute client example code.

int
Logger_Client::run (void)
{
  ACE_DECLARE_NEW_CORBA_ENV;
  ACE_TRY
    {
      // Create 3 Log_Records for the test
      Logger::Log_Record rec1;
      Logger::Log_Record rec2;
      Logger::Log_Record rec3;
      Logger::Log_Record rec4;
      ;
      // Setup the first log record
      this->init_record (rec1,
                         Logger::LM_DEBUG,
                         "log() test (1) \n");

      // Setup the second log record
      this->init_record (rec2,
                         Logger::LM_MAX,
                         "log() test (2) \n");

      // Setup the third log record
      this->init_record (rec3,
                         Logger::LM_INFO,
                         "logv() test (3) \n");

      // Setup the fourth log record
      this->init_record (rec4,
                         Logger::LM_EMERGENCY,
                         "log_twoway() test (4) \n");

      // If debugging, output the new log records
      if (TAO_debug_level > 0)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "\nFirst Log_Record created. Contents:\n"));
          this->show_record (rec1);

          ACE_DEBUG ((LM_DEBUG,
                      "\nSecond Log_Record created. Contents:\n"));
          this->show_record (rec2);

          ACE_DEBUG ((LM_DEBUG,
                      "\nThird log record created. Contents:\n"));
          this->show_record (rec3);

          ACE_DEBUG ((LM_DEBUG,
                      "\nFourth log record created. Contents:\n"));
          this->show_record (rec4);
        }

      // Change the verbosity.
      this->logger_1_->verbosity (Logger::VERBOSE_LITE ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Log the first Log_Record (VERBOSE_LITE)
      this->logger_1_->log (rec1 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Change the verbosity again.
      this->logger_2_->verbosity (Logger::VERBOSE ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Log the second Log_Record (VERBOSE)
      this->logger_2_->log (rec2 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Change the verbosity again
      this->logger_2_->verbosity (Logger::SILENT ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Log the third log record using logv() (this shows if the
      // verbosity level overrides the logger's verbosity level)
      this->logger_2_->logv (rec3, Logger::VERBOSE ACE_ENV_ARG_PARAMETER);

      // Change the verbosity again (so that regular log msgs can be
      // seen again)
      this->logger_2_->verbosity (Logger::VERBOSE ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      // Log the fourth record using log_twoway()
      this->logger_2_->log_twoway (rec4 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;
    }

  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "run");
    }
  ACE_ENDTRY;
  ACE_CHECK_RETURN (-1);
  return 0;
}

// Parses the command line arguments and returns an error status.

int
Logger_Client::parse_args (void)
{
  ACE_Get_Opt get_opts (argc_, argv_, "d");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'd':  // debug flag
        TAO_debug_level++;
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " [-d]"
                           "\n"
                           "    -d: increase debug level\n",
                           this->argv_ [0]),
                          -1);
      }

  // Indicates successful parsing of command line.
  return 0;
}


void
Logger_Client::init_record (Logger::Log_Record &newrec,
                            Logger::Log_Priority lp,
                            const char *msg)
{
  // Copy the message data into newrec.
  newrec.msg_data = CORBA::string_dup (msg);

  // Assign the log priority.
  newrec.type = lp;

  // Create and assign the timestamp.
  ACE_Time_Value time (ACE_OS::gettimeofday ());
  newrec.time = time.sec ();

  // Get and store the PID of the calling process.
  pid_t pid = ACE_OS::getpid ();
  newrec.app_id = pid;

  // Get and store the IP of the local host .
  char name[MAXHOSTNAMELEN];
  ACE_OS::hostname (name, MAXHOSTNAMELEN);
  hostent *he = ACE_OS::gethostbyname (name);
  newrec.host_addr =
    (ACE_reinterpret_cast (in_addr *,
                           he->h_addr_list[0])->s_addr);
}

void
Logger_Client::show_record (Logger::Log_Record &newrec)
{
  in_addr address;
  address.s_addr = newrec.host_addr;

  ACE_DEBUG ((LM_DEBUG,
              " Log Priority: %d\n"
              "         Time: %d\n"
              "          PID: %ld\n"
              " Host Address: %s\n"
              "      Message: %s\n",
              newrec.type,
              newrec.time,
              newrec.app_id,
              inet_ntoa (address),
              newrec.msg_data.in ()));
}