Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / Logging / Logger_i.cpp
blob765a792642e970ebf336463471168ac18662f746
1 #include "LoggerC.h"
2 #include "Logger_i.h"
3 #include "tao/debug.h"
4 #include "ace/ACE.h"
5 #include "ace/INET_Addr.h"
6 #include "ace/Log_Record.h"
7 #include "ace/os_include/os_netdb.h"
9 Logger_Factory_i::Logger_Factory_i (void)
13 Logger_Factory_i::~Logger_Factory_i (void)
17 Logger_ptr
18 Logger_Factory_i::make_logger (const char *name)
20 Logger_i *result = 0;
21 // If name is already in the map, <find> will assign <result> to the
22 // appropriate value
24 if (hash_map_.find (name, result) != 0)
26 if (TAO_debug_level > 0)
27 ACE_DEBUG ((LM_DEBUG,
28 "\nMaking a new logger"));
30 // This attempts to create a new Logger_i and throws an
31 // exception and returns a null value if it fails
32 ACE_NEW_THROW_EX (result,
33 Logger_i (name),
34 CORBA::NO_MEMORY ());
37 // Enter the new logger into the hash map. Check if the <bind>
38 // fails and if so, throw an UNKNOWN exception. <result> may be
39 // valid, but since it would not be properly bound, its behavior my
40 // be off, so delete it to be safe.
41 if (hash_map_.bind (name, result) == -1)
43 delete result;
44 throw CORBA::UNKNOWN ();
46 else
47 // Logger of name <name> already bound. <result> is set
48 // appropriately by <find>. So do nothing.
49 if (TAO_debug_level > 0)
50 ACE_DEBUG ((LM_DEBUG,
51 "\nLogger name already bound"));
53 // <_this> is an performance hit here, but apparently if the object
54 // is already registered with the POA, it will ignore the second
55 // registration attempt.
56 // @@ Matt, this code doesn't seem right. Can you please check with
57 // Irfan and Carlos about whether this is the right thing to do?
58 if (!result)
59 return Logger::_nil ();
61 return result->_this ();
64 Logger_i::Logger_i (const char *name)
65 : name_ (ACE_OS::strdup (name)),
66 verbosity_level_ (Logger::VERBOSE)
68 // Do nothing
71 Logger_i::~Logger_i (void)
73 ACE_OS::free (this->name_);
76 ACE_Log_Priority
77 Logger_i::priority_conversion (Logger::Log_Priority priority)
79 if (priority == Logger::LM_MAX)
80 return LM_MAX;
81 else
83 int pval = static_cast<int> (priority);
85 return static_cast<ACE_Log_Priority> (1 << pval);
86 // (1 << pval) == 2^pval. <ACE_Log_Priority> are powers of 2.
90 u_long
91 Logger_i::verbosity_conversion (Logger::Verbosity_Level verbosity_level)
93 // This isn't very elegant, but it's because there's no simple
94 // mapping from <Logger::Verbosity_Level>'s to the verbosity flags
95 // specified in <ace/Log_Msg.h>
96 switch (verbosity_level)
98 case Logger::SILENT:
99 return 64;
100 case Logger::VERBOSE_LITE:
101 return 32;
102 default:
103 case Logger::VERBOSE:
104 return 16;
108 void
109 Logger_i::log (const Logger::Log_Record &log_rec)
111 this->logv (log_rec, verbosity_level_);
114 void
115 Logger_i::log_twoway (const Logger::Log_Record &log_rec)
117 this->logv (log_rec, verbosity_level_);
120 void
121 Logger_i::logv_twoway (const Logger::Log_Record &log_rec,
122 Logger::Verbosity_Level verbosity)
124 this->logv (log_rec, verbosity);
127 void
128 Logger_i::logv (const Logger::Log_Record &log_rec,
129 Logger::Verbosity_Level verbosity)
131 // Create an <ACE_Log_Record> to leverage existing logging
132 // code. Since Logger::Log_Priority enum tags don't cleanly map to
133 // ACE_Log_Priority tags, <priority_conversion> is used to coerce
134 // the mapping.
135 ACE_Log_Record rec (this->priority_conversion (log_rec.type),
136 ACE_Time_Value (log_rec.time),
137 log_rec.app_id);
139 // Create a temporary buffer for manipulating the logging message,
140 // adding additional space for formatting characters..
141 ACE_TCHAR msgbuf [ACE_MAXLOGMSGLEN + 4];
143 // Format the message for proper display.
144 ACE_OS::strcpy (msgbuf, ACE_TEXT("::"));
146 // Copy the message data into the temporary buffer
147 ACE_OS::strncat (msgbuf,
148 ACE_TEXT_CHAR_TO_TCHAR(log_rec.msg_data),
149 ACE_MAXLOGMSGLEN);
151 // Set <ACE_Log_Record.msg_data> to the value stored in <msgbuf>.
152 rec.msg_data (msgbuf);
154 CORBA::Long addr = log_rec.host_addr;
156 // The constructor for <ACE_INET_Addr> requires a port number, which
157 // is not relevant in this context, so we give it 0.
158 ACE_INET_Addr addy (static_cast<u_short> (0),
159 static_cast<ACE_UINT32> (addr));
161 // Create a buffer and fill it with the host name of the logger
162 ACE_TCHAR namebuf[MAXHOSTNAMELEN + 1];
164 ACE_OS::strncpy (namebuf, ACE_TEXT_CHAR_TO_TCHAR(addy.get_host_addr ()), MAXHOSTNAMELEN);
166 u_long verb_level = this->verbosity_conversion (verbosity);
168 rec.print (namebuf,
169 verb_level,
170 stderr);
171 // Print out the logging message to stderr with the given level of
172 // verbosity
175 void
176 Logger_i::verbosity (Logger::Verbosity_Level level)
178 this->verbosity_level_ = level;