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