summaryrefslogtreecommitdiff
path: root/modules/CIAO/ciao/Logger/Logger_Service.cpp
blob: 83d5e45fdd550e47006be78641a649650c9720ab (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
// $Id$
#include "Logger_Service.h"
#include "Log_Macros.h"
#include "ace/Get_Opt.h"
#include "ace/CORBA_macros.h"
#include "ace/Env_Value_T.h"
#include "tao/SystemException.h"
#include "ace/Service_Config.h"
#include "ace/Arg_Shifter.h"

#if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
// Needed to set ACE_LOG_MSG::msg_ostream()
// FUZZ: disable check_for_streams_include
#  include "ace/streams.h"
#endif /* !ACE_LACKS_IOSTREAM_TOTALLY */

CIAO::Logger_Service::Logger_Service (void)
  : filename_ (ACE_TEXT("")),
    trace_ (false)
{
}

int
CIAO::Logger_Service::init (int argc, ACE_TCHAR * argv[])
{
  // Get prospective values from the environment first, those given on
  // command line can override
  ACE_Env_Value<int> log (ACE_TEXT("CIAO_LOG_LEVEL"), CIAO_debug_level);
  CIAO_debug_level = log;

  ACE_Env_Value<int> trace (ACE_TEXT("CIAO_TRACE_ENABLE"), 0);
  this->trace_ = (trace != 0);

  ACE_Env_Value<const ACE_TCHAR *> filename (ACE_TEXT("CIAO_LOG_FILE"), this->filename_.c_str ());
  this->filename_ = filename;

  this->parse_args (argc, argv);

  if (this->trace_)
    {
      CIAO_ENABLE_TRACE ();
    }
  else
    {
      CIAO_DISABLE_TRACE ();
    }

  if (this->filename_.length () > 0)
    {
#if defined (ACE_LACKS_IOSTREAM_TOTALLY)

      FILE* output_stream = ACE_OS::fopen (this->filename_.c_str (), ACE_TEXT ("a"));

      ACE_LOG_MSG->msg_ostream (output_stream, 1);
#else /* ! ACE_LACKS_IOSTREAM_TOTALLY */
      ofstream* output_stream = 0;

      ACE_NEW_THROW_EX (output_stream,
                        ofstream (),
                        CORBA::NO_MEMORY (
                          CORBA::SystemException::_tao_minor_code (
                            0,
                            ENOMEM),
                          CORBA::COMPLETED_NO));

      output_stream->open (ACE_TEXT_ALWAYS_CHAR (this->filename_.c_str ()),
                           ios::out | ios::app);

      if (!output_stream->bad ())
        {
          ACE_LOG_MSG->msg_ostream (output_stream, 1);
        }
#endif /* ACE_LACKS_IOSTREAM_TOTALLY */

      ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER);
      ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
    }

  return 0;
}

void
CIAO::Logger_Service::parse_args (int argc, ACE_TCHAR **argv)
{
  CIAO_TRACE ("CIAO::Logger_Service::parse_args");

  ACE_Arg_Shifter arg_shifter (argc, argv);

  while (arg_shifter.is_anything_left ())
    {
      const ACE_TCHAR *current_arg = 0;
      if (0 != (current_arg =
                     arg_shifter.get_the_parameter
                     (ACE_TEXT ("-CIAOLogLevel"))))
        {
          CIAO_debug_level = ACE_OS::atoi (current_arg);

          arg_shifter.consume_arg ();
        }
      else if (0 == arg_shifter.cur_arg_strncasecmp
          (ACE_TEXT ("-CIAOTraceEnable")))
        {
          this->trace_ = true;

          arg_shifter.consume_arg ();
        }
      else if (0 != (current_arg =
                     arg_shifter.get_the_parameter
                     (ACE_TEXT ("-CIAOLogFile"))))
        {
          this->filename_ = current_arg;

          arg_shifter.consume_arg ();
        }
      else
        {
          // Can't interpret this argument.  Move on to the next
          // argument.  Any arguments that don't match are ignored
          // so that the caller can still use them.
          arg_shifter.ignore_arg ();
        }
    }
}

int
CIAO::Logger_Service::Initializer (void)
{
  return ACE_Service_Config::process_directive (ace_svc_desc_CIAO_LOGGER_SERVICE);
}

ACE_STATIC_SVC_DEFINE (CIAO_LOGGER_SERVICE,
                       ACE_TEXT ("CIAO_Logger"),
                       ACE_SVC_OBJ_T,
                       &ACE_SVC_NAME (CIAO_LOGGER_SERVICE),
                       ACE_Service_Type::DELETE_THIS | ACE_Service_Type::DELETE_OBJ,
                       0)
ACE_FACTORY_DEFINE (CIAO_Logger, CIAO_LOGGER_SERVICE)