Commit 549a3dc5 authored by Pavel Vainerman's avatar Pavel Vainerman

(LogServer): сделал test-logserver "noinst"

+ ..небольшое переформатирование кода.. (LogServer): оптимизировал работу с очередью
parent 98b2caf9
bin_PROGRAMS = @PACKAGE@-test-logserver @PACKAGE@-log bin_PROGRAMS = @PACKAGE@-log
noinst_PROGRAMS = @PACKAGE@-test-logserver
@PACKAGE@_test_logserver_SOURCES = logserver.cc @PACKAGE@_test_logserver_SOURCES = logserver.cc
@PACKAGE@_test_logserver_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS) @PACKAGE@_test_logserver_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
......
...@@ -13,9 +13,9 @@ using namespace std; ...@@ -13,9 +13,9 @@ using namespace std;
static struct option longopts[] = { static struct option longopts[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' }, { "verbose", no_argument, 0, 'v' },
{ "iaddr", required_argument, 0, 'a' }, { "iaddr", required_argument, 0, 'i' },
{ "port", required_argument, 0, 'p' }, { "port", required_argument, 0, 'p' },
{ "add", required_argument, 0, 'l' }, { "add", required_argument, 0, 'a' },
{ "del", required_argument, 0, 'd' }, { "del", required_argument, 0, 'd' },
{ "set", required_argument, 0, 's' }, { "set", required_argument, 0, 's' },
{ "off", required_argument, 0, 'o' }, { "off", required_argument, 0, 'o' },
...@@ -28,12 +28,12 @@ static void print_help() ...@@ -28,12 +28,12 @@ static void print_help()
{ {
printf("-h, --help - this message\n"); printf("-h, --help - this message\n");
printf("-v, --verbose - Print all messages to stdout\n"); printf("-v, --verbose - Print all messages to stdout\n");
printf("[-a|--iaddr] addr - ULogServer ip or hostname.\n"); printf("[-i|--iaddr] addr - ULogServer ip or hostname.\n");
printf("[-p|--port] port - ULogServer port.\n"); printf("[-p|--port] port - ULogServer port.\n");
printf("\n"); printf("\n");
printf("Commands:\n"); printf("Commands:\n");
printf("[--add | -l] info,warn,crit,... - Add log levels.\n"); printf("[--add | -a] info,warn,crit,... - Add log levels.\n");
printf("[--del | -d] info,warn,crit,... - Delete log levels.\n"); printf("[--del | -d] info,warn,crit,... - Delete log levels.\n");
printf("[--set | -s] info,wanr,crit,... - Set log levels.\n"); printf("[--set | -s] info,wanr,crit,... - Set log levels.\n");
printf("--off, -o - Off the write log file (if enabled).\n"); printf("--off, -o - Off the write log file (if enabled).\n");
...@@ -55,7 +55,7 @@ int main( int argc, char **argv ) ...@@ -55,7 +55,7 @@ int main( int argc, char **argv )
try try
{ {
while( (opt = getopt_long(argc, argv, "hva:p:l:d:s:onr",longopts,&optindex)) != -1 ) while( (opt = getopt_long(argc, argv, "hva:p:i:d:s:onr",longopts,&optindex)) != -1 )
{ {
switch (opt) switch (opt)
{ {
...@@ -63,7 +63,7 @@ int main( int argc, char **argv ) ...@@ -63,7 +63,7 @@ int main( int argc, char **argv )
print_help(); print_help();
return 0; return 0;
case 'l': case 'a':
{ {
cmd = LogServerTypes::cmdAddLevel; cmd = LogServerTypes::cmdAddLevel;
sdata = string(optarg); sdata = string(optarg);
...@@ -91,7 +91,7 @@ int main( int argc, char **argv ) ...@@ -91,7 +91,7 @@ int main( int argc, char **argv )
cmd = LogServerTypes::cmdRotate; cmd = LogServerTypes::cmdRotate;
break; break;
case 'a': case 'i':
addr = string(optarg); addr = string(optarg);
break; break;
......
...@@ -82,6 +82,7 @@ int main( int argc, char **argv ) ...@@ -82,6 +82,7 @@ int main( int argc, char **argv )
} }
LogServer ls(dlog); LogServer ls(dlog);
// LogServer ls(cout);
dlog.addLevel(Debug::ANY); dlog.addLevel(Debug::ANY);
ls.run( addr, port, true ); ls.run( addr, port, true );
......
...@@ -32,6 +32,9 @@ ...@@ -32,6 +32,9 @@
</Services> </Services>
</UniSet> </UniSet>
<dlog name="dlog"/> <dlog name="dlog"/>
<LogServer name="dlog" port="3333" host="localhost" />
<LogServer name="ulog" port="3335" host="localhost" />
<settings> <settings>
<TestProc name="TestProc1" <TestProc name="TestProc1"
on_s="Input1_S" on_s="Input1_S"
......
...@@ -16,14 +16,16 @@ ...@@ -16,14 +16,16 @@
#ifdef UNISET_ENABLE_IO #ifdef UNISET_ENABLE_IO
#include "IOControl.h" #include "IOControl.h"
#endif #endif
#include "LogServer.h"
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
using namespace std; using namespace std;
using namespace UniSetTypes; using namespace UniSetTypes;
using namespace UniSetExtensions; using namespace UniSetExtensions;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
const int MaxAddNum = 10; const unsigned int MaxAddNum = 10;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
static void help_print( int argc, const char* argv[] ); static void help_print( int argc, const char* argv[] );
static LogServer* run_logserver( const std::string& cnamem, DebugStream& log );
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
int main( int argc, const char **argv ) int main( int argc, const char **argv )
{ {
...@@ -40,8 +42,8 @@ int main( int argc, const char **argv ) ...@@ -40,8 +42,8 @@ int main( int argc, const char **argv )
string logfilename = conf->getArgParam("--logfile", "smemory-plus.log"); string logfilename = conf->getArgParam("--logfile", "smemory-plus.log");
string logname( conf->getLogDir() + logfilename ); string logname( conf->getLogDir() + logfilename );
UniSetExtensions::dlog.logFile( logname ); // UniSetExtensions::dlog.logFile( logname );
ulog.logFile( logname ); // ulog.logFile( logname );
conf->initDebug(UniSetExtensions::dlog,"dlog"); conf->initDebug(UniSetExtensions::dlog,"dlog");
UniSetActivator* act = UniSetActivator::Instance(); UniSetActivator* act = UniSetActivator::Instance();
...@@ -184,6 +186,18 @@ int main( int argc, const char **argv ) ...@@ -184,6 +186,18 @@ int main( int argc, const char **argv )
(*it)->start(); (*it)->start();
#endif #endif
if( run_logserver("ulog",ulog) == 0 )
{
ulog.crit() << "(smemory-plus): run logserver for 'ulog' FAILED" << endl;
return 1;
}
if( run_logserver("dlog",dlog) == 0 )
{
dlog.crit() << "(smemory-plus): run logserver for 'dlog' FAILED" << endl;
return 1;
}
act->run(false); act->run(false);
on_sigchild(SIGTERM); on_sigchild(SIGTERM);
...@@ -240,3 +254,47 @@ void help_print( int argc, const char* argv[] ) ...@@ -240,3 +254,47 @@ void help_print( int argc, const char* argv[] )
cout << "--confile - Use confile. Default: configure.xml" << endl; cout << "--confile - Use confile. Default: configure.xml" << endl;
cout << "--logfile - Use logfile. Default: smemory-plus.log" << endl; cout << "--logfile - Use logfile. Default: smemory-plus.log" << endl;
} }
// -----------------------------------------------------------------------------
LogServer* run_logserver( const std::string& cname, DebugStream& log )
{
const UniXML* xml = UniSetTypes::conf->getConfXML();
xmlNode* cnode = UniSetTypes::conf->findNode(xml->getFirstNode(),"LogServer",cname);
if( cnode == 0 )
{
cerr << "(init_ulogserver): Not found xmlnode for '" << cname << "'" << endl;
return 0;
}
UniXML::iterator it(cnode);
LogServer* ls = new LogServer( log );
timeout_t sessTimeout = conf->getArgPInt("--" + cname + "-session-timeout",it.getProp("sessTimeout"),3600000);
timeout_t cmdTimeout = conf->getArgPInt("--" + cname + "-cmd-timeout",it.getProp("cmdTimeout"),2000);
timeout_t outTimeout = conf->getArgPInt("--" + cname + "-out-timeout",it.getProp("outTimeout"),2000);
ls->setSessionTimeout(sessTimeout);
ls->setCmdTimeout(cmdTimeout);
ls->setOutTimeout(outTimeout);
std::string host = conf->getArgParam("--" + cname + "-host",it.getProp("host"));
if( host.empty() )
{
cerr << "(init_ulogserver): " << cname << ": unknown host.." << endl;
delete ls;
return 0;
}
ost::tpport_t port = conf->getArgPInt("--" + cname + "-port",it.getProp("port"),0);
if( port == 0 )
{
cerr << "(init_ulogserver): " << cname << ": unknown port.." << endl;
delete ls;
return 0;
}
ls->run(host, port, true);
return ls;
}
// -----------------------------------------------------------------------------
...@@ -31,6 +31,7 @@ ${START} -f ./uniset2-smemory-plus --smemory-id SharedMemory --confile test.xml ...@@ -31,6 +31,7 @@ ${START} -f ./uniset2-smemory-plus --smemory-id SharedMemory --confile test.xml
--mbtcp2-gateway-port 2049 \ --mbtcp2-gateway-port 2049 \
--mbtcp2-recv-timeout 200 \ --mbtcp2-recv-timeout 200 \
--mbtcp2-force-out 1 \ --mbtcp2-force-out 1 \
$*
# --add-rtu \ # --add-rtu \
# --rs-dev /dev/cbsideA1 \ # --rs-dev /dev/cbsideA1 \
# --rs-id RTUExchange \ # --rs-id RTUExchange \
......
...@@ -24,7 +24,6 @@ ...@@ -24,7 +24,6 @@
#include <sigc++/sigc++.h> #include <sigc++/sigc++.h>
#include "Debug.h" #include "Debug.h"
#ifdef TEST_DEBUGSTREAM #ifdef TEST_DEBUGSTREAM
#include <string> #include <string>
struct Debug { struct Debug {
......
...@@ -15,6 +15,7 @@ class LogServer ...@@ -15,6 +15,7 @@ class LogServer
public: public:
LogServer( DebugStream& log ); LogServer( DebugStream& log );
LogServer( std::ostream& os );
~LogServer(); ~LogServer();
inline void setSessionTimeout( timeout_t msec ){ sessTimeout = msec; } inline void setSessionTimeout( timeout_t msec ){ sessTimeout = msec; }
...@@ -45,6 +46,7 @@ class LogServer ...@@ -45,6 +46,7 @@ class LogServer
ost::TCPSocket* tcp; ost::TCPSocket* tcp;
DebugStream* elog; DebugStream* elog;
std::ostream* oslog;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#endif // LogServer_H_ #endif // LogServer_H_
......
...@@ -14,7 +14,7 @@ class LogSession: ...@@ -14,7 +14,7 @@ class LogSession:
{ {
public: public:
LogSession( ost::TCPSocket &server, DebugStream* log, timeout_t sessTimeout=10000, timeout_t cmdTimeout=2000, timeout_t outTimeout=2000 ); LogSession( ost::TCPSocket &server, 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, LogSession*> FinalSlot;
...@@ -26,6 +26,7 @@ class LogSession: ...@@ -26,6 +26,7 @@ class LogSession:
virtual void run(); virtual void run();
virtual void final(); virtual void final();
void logOnEvent( const std::string& s ); void logOnEvent( const std::string& s );
void readStream();
private: private:
typedef std::deque<std::string> LogBuffer; typedef std::deque<std::string> LogBuffer;
...@@ -37,6 +38,7 @@ class LogSession: ...@@ -37,6 +38,7 @@ class LogSession:
timeout_t sessTimeout; timeout_t sessTimeout;
timeout_t cmdTimeout; timeout_t cmdTimeout;
timeout_t outTimeout; timeout_t outTimeout;
timeout_t delayTime;
PassiveTimer ptSessionTimeout; PassiveTimer ptSessionTimeout;
FinalSlot slFin; FinalSlot slFin;
...@@ -44,7 +46,6 @@ class LogSession: ...@@ -44,7 +46,6 @@ class LogSession:
UniSetTypes::uniset_rwmutex mLBuf; UniSetTypes::uniset_rwmutex mLBuf;
DebugStream slog; DebugStream slog;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#endif // LogSession_H_ #endif // LogSession_H_
......
...@@ -24,6 +24,19 @@ LogServer::~LogServer() ...@@ -24,6 +24,19 @@ LogServer::~LogServer()
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogServer::LogServer( std::ostream& os ):
timeout(TIMEOUT_INF),
sessTimeout(3600000),
cmdTimeout(2000),
outTimeout(2000),
cancelled(false),
thr(0),
tcp(0),
elog(0),
oslog(&os)
{
}
// -------------------------------------------------------------------------
LogServer::LogServer( DebugStream& log ): LogServer::LogServer( DebugStream& log ):
timeout(TIMEOUT_INF), timeout(TIMEOUT_INF),
sessTimeout(3600000), sessTimeout(3600000),
...@@ -32,7 +45,8 @@ outTimeout(2000), ...@@ -32,7 +45,8 @@ outTimeout(2000),
cancelled(false), cancelled(false),
thr(0), thr(0),
tcp(0), tcp(0),
elog(&log) elog(&log),
oslog(0)
{ {
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <sstream>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <cstring> #include <cstring>
...@@ -21,7 +22,7 @@ LogSession::~LogSession() ...@@ -21,7 +22,7 @@ LogSession::~LogSession()
} }
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
LogSession::LogSession( ost::TCPSocket &server, DebugStream* _log, timeout_t _sessTimeout, timeout_t _cmdTimeout, timeout_t _outTimeout ): LogSession::LogSession( ost::TCPSocket &server, DebugStream* _log, timeout_t _sessTimeout, timeout_t _cmdTimeout, timeout_t _outTimeout, timeout_t _delay ):
TCPSession(server), TCPSession(server),
peername(""), peername(""),
caddr(""), caddr(""),
...@@ -29,6 +30,7 @@ log(_log), ...@@ -29,6 +30,7 @@ log(_log),
sessTimeout(_sessTimeout), sessTimeout(_sessTimeout),
cmdTimeout(_cmdTimeout), cmdTimeout(_cmdTimeout),
outTimeout(_outTimeout), outTimeout(_outTimeout),
delayTime(_delay),
cancelled(false) cancelled(false)
{ {
// slog.addLevel(Debug::ANY); // slog.addLevel(Debug::ANY);
...@@ -141,18 +143,35 @@ void LogSession::run() ...@@ -141,18 +143,35 @@ void LogSession::run()
{ {
// slog.warn() << peername << "(run): send.." << endl; // slog.warn() << peername << "(run): send.." << endl;
ptSessionTimeout.reset(); ptSessionTimeout.reset();
uniset_rwmutex_wrlock l(mLBuf);
if( log )
{
// чтобы не застревать на посылке в сеть..
// делаем через промежуточный буффер (stringstream)
ostringstream sbuf;
bool send = false;
{
uniset_rwmutex_wrlock l(mLBuf);
if( !lbuf.empty() ) if( !lbuf.empty() )
{ {
slog.info() << peername << "(run): send messages.." << endl; slog.info() << peername << "(run): send messages.." << endl;
while( !lbuf.empty() ) while( !lbuf.empty() )
{ {
*tcp() << lbuf.front(); sbuf << lbuf.front();
lbuf.pop_front(); lbuf.pop_front();
} }
send = true;
}
}
if( send )
{
*tcp() << sbuf.str();
tcp()->sync(); tcp()->sync();
} }
// чтобы постоянно не проверять... (надо переделать на condition)
sleep(delayTime);
}
} }
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
//#include "DebugStream.h" //#include "DebugStream.h"
#include "Debug.h" #include "Debug.h"
#include "Mutex.h"
//�Since the current C++ lib in egcs does not have a standard implementation //�Since the current C++ lib in egcs does not have a standard implementation
// of basic_streambuf and basic_filebuf we don't have to include this // of basic_streambuf and basic_filebuf we don't have to include this
...@@ -229,11 +230,10 @@ private: ...@@ -229,11 +230,10 @@ private:
}; };
/// ///
class stringsigbuf : public stringbuf { class stringsigbuf : public streambuf {
public: public:
stringsigbuf():sb(0) stringsigbuf():sb(new stringbuf())
{ {
sb = new stringbuf();
} }
~stringsigbuf() ~stringsigbuf()
...@@ -243,7 +243,7 @@ public: ...@@ -243,7 +243,7 @@ public:
/// ///
stringsigbuf( stringbuf* b ) stringsigbuf( stringbuf* b )
: stringbuf(), sb(b) {} : streambuf(), sb(b) {}
typedef sigc::signal<void,const std::string&> StrBufOverflow_Signal; typedef sigc::signal<void,const std::string&> StrBufOverflow_Signal;
inline StrBufOverflow_Signal signal_overflow(){ return s_overflow; } inline StrBufOverflow_Signal signal_overflow(){ return s_overflow; }
...@@ -252,11 +252,13 @@ protected: ...@@ -252,11 +252,13 @@ protected:
#ifdef MODERN_STL_STREAMS #ifdef MODERN_STL_STREAMS
/// ///
virtual int sync() { virtual int sync() {
UniSetTypes::uniset_mutex_lock l(mut);
return sb->pubsync(); return sb->pubsync();
} }
/// ///
virtual streamsize xsputn(char_type const * p, streamsize n) { virtual streamsize xsputn(char_type const * p, streamsize n) {
UniSetTypes::uniset_mutex_lock l(mut);
return sb->sputn(p, n); return sb->sputn(p, n);
} }
/// ///
...@@ -264,9 +266,12 @@ protected: ...@@ -264,9 +266,12 @@ protected:
int_type r = sb->sputc(c); int_type r = sb->sputc(c);
if( r == '\n' ) if( r == '\n' )
{ {
UniSetTypes::uniset_mutex_lock l(mut);
s_overflow.emit( sb->str() ); s_overflow.emit( sb->str() );
delete sb; sb->str("");
sb = new stringbuf(); // stringbuf* old = sb;
// sb = new stringbuf();
// delete old;
} }
return r; return r;
} }
...@@ -287,7 +292,15 @@ protected: ...@@ -287,7 +292,15 @@ protected:
if( r == '\n' ) if( r == '\n' )
{ {
s_overflow.emit( sb->str() ); s_overflow.emit( sb->str() );
sb->rdbug.clear(); if( r == '\n' )
{
s_overflow.emit( sb->str() );
uniset_mutex_lock l(mut);
// из-за многопоточности.. сперва переприсвоим указатель на новый поток..
// а потом уже удалим старый..
stringbuf* old = sb;
sb = new stringbuf();
delete old;
} }
return r; return r;
} }
...@@ -296,6 +309,7 @@ private: ...@@ -296,6 +309,7 @@ private:
/// ///
StrBufOverflow_Signal s_overflow; StrBufOverflow_Signal s_overflow;
stringbuf* sb; stringbuf* sb;
UniSetTypes::uniset_mutex mut;
}; };
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
...@@ -376,6 +390,10 @@ void DebugStream::logFile( const std::string& f ) ...@@ -376,6 +390,10 @@ void DebugStream::logFile( const std::string& f )
internal = new debugstream_internal; internal = new debugstream_internal;
} }
if( !internal_sbuf ) {
internal_sbuf = new debugstream_sbuf;
}
if( !f.empty() ) if( !f.empty() )
{ {
internal->fbuf.open(f.c_str(), ios::out|ios::app); internal->fbuf.open(f.c_str(), ios::out|ios::app);
......
...@@ -30,8 +30,9 @@ int main( int argc, const char **argv ) ...@@ -30,8 +30,9 @@ int main( int argc, const char **argv )
if( tlog.is_level1() ) if( tlog.is_level1() )
tlog.level1() << ": is level1..." << endl; tlog.level1() << ": is level1..." << endl;
cout << "********" << endl; cout << "==================" << endl;
cout << ss.str(); cout << ss.str();
cout << "==================" << endl;
return 0; return 0;
} }
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
#include "Mutex.h" #include "Mutex.h"
#include "ThreadCreator.h" #include "ThreadCreator.h"
#include "UniSetTypes.h" #include "UniSetTypes.h"
#include "modbus/TCPCheck.h" #include "TCPCheck.h"
using namespace std; using namespace std;
using namespace UniSetTypes; using namespace UniSetTypes;
......
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