summaryrefslogtreecommitdiff
path: root/modules/CIAO/ciao/Logger/Logger_Service.cpp
blob: d5c1c9c5fbfbb38ee021315c0ef8f1994d54f3ac (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
// $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"

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

  int
  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"), this->log_level_);
    this->log_level_ = log;

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

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

    this->parse_args (argc, argv);
    this->set_levels ();

    return 0;
  }

  void
  Logger_Service::parse_args (int argc, ACE_TCHAR **argv)
  {
    const ACE_TCHAR *shortl = ACE_TEXT("-l");
    const ACE_TCHAR *longl = ACE_TEXT("--log-level");
    const ACE_TCHAR *tracel = ACE_TEXT("--trace");
    const ACE_TCHAR *traces = ACE_TEXT("-t");
    const ACE_TCHAR *lfl = ACE_TEXT("--log-file");
    const ACE_TCHAR *lfs = ACE_TEXT("-f");

    // We need to actually FIND the -l option, as the get_opt won't ignore
    // the ORB options and such.
    for (int i = 0; i < argc; ++i)
      {
        if (ACE_OS::strncmp (argv[i], traces, 2) == 0 ||
            ACE_OS::strncmp (argv[i], tracel, 7) == 0)
          {
            this->trace_ = true;
            continue;
          }

        if (ACE_OS::strncmp (argv[i], shortl, 2) == 0 ||
            ACE_OS::strncmp (argv[i], longl,11 ) == 0)
          {
            if ((i + 1) < argc && *argv[i + 1] != '-')
              {
                int level = ACE_OS::atoi (argv[i + 1]);

                if (level != 0)
                  this->log_level_ = level;
              }
          }

        if (ACE_OS::strncmp (argv[i], lfs, 2) == 0 ||
            ACE_OS::strncmp (argv[i], lfl, 10 ) == 0)
          {
            if ((i + 1) < argc && *argv[i + 1] != '-')
              {
                this->filename_ = argv[i+1];
              }
          }
      }
  }

  void
  Logger_Service::set_levels (void)
  {
    if (this->trace_)
      {
        CIAO_ENABLE_TRACE ();
        this->log_level_ = 10;
      }
    else
      {
        CIAO_DISABLE_TRACE ();
      }

    u_long new_mask = 0;

    if (this->log_level_ >= 9)
      {
        new_mask |= LM_TRACE;
      }
    if (this->log_level_ >= 8)
      {
        new_mask |= LM_DEBUG;
      }
    if (this->log_level_ >= 7)
      {
        new_mask |= LM_INFO;
      }
    if (this->log_level_ >= 6)
      {
        new_mask |= LM_NOTICE;
      }
    if (this->log_level_ >= 5)
      {
        new_mask |= LM_WARNING;
      }
    if (this->log_level_ >= 4)
      {
        new_mask |= LM_ERROR;
      }
    if (this->log_level_ >= 3)
      {
        new_mask |= LM_CRITICAL;
      }
    if (this->log_level_ >= 2)
      {
        new_mask |= LM_ALERT;
      }
    if (this->log_level_ >= 1)
      {
        new_mask |= LM_EMERGENCY;
      }
    ACE_Log_Msg::instance()->priority_mask(new_mask, ACE_Log_Msg::PROCESS);
    CIAO_DEBUG ( (LM_TRACE, CLINFO "Logging level is set to %i\n", this->log_level_));
  }

  ACE_Log_Msg_Backend *
  Logger_Service::get_logger_backend (CORBA::ORB_ptr)
  {
    File_Logger_Backend * the_backend;
    ACE_NEW_THROW_EX (the_backend,
                      File_Logger_Backend (this->filename_.c_str()),
                      CORBA::NO_MEMORY());
    return the_backend;
  }

} // CIAO

using namespace CIAO;
ACE_FACTORY_DEFINE (CIAO_Logger, Logger_Service)