#include <Logger.h>
Public Member Functions | |
Logger () | |
~Logger () | |
void | set_app_name (const std::string &appname_) |
Set application name. | |
void | enable_group (Group g_) |
Enable logging for group g_. | |
void | disable_group (Group g_) |
Disable logging for group g_. | |
void | enable_groups (u_long groups_) |
Enable logging for groups_. | |
void | disable_groups (u_long groups_) |
Disable logging for groups_. | |
void | enable_all_groups (void) |
void | disable_all_groups (void) |
bool | group_enabled (Group g_) const |
void | enable_timestamp (void) |
Add optional timezone: GMT vs. Local. | |
void | disable_timestamp (void) |
bool | timestamp_enabled (void) const |
void | set_timezone (int zone) |
0 - GMT, 1 - LOCAL | |
void | sign_on (const string &func_name_) |
void | sign_off (void) |
int | log_open (u_long groups_) |
Write log messages to standard output. | |
int | log_open (const char *logfname_, u_long groups_, u_long maxsize_) |
Write log messages to the logfile. | |
int | log_open (const std::string &logsvr_, const char *logfname_, u_long groups_, u_long maxsize_, Reactor *reactor_) |
Write log messages to the log server assa-logd. | |
void | log_resync (void) |
int | log_close (void) |
int | log_msg (u_long g_, const char *fmt_,...) |
Here is an interesting twist introduced by remote logging server:. | |
int | log_func (u_long g_, marker_t type_) |
Private Attributes | |
Logger_Impl * | m_impl |
stack< string > | m_context |
Logger implementation. | |
std::string | m_app_name |
Stack of all contexts. |
Definition at line 45 of file Logger.h.
ASSA::Logger::Logger | ( | ) | [inline] |
Definition at line 48 of file Logger.h.
00048 : m_impl (NULL), m_app_name ("zombie") { /* no-op */ }
ASSA::Logger::~Logger | ( | ) | [inline] |
void ASSA::Logger::set_app_name | ( | const std::string & | appname_ | ) | [inline] |
Set application name.
This should be the first call made to the newly created Logger object.
Definition at line 55 of file Logger.h.
References m_app_name.
00055 { m_app_name = appname_; }
void ASSA::Logger::enable_group | ( | Group | g_ | ) | [inline] |
void ASSA::Logger::disable_group | ( | Group | g_ | ) | [inline] |
void ASSA::Logger::enable_groups | ( | u_long | groups_ | ) | [inline] |
void ASSA::Logger::disable_groups | ( | u_long | groups_ | ) | [inline] |
void ASSA::Logger::enable_all_groups | ( | void | ) | [inline] |
void ASSA::Logger::disable_all_groups | ( | void | ) | [inline] |
bool ASSA::Logger::group_enabled | ( | Group | g_ | ) | const [inline] |
void ASSA::Logger::enable_timestamp | ( | void | ) | [inline] |
void ASSA::Logger::disable_timestamp | ( | void | ) | [inline] |
bool ASSA::Logger::timestamp_enabled | ( | void | ) | const [inline] |
void ASSA::Logger::set_timezone | ( | int | zone | ) | [inline] |
void ASSA::Logger::sign_on | ( | const string & | func_name_ | ) | [inline] |
void ASSA::Logger::sign_off | ( | void | ) | [inline] |
int Logger::log_open | ( | u_long | groups_ | ) |
Write log messages to standard output.
Definition at line 53 of file Logger.cpp.
References ASSA::endl(), ASSA::Logger_Impl::log_open(), and m_impl.
00054 { 00055 if (m_impl != NULL) { 00056 std::cerr << "Logger::log_open - Implementation already exist" 00057 << std::endl; 00058 return -1; 00059 } 00060 m_impl = new StdOutLogger; 00061 return m_impl->log_open (groups_); 00062 }
Write log messages to the logfile.
logfname_ | Name of the logfile | |
groups_ | Logging masks enabled | |
maxsize_ | Maximum size of the logfile |
Definition at line 66 of file Logger.cpp.
References ASSA::Logger_Impl::log_open(), and m_impl.
00067 { 00068 if (m_impl != NULL) { 00069 return -1; 00070 } 00071 m_impl = new FileLogger; 00072 return m_impl->log_open (logfname_, groups_, maxsize_); 00073 }
int Logger::log_open | ( | const std::string & | logsvr_, | |
const char * | logfname_, | |||
u_long | groups_, | |||
u_long | maxsize_, | |||
Reactor * | reactor_ | |||
) |
Write log messages to the log server assa-logd.
logsvr_ | Address of assa-logd (assalogd@hostname) | |
logfname_ | Name of the logfile | |
groups_ | Logging masks enabled | |
maxsize_ | Maximum size of the logfile | |
reactor_ | Pointer to the Reactor to use for communication. |
Definition at line 77 of file Logger.cpp.
References ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::connect(), ASSA::AutoPtr< X >::get(), ASSA::Logger_Impl::log_open(), m_app_name, m_impl, ASSA::Connector< SERVICE_HANDLER, PEER_CONNECTOR >::open(), and ASSA::AutoPtr< X >::release().
00082 { 00083 { 00084 TimeVal tv (10.0); 00085 INETAddress addr (logsvraddr_.c_str ()); 00086 if (addr.bad ()) { 00087 return -1; 00088 } 00089 00090 Connector <RemoteLogger, IPv4Socket> log_connector; 00091 AutoPtr<RemoteLogger> lsp (new RemoteLogger); 00092 log_connector.open (tv); 00093 00094 if (log_connector.connect (lsp.get (), addr) < 0) { 00095 delete m_impl; 00096 m_impl = NULL; 00097 return -1; 00098 } 00099 00100 m_impl = lsp.release (); 00101 } 00102 int ret = m_impl->log_open (m_app_name.c_str (), logfname_, 00103 groups_, maxsize_, reactor_); 00104 return ret; 00105 }
void ASSA::Logger::log_resync | ( | void | ) | [inline] |
int Logger::log_close | ( | void | ) |
Definition at line 39 of file Logger.cpp.
References ASSA::Logger_Impl::log_close(), and m_impl.
Referenced by ~Logger().
00040 { 00041 int ret = 0; 00042 00043 if (m_impl) { 00044 ret = m_impl->log_close (); 00045 delete m_impl; 00046 m_impl = 0; 00047 } 00048 return ret; 00049 }
int Logger::log_msg | ( | u_long | g_, | |
const char * | fmt_, | |||
... | ||||
) |
Here is an interesting twist introduced by remote logging server:.
Setting errno bits puts us in an unexpected situation. Asynchronous connection establishment reliest on examening errno to see if it was set by the system call, connect(), to EINPROGRESS. If it was, and thus connect() failed, IPv4Socket::connect() would log it as an error with EL() macro. When client is configured to log its messages to the server, call to EL() would result into the call to log_msg(), and because connection to the server has not yet been established, m_impl is set to 0 and errno is reset to EPERM. When stack unwinds itself, Connector::connect() fails because Connector::connectServiceHandler() returned errno different from expected EINPROGRESS (it is EPERM)! From vprintf(3S) manpage: int vsnprintf (char* buf, size_t cnt, const char* fmt, va_list argptr);
"The vsnprintf() function returns the number of characters formatted, that is, then number of characters that would have been written to the buffer if it were large enough. It returns a negative value if an output error was encountered."
However, mingw relies on WIN32 implementation of the function which is inherently broken (not C99 compliant). From MSD reference:
"vsnprint returns the number of characters written if the the number of characters to write is less than or equal to 'cnt'; if the number of characters to write is greater than 'cnt', RETURN -1 indicating that output has been truncated. The return value does not include the terminating null, if one is written.
Estimate message size
Fromat and write message to the log
For extra debuggin
if (ret < 0) { va_start (ap, fmt_); vsnprintf (tmpbuf, TMPBUF_SZ -1, fmt_, ap); std::cout << "m_impl->log_msg()=-1 message:\n" << tmpbuf << std::flush; va_end (ap); }
Definition at line 144 of file Logger.cpp.
References ASSA::Logger_Impl::log_msg(), m_context, m_impl, and TMPBUF_SZ.
00145 { 00146 va_list ap; 00147 va_list ap2; 00148 string empty_str; 00149 size_t expected_sz = 0; 00150 00151 static char tmpbuf[TMPBUF_SZ]; 00152 char* bufp = tmpbuf; 00153 int len = TMPBUF_SZ; 00154 int ret; 00155 00156 if (m_impl == NULL) { 00157 return -1; 00158 } 00159 00162 #if defined(WIN32) 00163 00164 va_copy (ap2, ap); 00165 ret = vsnprintf (bufp, len-1, fmt_, ap2); 00166 va_end (ap2); 00167 00168 if (ret == -1 || ret >= len) 00169 { 00170 len *= 2; 00171 bufp = new char [len]; 00172 while (1) { 00173 va_copy (ap2, ap); 00174 ret = vsnprintf (bufp, len-1, fmt_, ap2); 00175 va_end (ap2); 00176 if (ret > -1 && ret < len) { 00177 delete [] bufp; 00178 break; 00179 } 00180 len *= 2; 00181 delete [] bufp; 00182 bufp = new char [len]; 00183 // std::cout << "Logger::log_func : malloc(" << len << ")\n" 00184 // << std::flush; 00185 } 00186 } 00187 00188 #else /* POSIX, C99 compliant */ 00189 00190 char c; 00191 va_start (ap, fmt_); 00192 ret = ::vsnprintf (&c, 1, fmt_, ap); 00193 va_end (ap); 00194 00195 #endif 00196 00197 expected_sz = ret + 1; 00198 00199 // std::cout << "Logger::log_func : expected_sz = " 00200 // << expected_sz << "\n" << std::flush; 00201 00204 va_start (ap, fmt_); 00205 ret = m_impl->log_msg (static_cast<Group> (g_), 00206 m_context.size (), 00207 m_context.size () ? m_context.top () : empty_str, 00208 expected_sz, 00209 fmt_, 00210 ap); 00211 va_end (ap); 00212 00225 return ret; 00226 }
Definition at line 230 of file Logger.cpp.
References ASSA::Logger_Impl::log_func(), m_context, and m_impl.
00231 { 00232 std::string empty_str; 00233 00234 if (m_impl == NULL) { 00235 return -1; 00236 } 00237 00238 return m_impl->log_func (static_cast<Group> (g_), 00239 m_context.size (), 00240 m_context.size () ? m_context.top () : empty_str, 00241 type_); 00242 }
Logger_Impl* ASSA::Logger::m_impl [private] |
Definition at line 128 of file Logger.h.
Referenced by disable_all_groups(), disable_group(), disable_groups(), disable_timestamp(), enable_all_groups(), enable_group(), enable_groups(), enable_timestamp(), group_enabled(), log_close(), log_func(), log_msg(), log_open(), log_resync(), set_timezone(), and timestamp_enabled().
stack<string> ASSA::Logger::m_context [private] |
Logger implementation.
Definition at line 129 of file Logger.h.
Referenced by log_func(), log_msg(), sign_off(), and sign_on().
std::string ASSA::Logger::m_app_name [private] |
Stack of all contexts.
Definition at line 130 of file Logger.h.
Referenced by log_open(), and set_app_name().