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 ()
13 Logger_Factory_i::~Logger_Factory_i ()
18 Logger_Factory_i::make_logger (const char *name
)
21 // If name is already in the map, <find> will assign <result> to the
24 if (hash_map_
.find (name
, result
) != 0)
26 if (TAO_debug_level
> 0)
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
,
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)
44 throw CORBA::UNKNOWN ();
47 // Logger of name <name> already bound. <result> is set
48 // appropriately by <find>. So do nothing.
49 if (TAO_debug_level
> 0)
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?
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
)
71 Logger_i::~Logger_i ()
73 ACE_OS::free (this->name_
);
77 Logger_i::priority_conversion (Logger::Log_Priority priority
)
79 if (priority
== Logger::LM_MAX
)
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.
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
)
100 case Logger::VERBOSE_LITE
:
103 case Logger::VERBOSE
:
109 Logger_i::log (const Logger::Log_Record
&log_rec
)
111 this->logv (log_rec
, verbosity_level_
);
115 Logger_i::log_twoway (const Logger::Log_Record
&log_rec
)
117 this->logv (log_rec
, verbosity_level_
);
121 Logger_i::logv_twoway (const Logger::Log_Record
&log_rec
,
122 Logger::Verbosity_Level verbosity
)
124 this->logv (log_rec
, verbosity
);
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
135 ACE_Log_Record
rec (this->priority_conversion (log_rec
.type
),
136 ACE_Time_Value (log_rec
.time
),
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
),
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
);
171 // Print out the logging message to stderr with the given level of
176 Logger_i::verbosity (Logger::Verbosity_Level level
)
178 this->verbosity_level_
= level
;