Commit c20d49c3 authored by Pavel Vainerman's avatar Pavel Vainerman

(LogAgregator): перевёл на использование shared_ptr.

parent 115f617f
...@@ -36,7 +36,7 @@ int main( int argc, char **argv ) ...@@ -36,7 +36,7 @@ int main( int argc, char **argv )
int verb = 0; int verb = 0;
string addr("localhost"); string addr("localhost");
int port = 3333; int port = 3333;
int tout = 2000; //int tout = 2000;
timeout_t delay = 5000; timeout_t delay = 5000;
try try
...@@ -82,10 +82,10 @@ int main( int argc, char **argv ) ...@@ -82,10 +82,10 @@ int main( int argc, char **argv )
} }
DebugStream dlog; auto dlog = make_shared<DebugStream>();
dlog.setLogName("dlog"); dlog->setLogName("dlog");
DebugStream dlog2; auto dlog2 = make_shared<DebugStream>();
dlog2.setLogName("dlog2"); dlog2->setLogName("dlog2");
LogAgregator la; LogAgregator la;
la.add(dlog); la.add(dlog);
...@@ -93,22 +93,22 @@ int main( int argc, char **argv ) ...@@ -93,22 +93,22 @@ int main( int argc, char **argv )
LogServer ls(la); LogServer ls(la);
// LogServer ls(cout); // LogServer ls(cout);
dlog.addLevel(Debug::ANY); dlog->addLevel(Debug::ANY);
dlog2.addLevel(Debug::ANY); dlog2->addLevel(Debug::ANY);
ls.run( addr, port, true ); ls.run( addr, port, true );
unsigned int i=0; unsigned int i=0;
while( true ) while( true )
{ {
dlog << "[" << ++i << "] Test message for log" << endl; dlog->any() << "[" << ++i << "] Test message for log" << endl;
dlog.info() << ": dlog : INFO message" << endl; dlog->info() << ": dlog : INFO message" << endl;
dlog.warn() << ": dlog : WARN message" << endl; dlog->warn() << ": dlog : WARN message" << endl;
dlog.crit() << ": dlog : CRIT message" << endl; dlog->crit() << ": dlog : CRIT message" << endl;
dlog2.info() << ": dlog2: INFO message" << endl; dlog2->info() << ": dlog2: INFO message" << endl;
dlog2.warn() << ": dlog2: WARN message" << endl; dlog2->warn() << ": dlog2: WARN message" << endl;
dlog2.crit() << ": dlog2: CRIT message" << endl; dlog2->crit() << ": dlog2: CRIT message" << endl;
msleep(delay); msleep(delay);
} }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
Name: libuniset2 Name: libuniset2
Version: 2.0 Version: 2.0
Release: alt11 Release: alt12
Summary: UniSet - library for building distributed industrial control systems Summary: UniSet - library for building distributed industrial control systems
...@@ -406,6 +406,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname ...@@ -406,6 +406,9 @@ mv -f %buildroot%python_sitelibdir_noarch/* %buildroot%python_sitelibdir/%oname
%exclude %_pkgconfigdir/libUniSet2.pc %exclude %_pkgconfigdir/libUniSet2.pc
%changelog %changelog
* Fri Jan 23 2015 Pavel Vainerman <pv@altlinux.ru> 2.0-alt12
- refactoring LogAgregator,LogServer,LogSesson --> use shared_ptr
* Sat Jan 17 2015 Pavel Vainerman <pv@altlinux.ru> 2.0-alt11 * Sat Jan 17 2015 Pavel Vainerman <pv@altlinux.ru> 2.0-alt11
- refactoring "exit process" - refactoring "exit process"
- fixed bug in specfile: --enable-doc --> --enable-docs - fixed bug in specfile: --enable-doc --> --enable-docs
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
#define LogAgregator_H_ #define LogAgregator_H_
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#include <string> #include <string>
#include <list> #include <memory>
#include <map>
#include "DebugStream.h" #include "DebugStream.h"
#include "LogServerTypes.h" #include "LogServerTypes.h"
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
...@@ -18,8 +19,14 @@ class LogAgregator: ...@@ -18,8 +19,14 @@ class LogAgregator:
virtual void logFile( const std::string& f ); virtual void logFile( const std::string& f );
// функция не рекомендуется к использованию и сделана для
// совместимости со старым кодом или для глобальных DebugStream
// Рекомендуется, всё-таки использовать add( std::shared_ptr<DebugStream>.. );
void add( DebugStream& log ); void add( DebugStream& log );
void add( std::shared_ptr<DebugStream>& log );
std::shared_ptr<DebugStream> create( const std::string& logname );
// Управление "подчинёнными" логами // Управление "подчинёнными" логами
void addLevel( const std::string& logname, Debug::type t ); void addLevel( const std::string& logname, Debug::type t );
void delLevel( const std::string& logname, Debug::type t ); void delLevel( const std::string& logname, Debug::type t );
...@@ -29,12 +36,12 @@ class LogAgregator: ...@@ -29,12 +36,12 @@ class LogAgregator:
struct LogInfo struct LogInfo
{ {
LogInfo():log(0),logfile(""){} LogInfo():log(0),logfile(""){}
LogInfo( DebugStream* l ):log(l),logfile(l->getLogFile()){} LogInfo( std::shared_ptr<DebugStream>& l ):log(l),logfile(l->getLogFile()){}
DebugStream* log; std::shared_ptr<DebugStream> log;
std::string logfile; std::string logfile;
}; };
DebugStream* getLog( const std::string& logname ); std::shared_ptr<DebugStream> getLog( const std::string& logname );
LogInfo getLogInfo( const std::string& logname ); LogInfo getLogInfo( const std::string& logname );
protected: protected:
...@@ -42,8 +49,8 @@ class LogAgregator: ...@@ -42,8 +49,8 @@ class LogAgregator:
private: private:
typedef std::list<LogInfo> LogList; typedef std::map<std::string, LogInfo> LogMap;
LogList llst; LogMap lmap;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#endif // LogAgregator_H_ #endif // LogAgregator_H_
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#include <list> #include <list>
#include <string> #include <string>
#include <memory>
#include <cc++/socket.h> #include <cc++/socket.h>
#include "Mutex.h" #include "Mutex.h"
#include "DebugStream.h" #include "DebugStream.h"
...@@ -27,11 +28,11 @@ LogReader. Читающих клиентов может быть скольуг ...@@ -27,11 +28,11 @@ LogReader. Читающих клиентов может быть скольуг
При этом если необходимо управлять или читать сразу несколько логов можно воспользоваться специальным классом LogAgregator. При этом если необходимо управлять или читать сразу несколько логов можно воспользоваться специальным классом LogAgregator.
\code \code
DebugStream log1; auto log1 = make_shared<DebugStream>();
log1.setLogName("log1"); log1->setLogName("log1");
DebugStream log2; auto log2 = make_shared<DebugStream>();
log2.setLogName("log2"); log2->setLogName("log2");
LogAgregator la; LogAgregator la;
la.add(log1); la.add(log1);
...@@ -48,7 +49,7 @@ class LogServer ...@@ -48,7 +49,7 @@ class LogServer
{ {
public: public:
LogServer( DebugStream& log ); LogServer( std::shared_ptr<DebugStream>& log );
LogServer( std::ostream& os ); LogServer( std::ostream& os );
~LogServer(); ~LogServer();
...@@ -62,10 +63,10 @@ class LogServer ...@@ -62,10 +63,10 @@ class LogServer
LogServer(); LogServer();
void work(); void work();
void sessionFinished( LogSession* s ); void sessionFinished( std::shared_ptr<LogSession> s );
private: private:
typedef std::list<LogSession*> SessionList; typedef std::list< std::shared_ptr<LogSession> > SessionList;
SessionList slist; SessionList slist;
UniSetTypes::uniset_rwmutex mutSList; UniSetTypes::uniset_rwmutex mutSList;
...@@ -79,7 +80,7 @@ class LogServer ...@@ -79,7 +80,7 @@ class LogServer
ThreadCreator<LogServer>* thr; ThreadCreator<LogServer>* thr;
ost::TCPSocket* tcp; ost::TCPSocket* tcp;
DebugStream* elog; std::shared_ptr<DebugStream> elog;
std::ostream* oslog; std::ostream* oslog;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#define LogSession_H_ #define LogSession_H_
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#include <string> #include <string>
#include <memory>
#include <deque> #include <deque>
#include <cc++/socket.h> #include <cc++/socket.h>
#include "Mutex.h" #include "Mutex.h"
...@@ -11,14 +12,15 @@ ...@@ -11,14 +12,15 @@
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/*! Реализация "сессии" для клиентов LogServer. */ /*! Реализация "сессии" для клиентов LogServer. */
class LogSession: class LogSession:
public std::enable_shared_from_this<LogSession>,
public ost::TCPSession public ost::TCPSession
{ {
public: public:
LogSession( ost::TCPSocket& server, DebugStream* log, timeout_t sessTimeout=10000, timeout_t cmdTimeout=2000, timeout_t outTimeout=2000, timeout_t delay=2000 ); LogSession( ost::TCPSocket& server, std::shared_ptr<DebugStream>& log, timeout_t sessTimeout=10000, timeout_t cmdTimeout=2000, timeout_t outTimeout=2000, timeout_t delay=2000 );
virtual ~LogSession(); virtual ~LogSession();
typedef sigc::slot<void, LogSession*> FinalSlot; typedef sigc::slot<void, std::shared_ptr<LogSession>> FinalSlot;
void connectFinalSession( FinalSlot sl ); void connectFinalSession( FinalSlot sl );
inline std::string getClientAddress(){ return caddr; } inline std::string getClientAddress(){ return caddr; }
...@@ -34,7 +36,7 @@ class LogSession: ...@@ -34,7 +36,7 @@ class LogSession:
LogBuffer lbuf; LogBuffer lbuf;
std::string peername; std::string peername;
std::string caddr; std::string caddr;
DebugStream* log; std::shared_ptr<DebugStream> log;
timeout_t sessTimeout; timeout_t sessTimeout;
timeout_t cmdTimeout; timeout_t cmdTimeout;
......
#include <memory>
#include "DebugExtBuf.h" #include "DebugExtBuf.h"
#include "LogAgregator.h" #include "LogAgregator.h"
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
...@@ -31,66 +32,68 @@ void LogAgregator::logOnEvent( const std::string& s ) ...@@ -31,66 +32,68 @@ void LogAgregator::logOnEvent( const std::string& s )
(*this) << s; (*this) << s;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogAgregator::add( DebugStream& l ) std::shared_ptr<DebugStream> LogAgregator::create( const std::string& logname )
{ {
l.signal_stream_event().connect( sigc::mem_fun(this, &LogAgregator::logOnEvent) ); auto t = getLog(logname);
for( LogList::iterator i=llst.begin(); i!=llst.end(); i++ ) if( t )
{ return t;
if( &l == i->log )
return;
}
llst.push_back(&l); auto l = std::make_shared<DebugStream>();
l->setLogName(logname);
l->signal_stream_event().connect( sigc::mem_fun(this, &LogAgregator::logOnEvent) );
lmap[logname] = l;
return l;
}
// -------------------------------------------------------------------------
void LogAgregator::add( DebugStream& log )
{
// очень не красиво создавать shared-указатель по объекту
// вместо maske_shared..
auto l = std::shared_ptr<DebugStream>(&log);
add(l);
}
// -------------------------------------------------------------------------
void LogAgregator::add( std::shared_ptr<DebugStream>& l )
{
auto i = lmap.find(l->getLogName());
if( i != lmap.end() )
return;
l->signal_stream_event().connect( sigc::mem_fun(this, &LogAgregator::logOnEvent) );
lmap[l->getLogName()] = l;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogAgregator::addLevel( const std::string& logname, Debug::type t ) void LogAgregator::addLevel( const std::string& logname, Debug::type t )
{ {
for( auto& i: llst ) auto i = lmap.find(logname);
{ if( i != lmap.end() )
if( i.log->getLogName() == logname ) i->second.log->addLevel(t);
{
i.log->addLevel(t);
break;
}
}
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogAgregator::delLevel( const std::string& logname, Debug::type t ) void LogAgregator::delLevel( const std::string& logname, Debug::type t )
{ {
for( auto& i: llst ) auto i = lmap.find(logname);
{ if( i != lmap.end() )
if( i.log->getLogName() == logname ) i->second.log->delLevel(t);
{
i.log->delLevel(t);
break;
}
}
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogAgregator::level( const std::string& logname, Debug::type t ) void LogAgregator::level( const std::string& logname, Debug::type t )
{ {
for( auto& i: llst ) auto i = lmap.find(logname);
{ if( i != lmap.end() )
if( i.log->getLogName() == logname ) i->second.log->level(t);
{
i.log->level(t);
break;
}
}
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
DebugStream* LogAgregator::getLog( const std::string& logname ) std::shared_ptr<DebugStream> LogAgregator::getLog( const std::string& logname )
{ {
if( logname.empty() ) if( logname.empty() )
return 0; return nullptr;
for( auto& i: llst ) auto i = lmap.find(logname);
{ if( i != lmap.end() )
if( i.log->getLogName() == logname ) return i->second.log;
return i.log;
}
return 0; return nullptr;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogAgregator::LogInfo LogAgregator::getLogInfo( const std::string& logname ) LogAgregator::LogInfo LogAgregator::getLogInfo( const std::string& logname )
...@@ -98,11 +101,9 @@ LogAgregator::LogInfo LogAgregator::getLogInfo( const std::string& logname ) ...@@ -98,11 +101,9 @@ LogAgregator::LogInfo LogAgregator::getLogInfo( const std::string& logname )
if( logname.empty() ) if( logname.empty() )
return LogInfo(); return LogInfo();
for( auto& i: llst ) auto i = lmap.find(logname);
{ if( i != lmap.end() )
if( i.log->getLogName() == logname ) return i->second;
return i;
}
return LogInfo(); return LogInfo();
} }
......
...@@ -23,7 +23,7 @@ LogServer::~LogServer() ...@@ -23,7 +23,7 @@ LogServer::~LogServer()
for( auto& i: slist ) for( auto& i: slist )
{ {
if( i->isRunning() ) if( i->isRunning() )
delete i; i.reset();
} }
} }
...@@ -38,12 +38,12 @@ outTimeout(2000), ...@@ -38,12 +38,12 @@ outTimeout(2000),
cancelled(false), cancelled(false),
thr(0), thr(0),
tcp(0), tcp(0),
elog(0), elog(nullptr),
oslog(&os) oslog(&os)
{ {
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogServer::LogServer( DebugStream& log ): LogServer::LogServer( std::shared_ptr<DebugStream>& log ):
timeout(TIMEOUT_INF), timeout(TIMEOUT_INF),
sessTimeout(3600000), sessTimeout(3600000),
cmdTimeout(2000), cmdTimeout(2000),
...@@ -51,7 +51,7 @@ outTimeout(2000), ...@@ -51,7 +51,7 @@ outTimeout(2000),
cancelled(false), cancelled(false),
thr(0), thr(0),
tcp(0), tcp(0),
elog(&log), elog(log),
oslog(0) oslog(0)
{ {
} }
...@@ -64,7 +64,7 @@ outTimeout(2000), ...@@ -64,7 +64,7 @@ outTimeout(2000),
cancelled(false), cancelled(false),
thr(0), thr(0),
tcp(0), tcp(0),
elog(0) elog(nullptr)
{ {
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
...@@ -110,7 +110,7 @@ void LogServer::work() ...@@ -110,7 +110,7 @@ void LogServer::work()
{ {
while( !cancelled && tcp->isPendingConnection(timeout) ) while( !cancelled && tcp->isPendingConnection(timeout) )
{ {
LogSession* s = new LogSession(*tcp, elog, sessTimeout, cmdTimeout, outTimeout); auto s = make_shared<LogSession>(*tcp, elog, sessTimeout, cmdTimeout, outTimeout);
{ {
uniset_rwmutex_wrlock l(mutSList); uniset_rwmutex_wrlock l(mutSList);
slist.push_back(s); slist.push_back(s);
...@@ -144,12 +144,12 @@ void LogServer::work() ...@@ -144,12 +144,12 @@ void LogServer::work()
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
void LogServer::sessionFinished( LogSession* s ) void LogServer::sessionFinished( std::shared_ptr<LogSession> s )
{ {
uniset_rwmutex_wrlock l(mutSList); uniset_rwmutex_wrlock l(mutSList);
for( SessionList::iterator i=slist.begin(); i!=slist.end(); ++i ) for( SessionList::iterator i=slist.begin(); i!=slist.end(); ++i )
{ {
if( (*i) == s ) if( i->get() == s.get() )
{ {
slist.erase(i); slist.erase(i);
return; return;
......
#include <iostream> #include <iostream>
#include <memory>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fcntl.h> #include <fcntl.h>
...@@ -23,7 +24,7 @@ LogSession::~LogSession() ...@@ -23,7 +24,7 @@ LogSession::~LogSession()
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogSession::LogSession( ost::TCPSocket &server, DebugStream* _log, timeout_t _sessTimeout, timeout_t _cmdTimeout, timeout_t _outTimeout, timeout_t _delay ): LogSession::LogSession( ost::TCPSocket &server, std::shared_ptr<DebugStream>& _log, timeout_t _sessTimeout, timeout_t _cmdTimeout, timeout_t _outTimeout, timeout_t _delay ):
TCPSession(server), TCPSession(server),
peername(""), peername(""),
caddr(""), caddr(""),
...@@ -85,12 +86,12 @@ void LogSession::run() ...@@ -85,12 +86,12 @@ void LogSession::run()
slog.info() << peername << "(run): receive command: '" << msg.cmd << "'" << endl; slog.info() << peername << "(run): receive command: '" << msg.cmd << "'" << endl;
string cmdLogName(msg.logname); string cmdLogName(msg.logname);
DebugStream* cmdlog = log; auto cmdlog = log;
string logfile(log->getLogFile()); string logfile(log->getLogFile());
if( !cmdLogName.empty () ) if( !cmdLogName.empty () )
{ {
LogAgregator* lag = dynamic_cast<LogAgregator*>(log); auto lag = dynamic_pointer_cast<LogAgregator>(log);
if( lag ) if( lag )
{ {
LogAgregator::LogInfo inf = lag->getLogInfo(cmdLogName); LogAgregator::LogInfo inf = lag->getLogInfo(cmdLogName);
...@@ -222,7 +223,7 @@ void LogSession::run() ...@@ -222,7 +223,7 @@ void LogSession::run()
void LogSession::final() void LogSession::final()
{ {
tcp()->sync(); tcp()->sync();
slFin(this); slFin( shared_from_this() );
delete this; delete this;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -41,37 +41,35 @@ int main( int argc, const char **argv ) ...@@ -41,37 +41,35 @@ int main( int argc, const char **argv )
cout << ss.str(); cout << ss.str();
cout << "==================" << endl; cout << "==================" << endl;
DebugStream log1; auto log1 = make_shared<DebugStream>();
log1.setLogName("log1"); log1->setLogName("log1");
DebugStream log2; auto log2 = make_shared<DebugStream>();
log2.setLogName("log2"); log2->setLogName("log2");
LogAgregator la; LogAgregator la;
la.signal_stream_event().connect(&check_alog_signal); la.signal_stream_event().connect(&check_alog_signal);
la.add(log1); la.add(log1);
la.add(log2); la.add(log2);
log1 << "log1: test message..." << endl; log1->any() << "log1: test message..." << endl;
log2 << "log2: test message..." << endl; log2->any() << "log2: test message..." << endl;
la << "la: test message.." << endl; la << "la: test message.." << endl;
cout << "===== Test 2 =====" << endl; cout << "===== Test 2 =====" << endl;
cout << ss1.str(); cout << ss1.str();
cout << "==================" << endl; cout << "==================" << endl;
DebugStream* l = la.getLog("log1"); auto l = la.getLog("log1");
if( l != &log1 ) if( l != &log1 )
cout << "**** TEST FAILED: LogAgregator::getLog() " << endl; cout << "**** TEST FAILED: LogAgregator::getLog() " << endl;
cout << "===== Test 3 =====" << endl; cout << "===== Test 3 =====" << endl;
tlog.level(Debug::ANY); tlog.level(Debug::ANY);
tlog.logFile("tlog.log"); tlog.logFile("tlog.log");
tlog << "TEST TEXT" << endl; tlog << "TEST TEXT" << endl;
tlog.logFile("tlog.log",true); tlog.logFile("tlog.log",true);
return 0; return 0;
} }
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