00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <iostream>
00018 #include <sstream>
00019
00020 #include "Reactor.h"
00021 #include "Socket.h"
00022 #include "RemoteLogger.h"
00023
00024 using namespace ASSA;
00025
00026
00027
00028
00029 RemoteLogger::
00030 RemoteLogger () :
00031 m_state (closed),
00032 m_recursive_call (false)
00033 {
00034
00035 }
00036
00037 int
00038 RemoteLogger::
00039 open ()
00040 {
00041 return 0;
00042 }
00043
00044 int
00045 RemoteLogger::
00046 log_open (const char* appname_,
00047 const char* logfname_,
00048 u_long groups_,
00049 u_long maxsize_,
00050 Reactor* reactor_)
00051 {
00052 if (m_recursive_call) {
00053 return 0;
00054 }
00055 m_recursive_call = true;
00056
00057 if (m_state == opened) {
00058 return 0;
00059 }
00060 m_logfname = logfname_;
00061 m_groups = groups_;
00062 m_reactor = reactor_;
00063
00064 m_reactor->registerIOHandler (this, get_stream ().getHandler(),
00065 ASSA::READ_EVENT);
00066
00070 get_stream ().turnOptionOff (Socket::nonblocking);
00071
00074 size_t len = sizeof (maxsize_) +
00075 Socket::xdr_length (appname_) +
00076 Socket::xdr_length (logfname_);
00077
00080 get_stream () << 1234567890 << SIGN_ON << len
00081 << maxsize_ << appname_ << logfname_ << ASSA::flush;
00082 m_state = opened;
00083 m_recursive_call = false;
00084 return 0;
00085 }
00086
00087 int
00088 RemoteLogger::
00089 log_close (void)
00090 {
00094 if (m_state == opened) {
00095 m_recursive_call = true;
00096 get_stream () << 1234567890 << SIGN_OFF << 0 << ASSA::flush;
00097 m_reactor->removeHandler (this, READ_EVENT);
00098 m_recursive_call = false;
00099 }
00100 return 0;
00101 }
00102
00103 int
00104 RemoteLogger::
00105 handle_close (int fd_)
00106 {
00107 m_state = closed;
00108 m_logfname.empty ();
00109 return 0;
00110 }
00111
00112 void
00113 RemoteLogger::
00114 log_resync (void)
00115 {
00116 if (m_state == opened) {
00117 m_recursive_call = true;
00118 get_stream () << ASSA::flush;
00119 m_recursive_call = false;
00120 }
00121 }
00122
00123 int
00124 RemoteLogger::
00125 log_msg (Group groups_,
00126 size_t indent_level_,
00127 const string& func_name_,
00128 size_t expected_sz_,
00129 const char* fmt_,
00130 va_list msg_list_)
00131 {
00132 if (m_recursive_call) {
00133 return 0;
00134 }
00135 if (m_state == closed) {
00136 return -1;
00137 }
00138 if (!group_enabled (groups_)) {
00139 return 0;
00140 }
00141
00142 std::ostringstream os;
00143 add_timestamp (os);
00144 indent_func_name (os, func_name_, indent_level_, FUNC_MSG);
00145
00146 bool release = false;
00147 char* msgbuf_ptr = format_msg (expected_sz_, fmt_, msg_list_, release);
00148 if (msgbuf_ptr == NULL) {
00149 return -1;
00150 }
00151
00152 os << msgbuf_ptr;
00153
00154 if (release) {
00155 delete [] msgbuf_ptr;
00156 }
00157
00160 if (get_stream ()) {
00161 m_recursive_call = true;
00162 Assure_exit (os.str ().length () != 0);
00163 get_stream () << 1234567890 << LOG_MSG << Socket::xdr_length (os.str ())
00164 << os.str () << ASSA::flush;
00165 m_recursive_call = false;
00166 }
00167 else {
00168 m_state = closed;
00169 }
00170 return 0;
00171 }
00172
00173 int
00174 RemoteLogger::
00175 log_func (Group groups_,
00176 size_t indent_level_,
00177 const string& func_name_,
00178 marker_t type_)
00179 {
00180 if (m_recursive_call) {
00181 return 0;
00182 }
00183 if (m_state == closed) {
00184 return -1;
00185 }
00186 if (! group_enabled (groups_)) {
00187 return 0;
00188 }
00189
00190 std::ostringstream os;
00191 add_timestamp (os);
00192 indent_func_name (os, func_name_, indent_level_, type_);
00193 os << ((type_ == FUNC_ENTRY) ? "---v---\n" : "---^---\n");
00194
00197 if (get_stream ().good ()) {
00198 m_recursive_call = true;
00199 get_stream () << 1234567890 << LOG_MSG << Socket::xdr_length (os.str ())
00200 << os.str () << ASSA::flush;
00201 m_recursive_call = false;
00202 }
00203 else {
00204 m_state = closed;
00205 }
00206
00207 return 0;
00208 }
00209
00210