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