Commit bbe221bf authored by Simon Morlat's avatar Simon Morlat

fix event logs for asynchronous events.

Improve logging of text messages.
parent 95c320e2
......@@ -481,7 +481,7 @@ bool Agent::isUs(const url_t *url, bool check_aliases) const {
void Agent::logEvent(const shared_ptr<SipEvent> &ev){
if (mLogWriter){
shared_ptr<EventLog> evlog;
if (ev->isTerminated() && (evlog=ev->getEventLog<EventLog>())){
if ((evlog=ev->getEventLog<EventLog>())){
if (evlog->isCompleted()) mLogWriter->write(evlog);
}
}
......@@ -537,7 +537,6 @@ void Agent::sendRequestEvent(shared_ptr<RequestSipEvent> ev) {
if (!ev->isTerminated() && !ev->isSuspended()) {
LOGA("Event not handled");
}
logEvent(ev);
}
void Agent::sendResponseEvent(shared_ptr<ResponseSipEvent> ev) {
......@@ -600,7 +599,6 @@ void Agent::sendResponseEvent(shared_ptr<ResponseSipEvent> ev) {
if (!ev->isTerminated() && !ev->isSuspended()) {
LOGA("Event not handled");
}
logEvent(ev);
}
void Agent::injectRequestEvent(shared_ptr<RequestSipEvent> ev) {
......@@ -624,7 +622,6 @@ void Agent::injectRequestEvent(shared_ptr<RequestSipEvent> ev) {
if (!ev->isTerminated() && !ev->isSuspended()) {
LOGA("Event not handled");
}
logEvent(ev);
LOG_END
}
......@@ -649,7 +646,6 @@ void Agent::injectResponseEvent(shared_ptr<ResponseSipEvent> ev) {
if (!ev->isTerminated() && !ev->isSuspended()) {
LOGA("Event not handled");
}
logEvent(ev);
LOG_END
}
......
......@@ -147,6 +147,7 @@ class Agent: public IncomingAgent, public OutgoingAgent, public std::enable_shar
void sendResponseEvent(std::shared_ptr<ResponseSipEvent> ev);
void incrReplyStat(int status);
bool doOnConfigStateChanged(const ConfigValue &conf, ConfigState state);
void logEvent(const std::shared_ptr<SipEvent> &ev);
protected:
void sendTransactionEvent(const std::shared_ptr<Transaction> &transaction, Transaction::Event event);
int onIncomingMessage(msg_t *msg, sip_t *sip);
......@@ -156,7 +157,6 @@ class Agent: public IncomingAgent, public OutgoingAgent, public std::enable_shar
virtual void reply(const std::shared_ptr<MsgSip> &msg, int status, char const *phrase, tag_type_t tag, tag_value_t value, ...);
void discoverInterfaces();
void startLogWriter();
void logEvent(const std::shared_ptr<SipEvent> &ev);
std::string mServerString;
std::list<Module*> mModules;
std::list<std::string> mAliases;
......
......@@ -93,10 +93,29 @@ SipEvent::~SipEvent() {
//LOGD("Destroy SipEvent %p", this);
}
void SipEvent::flushLog(){
if (mEventLog){
Agent *agent=NULL;
if (mIncomingAgent) agent=mIncomingAgent->getAgent();
else if (mOutgoingAgent) agent=mOutgoingAgent->getAgent();
else LOGA("Event has no incoming nor outgoing agent.");
agent->logEvent(shared_from_this());
mEventLog=NULL;
}
}
void SipEvent::setEventLog(const std::shared_ptr<EventLog> & log){
mEventLog=log;
if (mState==TERMINATED){
flushLog();
}
}
void SipEvent::terminateProcessing() {
LOGD("Terminate SipEvent %p", this);
if (mState == STARTED || mState == SUSPENDED) {
mState = TERMINATED;
flushLog();
} else {
LOGA("Can't terminateProcessing: wrong state %s", stateStr(mState).c_str());
}
......
......@@ -72,7 +72,7 @@ private:
bool mOriginal;
};
class SipEvent {
class SipEvent : public std::enable_shared_from_this<SipEvent>{
friend class Agent;
public:
......@@ -130,10 +130,8 @@ public:
std::shared_ptr<_eventLogT> getEventLog(){
return std::dynamic_pointer_cast<_eventLogT>(mEventLog);
}
void setEventLog(const std::shared_ptr<EventLog> & log){
mEventLog=log;
}
void setEventLog(const std::shared_ptr<EventLog> & log);
void flushLog();/*to be used exceptionally when an eventlog needs to be flushed immediately, for example because you need to submit a new one.*/
protected:
Module *mCurrModule;
std::shared_ptr<MsgSip> mMsgSip;
......
......@@ -219,7 +219,12 @@ int FilesystemEventLogWriter::openPath(const url_t *uri, const char *kind, time_
const char *domain=uri->url_host;
path<<mRootPath<<"/"<<domain;
path<<mRootPath<<"/users";
if (!createDirectoryIfNotExist(path.str().c_str()))
return -1;
path<<"/"<<domain;
if (!createDirectoryIfNotExist(path.str().c_str()))
return -1;
......@@ -266,8 +271,8 @@ void FilesystemEventLogWriter::writeRegistrationLog(const std::shared_ptr<Regist
if (fd==-1) return;
ostringstream msg;
msg<<PrettyTime(rlog->mDate)<<": "<<rlog->mType<<" "<<rlog->mFrom<<" with contact "<<rlog->mContacts->m_url;
msg<<" ("<<rlog->mUA<<")"<<endl;
msg<<PrettyTime(rlog->mDate)<<": "<<rlog->mType<<" "<<rlog->mFrom<<" ("<<rlog->mContacts->m_url<<") ";
msg<<rlog->mUA<<endl;
if (::write(fd,msg.str().c_str(),msg.str().size())==-1){
LOGE("Fail to write registration log: %s",strerror(errno));
......@@ -328,6 +333,7 @@ void FilesystemEventLogWriter::writeAuthLog(const std::shared_ptr<AuthLog> &alog
ostringstream msg;
msg<<PrettyTime(alog->mDate)<<" "<<alog->mMethod<<" "<<alog->mFrom;
if (alog->mOrigin) msg<<" ("<<alog->mOrigin<<") ";
if (alog->mUA) msg<<" ("<<alog->mUA<<") ";
msg<<" --> "<<alog->mTo<<" ";
msg<<alog->mStatusCode<<" "<<alog->mReason<<endl;
......
......@@ -45,7 +45,7 @@ void ForkMessageContext::markAsDelivered(const shared_ptr<SipEvent> &ev){
}
}
void ForkMessageContext::forward(const shared_ptr<SipEvent> &ev) {
void ForkMessageContext::forward(const shared_ptr<ResponseSipEvent> &ev) {
sip_t *sip = ev->getMsgSip()->getSip();
if (sip->sip_status->st_status >= 200 ) {
......@@ -54,6 +54,8 @@ void ForkMessageContext::forward(const shared_ptr<SipEvent> &ev) {
if (mDeliveredCount>1){
/*should only transfer one response*/
ev->setIncomingAgent(shared_ptr<IncomingAgent>());
}else if (mIncoming){
logReceptionEvent(ev);
}
checkFinished();
}
......@@ -103,19 +105,19 @@ void ForkMessageContext::onResponse(const shared_ptr<OutgoingTransaction> &trans
sip_t *sip = ms->getSip();
if (sip != NULL && sip->sip_status != NULL) {
LOGD("Fork: outgoingCallback %d", sip->sip_status->st_status);
if (sip->sip_status->st_status > 100 && sip->sip_status->st_status < 300) {
forward(event);
} else {
store(event);
}
sip_t *sip=event->getMsgSip()->getSip();
auto log=make_shared<MessageLog>(MessageLog::Delivery,sip->sip_from,sip->sip_to,sip->sip_call_id->i_hash);
log->setDestination(transaction->getRequestUri());
log->setStatusCode(sip->sip_status->st_status,sip->sip_status->st_phrase);
log->setCompleted();
event->setEventLog(log);
if (sip->sip_status->st_status > 100 && sip->sip_status->st_status < 300) {
forward(event);
return;
} else {
store(event);
return;
}
return;
}
LOGW("ForkMessageContext : ignore message");
}
......@@ -126,6 +128,7 @@ void ForkMessageContext::logReceptionEvent(const shared_ptr<ResponseSipEvent> &e
log->setStatusCode(sip->sip_status->st_status,sip->sip_status->st_phrase);
log->setCompleted();
ev->setEventLog(log);
ev->flushLog();
}
void ForkMessageContext::finishIncomingTransaction(){
......
......@@ -32,7 +32,7 @@ private:
int mDeliveredCount;
std::list<int> mForwardResponses;
std::map<std::string,bool> mDeliveryMap;//map of pairs (unique-id,delivered)
void forward(const std::shared_ptr<SipEvent> &ev);
void forward(const std::shared_ptr<ResponseSipEvent> &ev);
void store(std::shared_ptr<ResponseSipEvent> &event);
void markAsDelivered(const std::shared_ptr<SipEvent> &ev);
su_timer_t *mAcceptanceTimer; /*timeout after which an answer must be sent through the incoming transaction even if no success response was received on the outgoing transactions*/
......
......@@ -460,6 +460,7 @@ bool Authentication::AuthenticationListener::sendReply(){
mPasswordFound);
log->setStatusCode(mAs->as_status,mAs->as_phrase);
log->setOrigin(sip->sip_via);
if (sip->sip_user_agent) log->setUserAgent(sip->sip_user_agent);
log->setCompleted();
mEv->setEventLog(log);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment