Commit 807617b0 authored by Pavel Vainerman's avatar Pavel Vainerman

Merge branch 'log-monitor'

parents f9fa80af 41cba1cc
......@@ -3,7 +3,7 @@
############################################################################
SUBDIRS = scripts Admin NullController SViewer-text \
SMonit MBTester codegen SImitator
SMonit MBTester codegen SImitator ULog
bin_PROGRAMS = @PACKAGE@-log @PACKAGE@-logserver-wrap
noinst_PROGRAMS = @PACKAGE@-test-logserver @PACKAGE@-log-stdin
@PACKAGE@_test_logserver_SOURCES = logserver.cc
@PACKAGE@_test_logserver_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
@PACKAGE@_test_logserver_CPPFLAGS = $(COMCPP_CFLAGS)
@PACKAGE@_log_SOURCES = log.cc
@PACKAGE@_log_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
@PACKAGE@_log_CPPFLAGS = $(COMCPP_CFLAGS)
@PACKAGE@_log_stdin_SOURCES = log-stdin.cc
@PACKAGE@_log_stdin_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
@PACKAGE@_log_stdin_CPPFLAGS = $(COMCPP_CFLAGS)
@PACKAGE@_logserver_wrap_SOURCES = log-wrap.cc
@PACKAGE@_logserver_wrap_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
@PACKAGE@_logserver_wrap_CPPFLAGS = $(COMCPP_CFLAGS)
// --------------------------------------------------------------------------
#include <getopt.h>
#include <iostream>
#include <string>
#include "DebugStream.h"
#include "LogServer.h"
#include "Exceptions.h"
// --------------------------------------------------------------------------
using namespace UniSetTypes;
using namespace std;
// -------------------------------------------------------------------------
static struct option longopts[] = {
{ "help", no_argument, 0, 'h' },
{ "iaddr", required_argument, 0, 'i' },
{ "port", required_argument, 0, 'p' },
{ "verbose", no_argument, 0, 'v' },
{ NULL, 0, 0, 0 }
};
// --------------------------------------------------------------------------
static void print_help()
{
printf("-h - this message\n");
printf("-v - Print all messages to stdout\n");
printf("-i addr - LogServer ip or hostname. Default: localhost.\n");
printf("-p port - LogServer port. Default: 3333.\n");
}
// --------------------------------------------------------------------------
int main( int argc, char* argv[], char* envp[] )
{
int optindex = 0;
int opt = 0;
int verb = 0;
string addr("localhost");
int port = 3333;
try
{
while( (opt = getopt_long(argc, argv, "hvi:p:",longopts,&optindex)) != -1 )
{
switch (opt)
{
case 'h':
print_help();
return 0;
case 'i':
addr = string(optarg);
break;
case 'p':
port = atoi(optarg);
break;
case 'v':
verb = 1;
break;
case '?':
default:
printf("? argumnet\n");
return 0;
}
}
if( verb )
cout << "(init): listen " << addr << ":" << port << endl;
DebugStream log;
LogServer ls(log);
ls.run(addr,port,true);
char buf[10000];
while( true )
{
size_t r = read(fileno(stdin), buf, sizeof(buf)-1);
if( r > 0 )
{
buf[r] = '\0';
log << buf;
}
}
}
catch( SystemError& err )
{
cerr << "(log-stdin): " << err << endl;
return 1;
}
catch( Exception& ex )
{
cerr << "(log-stdin): " << ex << endl;
return 1;
}
catch(...)
{
cerr << "(log-stdin): catch(...)" << endl;
return 1;
}
return 0;
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
#include <string>
#include "DebugStream.h"
#include "UniSetTypes.h"
#include "LogServer.h"
#include "LogServerTypes.h"
#include "Exceptions.h"
#include <sys/wait.h>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
// --------------------------------------------------------------------------
using namespace UniSetTypes;
using namespace std;
// -------------------------------------------------------------------------
static void print_help()
{
printf("\n");
printf("Usage: uniset2-logserver-wrap listen-addr listen-port PROGRAMM ARGS..\n");
printf("\n");
}
// --------------------------------------------------------------------------
int main( int argc, char* argv[], char* envp[] )
{
if( argc < 4 )
{
print_help();
return 1;
}
string addr(argv[1]);
int port = atoi(argv[2]);
int pid;
int cp[2]; /* Child to parent pipe */
if( pipe(cp) < 0)
{
perror("Can't make pipe");
exit(1);
}
try
{
switch( pid = fork() )
{
case -1:
{
perror("Can't fork");
exit(1);
}
case 0:
{
/* Child. */
close(cp[0]);
close( fileno(stdout) );
dup2(cp[1],fileno(stdout));
close( fileno(stderr) );
dup2(fileno(stdout),fileno(stderr));
execvpe(argv[3], argv + 3, envp);
perror("No exec");
kill(getppid(), SIGQUIT);
exit(1);
}
break;
default:
{
/* Parent. */
close(cp[1]);
DebugStream zlog;
zlog.addLevel(Debug::ANY);
LogServer ls(zlog);
cout << "wrap: server " << addr << ":" << port << endl;
ls.run( addr, port, true );
char buf[5000];
while( true )
{
ssize_t r = read(cp[0], &buf, sizeof(buf)-1 );
if( r > 0 )
{
buf[r] = '\0';
zlog << buf;
}
}
exit(0);
}
break;
}
}
catch( SystemError& err )
{
cerr << "(logserver-wrap): " << err << endl;
return 1;
}
catch( Exception& ex )
{
cerr << "(logserver-wrap): " << ex << endl;
return 1;
}
catch(...)
{
cerr << "(logserver-wrap): catch(...)" << endl;
return 1;
}
return 0;
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
#include <string>
#include <getopt.h>
#include "Debug.h"
#include "UniSetTypes.h"
#include "Exceptions.h"
#include "LogReader.h"
#include "LogServerTypes.h"
// --------------------------------------------------------------------------
using namespace UniSetTypes;
using namespace std;
// --------------------------------------------------------------------------
static struct option longopts[] = {
{ "help", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' },
{ "iaddr", required_argument, 0, 'i' },
{ "port", required_argument, 0, 'p' },
{ "add", required_argument, 0, 'a' },
{ "del", required_argument, 0, 'd' },
{ "set", required_argument, 0, 's' },
{ "off", required_argument, 0, 'o' },
{ "on", required_argument, 0, 'n' },
{ "rotate", required_argument, 0, 'r' },
{ "logname", required_argument, 0, 'l' },
{ "command-only", no_argument, 0, 'b' },
{ NULL, 0, 0, 0 }
};
// --------------------------------------------------------------------------
static void print_help()
{
printf("-h, --help - this message\n");
printf("-v, --verbose - Print all messages to stdout\n");
printf("[-i|--iaddr] addr - LogServer ip or hostname.\n");
printf("[-p|--port] port - LogServer port.\n");
printf("[-l|--logname] name - Send command only for 'logname'.\n");
printf("[-b|--command-only] - Send command and break. (No read logs).\n");
printf("\n");
printf("Commands:\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");
printf("--on, -n - On the write log file (if before disabled).\n");
printf("--rotate, -r - rotate log file.\n");
}
// --------------------------------------------------------------------------
int main( int argc, char **argv )
{
int optindex = 0;
int opt = 0;
int verb = 0;
string addr("localhost");
int port = 3333;
DebugStream dlog;
int cmd = LogServerTypes::cmdNOP;
int data = 0;
string sdata("");
int cmdonly = 0;
string logname("");
try
{
while( (opt = getopt_long(argc, argv, "hva:p:i:d:s:l:onrb",longopts,&optindex)) != -1 )
{
switch (opt)
{
case 'h':
print_help();
return 0;
case 'a':
{
cmd = LogServerTypes::cmdAddLevel;
sdata = string(optarg);
}
break;
case 'd':
{
cmd = LogServerTypes::cmdDelLevel;
sdata = string(optarg);
}
break;
case 's':
{
cmd = LogServerTypes::cmdSetLevel;
sdata = string(optarg);
}
break;
case 'o':
cmd = LogServerTypes::cmdOffLogFile;
break;
case 'n':
cmd = LogServerTypes::cmdOnLogFile;
break;
case 'r':
cmd = LogServerTypes::cmdRotate;
break;
case 'i':
addr = string(optarg);
break;
case 'l':
logname = string(optarg);
break;
case 'b':
cmdonly = 1;
break;
case 'p':
port = uni_atoi(optarg);
break;
case 'v':
verb = 1;
break;
case '?':
default:
printf("? argumnet\n");
return 0;
}
}
if( verb )
{
cout << "(init): read from " << addr << ":" << port << endl;
dlog.addLevel( Debug::type(Debug::CRIT | Debug::WARN | Debug::INFO) );
}
LogReader lr;
lr.setCommandOnlyMode(cmdonly);
if( !sdata.empty() )
{
data = (int)Debug::value(sdata);
if( verb )
cout << "SEND COMMAND: '" << (LogServerTypes::Command)cmd << " data='" << sdata << "'" << endl;
}
lr.readlogs( addr, port, (LogServerTypes::Command)cmd, data, logname, verb );
}
catch( SystemError& err )
{
cerr << "(log): " << err << endl;
}
catch( Exception& ex )
{
cerr << "(log): " << ex << endl;
}
catch(...)
{
cerr << "(log): catch(...)" << endl;
}
return 0;
}
// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
#include <string>
#include <getopt.h>
#include "Debug.h"
#include "UniSetTypes.h"
#include "Exceptions.h"
#include "LogServer.h"
#include "LogAgregator.h"
// --------------------------------------------------------------------------
using namespace UniSetTypes;
using namespace std;
// --------------------------------------------------------------------------
static struct option longopts[] = {
{ "help", no_argument, 0, 'h' },
{ "iaddr", required_argument, 0, 'i' },
{ "port", required_argument, 0, 'p' },
{ "verbose", no_argument, 0, 'v' },
{ "delay", required_argument, 0, 'd' },
{ NULL, 0, 0, 0 }
};
// --------------------------------------------------------------------------
static void print_help()
{
printf("-h|--help - this message\n");
// printf("[-t|--timeout] msec - Timeout. Default: 2000.\n");
printf("[-v|--verbose] - Print all messages to stdout\n");
printf("[-i|--iaddr] addr - Inet address for listen connections.\n");
printf("[-p|--port] port - Bind port.\n");
printf("[-d|--delay] msec - Delay for generate message. Default 5000.\n");
}
// --------------------------------------------------------------------------
int main( int argc, char **argv )
{
int optindex = 0;
int opt = 0;
int verb = 0;
string addr("localhost");
int port = 3333;
int tout = 2000;
timeout_t delay = 5000;
try
{
while( (opt = getopt_long(argc, argv, "hvi:p:d:",longopts,&optindex)) != -1 )
{
switch (opt)
{
case 'h':
print_help();
return 0;
case 'i':
addr = string(optarg);
break;
case 'p':
port = uni_atoi(optarg);
break;
case 'd':
delay = uni_atoi(optarg);
break;
case 'v':
verb = 1;
break;
case '?':
default:
printf("? argumnet\n");
return 0;
}
}
if( verb )
{
cout << "(init): listen " << addr << ":" << port
// << " timeout=" << tout << " msec "
<< endl;
// dlog.addLevel( Debug::type(Debug::CRIT | Debug::WARN | Debug::INFO) );
}
DebugStream dlog;
dlog.setLogName("dlog");
DebugStream dlog2;
dlog2.setLogName("dlog2");
LogAgregator la;
la.add(dlog);
la.add(dlog2);
LogServer ls(la);
// LogServer ls(cout);
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;
dlog2.info() << ": dlog2: INFO message" << endl;
dlog2.warn() << ": dlog2: WARN message" << endl;
dlog2.crit() << ": dlog2: CRIT message" << endl;
msleep(delay);
}
}
catch( SystemError& err )
{
cerr << "(logserver): " << err << endl;
}
catch( Exception& ex )
{
cerr << "(logserver): " << ex << endl;
}
catch(...)
{
cerr << "(logserver): catch(...)" << endl;
}
return 0;
}
// --------------------------------------------------------------------------
#!/bin/sh
for i in `seq 1 60`; do
echo "MSG$i"
sleep 1
done
\ No newline at end of file
......@@ -319,7 +319,7 @@ void <xsl:value-of select="$CLASSNAME"/>_SK::sysCommand( const SystemMessage* _s
string fname( mylog.getLogFile() );
if( !fname.empty() )
{
mylog.logFile(fname.c_str());
mylog.logFile(fname.c_str(),true);
mylog &lt;&lt; myname &lt;&lt; "(sysCommand): ***************** mylog LOG ROTATE *****************" &lt;&lt; endl;
}
}
......
......@@ -14,7 +14,7 @@
<AutoStartUpTime name="1"/>
<DumpStateTime name="10"/>
<SleepTickMS name="500"/>
<UniSetDebug levels="crit,warn" name="ulog"/>
<UniSetDebug levels="" name="ulog"/>
<ConfDir name="./"/>
<DataDir name="./"/>
<BinDir name="./"/>
......@@ -32,6 +32,8 @@
</Services>
</UniSet>
<dlog name="dlog"/>
<LogServer name="smplus" port="3333" host="localhost" />
<settings>
<TestProc name="TestProc1"
on_s="Input1_S"
......
......@@ -186,8 +186,8 @@ AC_SUBST(UNISET_EXT_LIBS)
# export
LDFLAGS="${OMNI_LIBS} ${XML_LIBS}"
CXXFLAGS="-pedantic -Wall -funsigned-char -std=c++11 -g -D_GNU_SOURCE ${OMNI_CFLAGS} ${XML_CFLAGS} -I\$(top_builddir)/include"
LDFLAGS="${OMNI_LIBS} ${XML_LIBS} ${SIGC_LIBS}"
CXXFLAGS="-pedantic -Wall -funsigned-char -std=c++11 -g -D_GNU_SOURCE ${OMNI_CFLAGS} ${XML_CFLAGS} ${SIGC_CFLAGS} -I\$(top_builddir)/include"
AC_SUBST(LDFLAGS)
AC_SUBST(CXXFLAGS)
......@@ -221,12 +221,14 @@ AC_CONFIG_FILES([Makefile
IDL/Processes/Makefile
src/Communications/Makefile
src/Communications/Modbus/Makefile
src/Communications/TCP/Makefile
src/Interfaces/Makefile
src/ObjectRepository/Makefile
src/Processes/Makefile
src/Services/Makefile
src/Timers/Makefile
src/Various/Makefile
src/Log/Makefile
src/Makefile
include/Makefile
include/modbus/Makefile
......@@ -246,6 +248,7 @@ AC_CONFIG_FILES([Makefile
Utilities/codegen/Makefile
Utilities/codegen/uniset2-codegen
Utilities/codegen/tests/Makefile
Utilities/ULog/Makefile
extensions/Makefile
extensions/libUniSet2Extensions.pc
extensions/lib/Makefile
......
......@@ -1226,7 +1226,7 @@ void IOControl::sysCommand( const SystemMessage* sm )
string fname( ulog.getLogFile() );
if( !fname.empty() )
{
ulog.logFile(fname);
ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << endl;
}
......@@ -1234,7 +1234,7 @@ void IOControl::sysCommand( const SystemMessage* sm )
fname = dlog.getLogFile();
if( !fname.empty() )
{
dlog.logFile(fname);
dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** GGDEB LOG ROTATE *****************" << endl;
}
}
......
......@@ -148,7 +148,7 @@ void PassiveLProcessor::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname (ulog.getLogFile() );
if( !fname.empty() )
{
ulog.logFile(fname);
ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
}
......@@ -156,7 +156,7 @@ void PassiveLProcessor::sysCommand( const UniSetTypes::SystemMessage *sm )
fname = dlog.getLogFile();
if( !fname.empty() )
{
dlog.logFile(fname);
dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
}
}
......
......@@ -165,13 +165,19 @@ MBExchange::~MBExchange()
{
if( it1->second->rtu )
{
try {
delete it1->second->rtu;
it1->second->rtu = 0;
}catch(...){}
}
RTUDevice* d(it1->second);
for( auto it=d->regmap.begin(); it!=d->regmap.end(); ++it )
{
try {
delete it->second;
}catch(...){}
}
delete it1->second;
}
......@@ -193,6 +199,7 @@ void MBExchange::waitSMReady()
ostringstream err;
err << myname << "(waitSMReady): failed waiting SharedMemory " << ready_timeout << " msec";
dcrit << err.str() << endl;
if( checkProcActive() )
throw SystemError(err.str());
}
}
......@@ -233,7 +240,15 @@ void MBExchange::sigterm( int signo )
{
dwarn << myname << ": ********* SIGTERM(" << signo << ") ********" << endl;
setProcActive(false);
try
{
UniSetObject_LT::sigterm(signo);
}
catch( ... )
{
// std::exception_ptr p = std::current_exception();
// std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
}
}
// ------------------------------------------------------------------------------------------
void MBExchange::readConfiguration()
......@@ -2547,14 +2562,14 @@ void MBExchange::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname(ulog.getLogFile());
if( !fname.empty() )
{
ulog.logFile(fname);
ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
}
dlog << myname << "(sysCommand): logRotate" << std::endl;
fname = dlog.getLogFile();
if( !fname.empty() )
{
dlog.logFile(fname);
dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
}
}
......@@ -2580,7 +2595,8 @@ void MBExchange::askSensors( UniversalIO::UIOCommand cmd )
dcrit << err.str() << endl;
kill(SIGTERM,getpid()); // прерываем (перезапускаем) процесс...
throw SystemError(err.str());
// throw SystemError(err.str());
return;
}
try
......
......@@ -75,6 +75,12 @@ pollThread(0)
// -----------------------------------------------------------------------------
MBTCPMaster::~MBTCPMaster()
{
if( pollThread )
{
pollThread->stop();
if( pollThread->isRunning() )
pollThread->join();
}
delete pollThread;
//delete mbtcp;
}
......@@ -146,12 +152,20 @@ void MBTCPMaster::poll_thread()
if( sidExchangeMode != DefaultObjectId && force )
exchangeMode = shm->localGetValue(itExchangeMode,sidExchangeMode);
}
catch(...){}
catch(...)
{
throw;
}
try
{
poll();
}
catch(...){}
catch(...)
{
// if( !checkProcActive() )
throw;
}
if( !checkProcActive() )
break;
......@@ -160,6 +174,36 @@ void MBTCPMaster::poll_thread()
}
}
// -----------------------------------------------------------------------------
void MBTCPMaster::sigterm( int signo )
{
setProcActive(false);
if( pollThread )
{
pollThread->stop();
if( pollThread->isRunning() )
pollThread->join();
delete pollThread;
pollThread = 0;
}
try
{
MBExchange::sigterm(signo);
}
catch( const std::exception& ex )
{
cerr << "catch: " << ex.what() << endl;
}
catch( ... )
{
std::exception_ptr p = std::current_exception();
std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
}
}
// -----------------------------------------------------------------------------
void MBTCPMaster::help_print( int argc, const char* const* argv )
{
cout << "Default: prefix='mbtcp'" << endl;
......
......@@ -208,6 +208,7 @@ class MBTCPMaster:
protected:
virtual void sysCommand( const UniSetTypes::SystemMessage *sm ) override;
virtual std::shared_ptr<ModbusClient> initMB( bool reopen=false ) override;
virtual void sigterm( int signo ) override;
UniSetTypes::uniset_rwmutex mbMutex;
std::string iaddr;
......
......@@ -78,6 +78,9 @@ int main( int argc, const char** argv )
}
catch(...)
{
std::exception_ptr p = std::current_exception();
std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
dcrit << "(mbtcpmaster): catch ..." << std::endl;
}
......
......@@ -4,7 +4,7 @@
--confile test.xml \
--mbtcp-name MBMaster1 \
--smemory-id SharedMemory \
--dlog-add-levels info,crit,warn,level4,level3 \
--dlog-add-levels system,info,crit,warn,level4,level3 \
--mbtcp-set-prop-prefix \
--mbtcp-filter-field rs \
--mbtcp-filter-value 5 \
......
......@@ -623,7 +623,7 @@ void MBSlave::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname(ulog.getLogFile());
if( !fname.empty() )
{
ulog.logFile(fname);
ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
}
......@@ -631,7 +631,7 @@ void MBSlave::sysCommand( const UniSetTypes::SystemMessage *sm )
fname = dlog.getLogFile();
if( !fname.empty() )
{
dlog.logFile(fname);
dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
}
}
......
......@@ -264,7 +264,7 @@ CORBA::Boolean SharedMemory::exist()
// ------------------------------------------------------------------------------------------
void SharedMemory::sigterm( int signo )
{
if( signo == SIGTERM )
if( signo == SIGTERM && wdt )
wdt->stop();
// raise(SIGKILL);
IONotifyController_LT::sigterm(signo);
......
......@@ -16,15 +16,47 @@
#ifdef UNISET_ENABLE_IO
#include "IOControl.h"
#endif
#include "LogAgregator.h"
#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 );
static LogServer* logserver = 0;
#ifdef UNISET_ENABLE_IO
std::list< ThreadCreator<IOControl>* > lst_iothr;
#endif
// --------------------------------------------------------------------------
void activator_terminate( int signo )
{
if( logserver )
{
try
{
delete logserver;
logserver = 0;
}
catch(...){}
}
#ifdef UNISET_IO_ENABLE
for( auto& i: lst_iothr )
{
try
{
i->stop();
}
catch(...){}
}
#endif
}
// --------------------------------------------------------------------------
int main( int argc, const char **argv )
{
if( argc>1 && ( strcmp(argv[1],"--help")==0 || strcmp(argv[1],"-h")==0 ) )
......@@ -45,6 +77,7 @@ int main( int argc, const char **argv )
conf->initDebug(UniSetExtensions::dlog,"dlog");
UniSetActivator* act = UniSetActivator::Instance();
act->signal_terminate_event().connect( &activator_terminate );
// ------------ SharedMemory ----------------
SharedMemory* shm = SharedMemory::init_smemory(argc,argv);
if( shm == NULL )
......@@ -54,7 +87,7 @@ int main( int argc, const char **argv )
#ifdef UNISET_ENABLE_IO
// ------------ IOControl ----------------
std::list< ThreadCreator<IOControl>* > lst_iothr;
// std::list< ThreadCreator<IOControl>* > lst_iothr;
for( unsigned int i=0; i<MaxAddNum; i++ )
{
stringstream s;
......@@ -180,13 +213,24 @@ int main( int argc, const char **argv )
act->broadcast( sm.transport_msg() );
#ifdef UNISET_IO_ENABLE
for( std::list< ThreadCreator<IOControl>* >::iterator it=lst_iothr.begin(); it!=lst_iothr.end(); ++it )
(*it)->start();
for( auto& i: lst_iothr )
i->start();
#endif
LogAgregator la;
la.add(ulog);
la.add(dlog);
logserver = run_logserver("smplus",la);
if( logserver == 0 )
{
cerr << "(smemory-plus): run logserver for 'smplus' FAILED" << endl;
return 1;
}
act->run(false);
on_sigchild(SIGTERM);
on_sigchild(SIGTERM);
return 0;
}
catch(Exception& ex)
......@@ -240,3 +284,48 @@ 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;
}
cout << "logserver: " << host << ":" << port << endl;
ls->run(host, port, true);
return ls;
}
// -----------------------------------------------------------------------------
......@@ -4,7 +4,6 @@ ulimit -Sc 10000000
START=uniset2-start.sh
${START} -f ./uniset2-smemory-plus --smemory-id SharedMemory --confile test.xml \
--dlog-add-levels any \
--io-name IOControl \
--io-polltime 100 \
--io-s-filter-field io \
......@@ -31,6 +30,8 @@ ${START} -f ./uniset2-smemory-plus --smemory-id SharedMemory --confile test.xml
--mbtcp2-gateway-port 2049 \
--mbtcp2-recv-timeout 200 \
--mbtcp2-force-out 1 \
--ulog-add-levels system \
$*
# --add-rtu \
# --rs-dev /dev/cbsideA1 \
# --rs-id RTUExchange \
......
......@@ -523,7 +523,7 @@ void UNetExchange::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname(ulog.getLogFile());
if( !fname.empty() )
{
ulog.logFile(fname);
ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
}
......@@ -531,7 +531,7 @@ void UNetExchange::sysCommand( const UniSetTypes::SystemMessage *sm )
fname = dlog.getLogFile();
if( !fname.empty() )
{
dlog.logFile(fname);
dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
}
}
......
......@@ -273,7 +273,7 @@ void UObject_SK::sysCommand( const SystemMessage* _sm )
string fname( mylog.getLogFile() );
if( !fname.empty() )
{
mylog.logFile(fname.c_str());
mylog.logFile(fname.c_str(),true);
mylog << myname << "(sysCommand): ***************** mylog LOG ROTATE *****************" << endl;
}
}
......
......@@ -10,6 +10,7 @@
// but should be adaptable to any project.
// (c) 2002 adapted for UniSet by Lav, GNU GPL license
// Modify for UniSet by pv@eterspft.ru, GNU GPL license
#ifndef DEBUGSTREAM_H
#define DEBUGSTREAM_H
......@@ -20,6 +21,8 @@
#include <iostream>
#include <string>
#include <sigc++/sigc++.h>
#include "Debug.h"
#ifdef TEST_DEBUGSTREAM
#include <string>
......@@ -86,10 +89,13 @@ public:
/// Constructor, sets the log file to f, and the debug level to t.
explicit
DebugStream(char const * f, Debug::type t = Debug::NONE);
DebugStream(char const * f, Debug::type t = Debug::NONE, bool truncate=false );
///
~DebugStream();
virtual ~DebugStream();
typedef sigc::signal<void,const std::string&> StreamEvent_Signal;
StreamEvent_Signal signal_stream_event();
/// Sets the debug level to t.
void level(Debug::type t) {
......@@ -112,7 +118,7 @@ public:
}
/// Sets the debugstreams' logfile to f.
void logFile( const std::string& f );
virtual void logFile( const std::string& f, bool truncate=false );
inline std::string getLogFile(){ return fname; }
......@@ -194,7 +200,13 @@ public:
const DebugStream &operator=(const DebugStream& r);
private:
inline void setLogName( const std::string& n ){ logname = n; }
inline std::string getLogName(){ return logname; }
protected:
void sbuf_overflow( const std::string& s );
// private:
/// The current debug level
Debug::type dt;
/// The no-op stream.
......@@ -206,6 +218,8 @@ private:
bool show_datetime;
std::string fname;
StreamEvent_Signal s_stream;
std::string logname;
};
#endif
#ifndef LogAgregator_H_
#define LogAgregator_H_
// -------------------------------------------------------------------------
#include <string>
#include <list>
#include "DebugStream.h"
#include "LogServerTypes.h"
// -------------------------------------------------------------------------
class LogAgregator:
public DebugStream
{
public:
explicit LogAgregator( Debug::type t = Debug::NONE );
explicit LogAgregator( char const * f, Debug::type t = Debug::NONE );
virtual ~LogAgregator();
virtual void logFile( const std::string& f );
void add( DebugStream& log );
// Управление "подчинёнными" логами
void addLevel( const std::string& logname, Debug::type t );
void delLevel( const std::string& logname, Debug::type t );
void level( const std::string& logname, Debug::type t );
struct LogInfo
{
LogInfo():log(0),logfile(""){}
LogInfo( DebugStream* l ):log(l),logfile(l->getLogFile()){}
DebugStream* log;
std::string logfile;
};
DebugStream* getLog( const std::string& logname );
LogInfo getLogInfo( const std::string& logname );
protected:
void logOnEvent( const std::string& s );
private:
typedef std::list<LogInfo> LogList;
LogList llst;
};
// -------------------------------------------------------------------------
#endif // LogAgregator_H_
// -------------------------------------------------------------------------
#ifndef LogReader_H_
#define LogReader_H_
// -------------------------------------------------------------------------
#include <string>
#include <queue>
#include <cc++/socket.h>
#include "UTCPStream.h"
#include "DebugStream.h"
#include "LogServerTypes.h"
// -------------------------------------------------------------------------
class LogReader
{
public:
LogReader();
~LogReader();
void readlogs( const std::string& addr, ost::tpport_t port,
LogServerTypes::Command c = LogServerTypes::cmdNOP,
int data = 0,
const std::string& logname="",
bool verbose = false );
void readlogs( const std::string& addr, ost::tpport_t port, LogServerTypes::lsMessage& m, bool verbose = false );
bool isConnection();
inline void setCommandOnlyMode( bool s ){ cmdonly = s; }
protected:
void connect( const std::string& addr, ost::tpport_t port, timeout_t tout=TIMEOUT_INF );
void connect( ost::InetAddress addr, ost::tpport_t port, timeout_t tout=TIMEOUT_INF );
void disconnect();
private:
UTCPStream* tcp;
std::string iaddr;
ost::tpport_t port;
bool cmdonly;
DebugStream rlog;
};
// -------------------------------------------------------------------------
#endif // LogReader_H_
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
#ifndef LogServer_H_
#define LogServer_H_
// -------------------------------------------------------------------------
#include <list>
#include <string>
#include <cc++/socket.h>
#include "Mutex.h"
#include "DebugStream.h"
#include "ThreadCreator.h"
class LogSession;
// -------------------------------------------------------------------------
/*! \page pgLogServer Лог сервер
Лог сервер предназначен для возможности удалённого чтения логов (DebugStream).
Ему указывается host и port для прослушивания запросов, которые можно делать при помощи
LogReader. Читающих клиентов может быть скольугодно много, на каждого создаётся своя "сессия"(LogSession).
При этом через лог сервер имеется возможность управлять включением или отключением определённых уровней логов,
записью, отключением записи или ротацией файла с логами. DebugStream за которым ведётся "слежение"
задаётся в конструкторе для LogServer.
\code
DebugStream mylog;
LogServer logsrv(mylog);
...
logsrv.run(host,port,create_thread);
...
\endcode
При этом если необходимо управлять или читать сразу несколько логов можно воспользоваться специальным классом LogAgregator.
\code
DebugStream log1;
log1.setLogName("log1");
DebugStream log2;
log2.setLogName("log2");
LogAgregator la;
la.add(log1);
la.add(log2);
LogServer logsrv(la);
...
logsrv.run(host,port,create_thread);
...
\endcode
*/
// -------------------------------------------------------------------------
class LogServer
{
public:
LogServer( DebugStream& log );
LogServer( std::ostream& os );
~LogServer();
inline void setSessionTimeout( timeout_t msec ){ sessTimeout = msec; }
inline void setCmdTimeout( timeout_t msec ){ cmdTimeout = msec; }
inline void setOutTimeout( timeout_t msec ){ outTimeout = msec; }
void run( const std::string& addr, ost::tpport_t port, bool thread=true );
protected:
LogServer();
void work();
void sessionFinished( LogSession* s );
private:
typedef std::list<LogSession*> SessionList;
SessionList slist;
UniSetTypes::uniset_rwmutex mutSList;
timeout_t timeout;
timeout_t sessTimeout;
timeout_t cmdTimeout;
timeout_t outTimeout;
std::atomic_bool cancelled;
DebugStream mylog;
ThreadCreator<LogServer>* thr;
ost::TCPSocket* tcp;
DebugStream* elog;
std::ostream* oslog;
};
// -------------------------------------------------------------------------
#endif // LogServer_H_
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
#ifndef LogServerTypes_H_
#define LogServerTypes_H_
// -------------------------------------------------------------------------
#include <ostream>
#include <cstring>
// -------------------------------------------------------------------------
namespace LogServerTypes
{
const unsigned int MAGICNUM = 0x20140904;
enum Command
{
cmdNOP, /*!< отсутствие команды */
cmdSetLevel, /*!< установить уровень вывода */
cmdAddLevel, /*!< добавить уровень вывода */
cmdDelLevel, /*!< удалить уровень вывода */
cmdRotate, /*!< пересоздать файл с логами */
cmdOffLogFile, /*!< отключить запись файла логов (если включена) */
cmdOnLogFile /*!< включить запись файла логов (если была отключена) */
// cmdSetLogFile
};
std::ostream& operator<<(std::ostream& os, Command c );
struct lsMessage
{
lsMessage():magic(MAGICNUM),cmd(cmdNOP),data(0){ std::memset(logname,0,sizeof(logname)); }
unsigned int magic;
Command cmd;
unsigned int data;
static const size_t MAXLOGNAME = 20;
char logname[MAXLOGNAME+1]; // +1 reserverd for '\0'
void setLogName( const std::string& name );
// для команды 'cmdSetLogFile'
// static const size_t MAXLOGFILENAME = 200;
// char logfile[MAXLOGFILENAME];
}__attribute__((packed));
std::ostream& operator<<(std::ostream& os, lsMessage& m );
}
// -------------------------------------------------------------------------
#endif // LogServerTypes_H_
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
#ifndef LogSession_H_
#define LogSession_H_
// -------------------------------------------------------------------------
#include <string>
#include <deque>
#include <cc++/socket.h>
#include "Mutex.h"
#include "DebugStream.h"
#include "PassiveTimer.h"
// -------------------------------------------------------------------------
/*! Реализация "сессии" для клиентов LogServer. */
class 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 );
virtual ~LogSession();
typedef sigc::slot<void, LogSession*> FinalSlot;
void connectFinalSession( FinalSlot sl );
inline std::string getClientAddress(){ return caddr; }
protected:
virtual void run();
virtual void final();
void logOnEvent( const std::string& s );
void readStream();
private:
typedef std::deque<std::string> LogBuffer;
LogBuffer lbuf;
std::string peername;
std::string caddr;
DebugStream* log;
timeout_t sessTimeout;
timeout_t cmdTimeout;
timeout_t outTimeout;
timeout_t delayTime;
PassiveTimer ptSessionTimeout;
FinalSlot slFin;
std::atomic_bool cancelled;
UniSetTypes::uniset_rwmutex mLBuf;
DebugStream slog;
};
// -------------------------------------------------------------------------
#endif // LogSession_H_
// -------------------------------------------------------------------------
......@@ -177,9 +177,8 @@ template <class ThreadMaster>
void ThreadCreator<ThreadMaster>::run()
{
pid = getpid();
if(m)
if( m )
(m->*act)();
// PosixThread::stop()
}
//----------------------------------------------------------------------------------------
template <class ThreadMaster>
......
......@@ -27,6 +27,7 @@
// --------------------------------------------------------------------------
#include <deque>
#include <omniORB4/CORBA.h>
#include <cc++/socket.h>
#include "UniSetTypes.h"
#include "UniSetObject.h"
#include "UniSetManager.h"
......@@ -67,6 +68,9 @@ class UniSetActivator:
virtual UniSetTypes::ObjectType getType() override { return UniSetTypes::ObjectType("UniSetActivator"); }
typedef sigc::signal<void,int> TerminateEvent_Signal;
TerminateEvent_Signal signal_terminate_event();
protected:
/*! Команды доступные при заказе сигналов
......@@ -118,6 +122,7 @@ class UniSetActivator:
ThreadCreator<UniSetActivator> *orbthr;
CORBA::ORB_var orb;
TerminateEvent_Signal s_term;
bool omDestroy;
bool sig;
......
......@@ -11,7 +11,7 @@
class WDTInterface
{
public:
WDTInterface(const std::string& dev);
WDTInterface( const std::string& dev );
~WDTInterface();
bool ping();
......
......@@ -15,10 +15,11 @@ libUniSet2_la_LDFLAGS = -version-info @LIBVER@
libUniSet2_la_LIBADD = -lm \
$(top_builddir)/src/Communications/libCommunications.la \
$(top_builddir)/src/Communications/Modbus/libModbus.la \
$(top_builddir)/src/Communications/TCP/libTCP.la \
$(top_builddir)/src/Interfaces/libInterfaces.la \
$(top_builddir)/src/ObjectRepository/libObjectsRepository.la \
$(top_builddir)/src/Processes/libProcesses.la \
$(top_builddir)/src/Services/libServices.la \
$(top_builddir)/src/Timers/libTimers.la \
$(top_builddir)/src/Various/libVarious.la
$(top_builddir)/src/Various/libVarious.la \
$(top_builddir)/src/Log/libLog.la
############################################################################
# This file is part of the UniSet library #
############################################################################
SUBDIRS=Modbus
SUBDIRS=TCP Modbus
noinst_LTLIBRARIES = libCommunications.la
libCommunications_la_SOURCES = ComPort.cc ComPort485F.cc
......
......@@ -2,10 +2,11 @@
# This file is part of the UniSet library #
############################################################################
noinst_LTLIBRARIES = libModbus.la
libModbus_la_SOURCES = ModbusTypes.cc ModbusHelpers.cc ModbusTCPSession.cc UTCPStream.cc \
libModbus_la_SOURCES = ModbusTypes.cc ModbusHelpers.cc ModbusTCPSession.cc \
ModbusClient.cc ModbusServer.cc ModbusServerSlot.cc \
ModbusRTUSlave.cc ModbusRTUSlaveSlot.cc ModbusRTUMaster.cc \
ModbusTCPCore.cc ModbusTCPServer.cc ModbusTCPServerSlot.cc ModbusTCPMaster.cc TCPCheck.cc
ModbusTCPCore.cc ModbusTCPServer.cc ModbusTCPServerSlot.cc ModbusTCPMaster.cc
libModbus_la_CXXFLAGS = -I$(top_builddir)/include/Communications/modbus $(SIGC_CFLAGS) $(COMCPP_CFLAGS)
#libModbus_la_LIBADD = $(top_builddir)/src/Communications/TCP/libTCP.la $(SIGC_LIBS) $(COMCPP_LIBS)
libModbus_la_LIBADD = $(SIGC_LIBS) $(COMCPP_LIBS)
......@@ -177,7 +177,7 @@ std::ostream& ModbusRTU::mbPrintMessage( std::ostream& os, ModbusByte* m, int le
// << setiosflags(ios::showbase) // для вывода в формате 0xNN
s << hex << showbase << setfill('0'); // << showbase;
for( unsigned int i=0; i<len; i++ )
for( ssize_t i=0; i<len; i++ )
s << setw(2) << (short)(m[i]) << " ";
// s << "<" << setw(2) << (int)(m[i]) << ">";
......@@ -965,7 +965,7 @@ ModbusMessage ReadOutputRetMessage::transport_msg()
// Создаём временно массив, переворачиваем байты
ModbusData* dtmp = new ModbusData[count];
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
dtmp[i] = SWAPSHORT(data[i]);
// копируем
......@@ -1119,7 +1119,7 @@ void ReadInputRetMessage::init( ModbusMessage& m )
void ReadInputRetMessage::swapData()
{
// переворачиваем данные
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
data[i] = SWAPSHORT(data[i]);
}
// -------------------------------------------------------------------------
......@@ -1174,7 +1174,7 @@ ModbusMessage ReadInputRetMessage::transport_msg()
// Создаём временно массив, переворачиваем байты
ModbusData* dtmp = new ModbusData[count];
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
dtmp[i] = SWAPSHORT(data[i]);
// копируем
......@@ -1391,7 +1391,7 @@ std::ostream& ModbusRTU::operator<<(std::ostream& os, ForceCoilsMessage& m )
<< " bcnt=" << b2str(m.bcnt)
<< " data[" << (int)m.quant <<"]={ ";
for( unsigned int i=0; i<m.bcnt; i++ )
for( ssize_t i=0; i<m.bcnt; i++ )
{
DataBits d(m.data[i]);
os << "" << d << " ";
......@@ -1539,7 +1539,7 @@ ModbusMessage WriteOutputMessage::transport_msg()
// Создаём временно массив, переворачиваем байты
ModbusData* dtmp = new ModbusData[quant];
for( unsigned int i=0; i<quant; i++ )
for( ssize_t i=0; i<quant; i++ )
dtmp[i] = SWAPSHORT(data[i]);
// копируем данные
......@@ -1606,7 +1606,7 @@ void WriteOutputMessage::init( ModbusMessage& m )
memcpy(&crc,&(m.data[m.len-szCRC]),szCRC);
int count( bcnt/sizeof(ModbusData) );
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
data[i] = SWAPSHORT(data[i]);
}
// -------------------------------------------------------------------------
......@@ -1651,7 +1651,7 @@ std::ostream& ModbusRTU::operator<<(std::ostream& os, WriteOutputMessage& m )
<< " bcnt=" << dat2str(m.bcnt)
<< " data[" << (int)m.quant <<"]={ ";
for( unsigned int i=0; i<m.quant; i++ )
for( ssize_t i=0; i<m.quant; i++ )
os << "" << dat2str(m.data[i]) << " ";
os << "}";
......@@ -2178,7 +2178,7 @@ void DiagnosticMessage::init( ModbusMessage& m )
last +=sizeof(ModbusData)*count;
// переворачиваем данные
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
data[i] = SWAPSHORT(data[i]);
memcpy(&crc,&(m.data[last]),szCRC);
......@@ -2243,7 +2243,7 @@ ModbusMessage DiagnosticMessage::transport_msg()
// Создаём временно массив, переворачиваем байты
ModbusData* dtmp = new ModbusData[count];
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
dtmp[i] = SWAPSHORT(data[i]);
// копируем
......@@ -2294,7 +2294,7 @@ std::ostream& ModbusRTU::operator<<(std::ostream& os, DiagnosticMessage& m )
<< " subf=" << dat2str(m.subf)
<< " data[" << m.count << "]={";
for( unsigned int i=0; i<m.count; i++ )
for( ssize_t i=0; i<m.count; i++ )
os << dat2str(m.data[i]) << " ";
os << "}";
......@@ -2314,7 +2314,7 @@ std::ostream& ModbusRTU::operator<<(std::ostream& os, DiagnosticRetMessage& m )
<< " subf=" << dat2str(m.subf)
<< " data[" << m.count << "]={";
for( unsigned int i=0; i<m.count; i++ )
for( ssize_t i=0; i<m.count; i++ )
os << dat2str(m.data[i]) << " ";
os << "}";
......@@ -2440,7 +2440,7 @@ RDIObjectInfo::RDIObjectInfo( ModbusByte id, ModbusByte* dat, ModbusByte len ):
{
val.reserve(len);
for( unsigned int i=0; i<len; i++ )
for( ssize_t i=0; i<len; i++ )
val.push_back( (char)dat[i] );
}
// -------------------------------------------------------------------------
......@@ -2738,7 +2738,7 @@ ModbusMessage JournalCommandRetMessage::transport_msg()
// --------------------
// копирование с переворотом данных (для ModbusData)
ModbusData* dtmp = new ModbusData[count];
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
dtmp[i] = SWAPSHORT(data[i]);
// копируем
......@@ -3240,7 +3240,7 @@ void ReadFileRecordMessage::init( ModbusMessage& m )
memcpy(&crc,&(m.data[m.len-szCRC]),szCRC);
count = bcnt/sizeof(SubRequest);
for( unsigned int i=0; i<count; i++ )
for( ssize_t i=0; i<count; i++ )
{
data[i].numfile = SWAPSHORT(data[i].numfile);
data[i].numrec = SWAPSHORT(data[i].numrec);
......@@ -3440,7 +3440,7 @@ ModbusMessage FileTransferRetMessage::transport_msg()
// копируем предварительный заголовок
ModbusData dhead[] = { numfile, numpacks, packet };
for( unsigned int i=0; i<sizeof(dhead)/sizeof(ModbusData); i++ )
for( size_t i=0; i<sizeof(dhead)/sizeof(ModbusData); i++ )
dhead[i] = SWAPSHORT(dhead[i]);
memcpy(&(mm.data[ind]),dhead,sizeof(dhead));
......@@ -3479,7 +3479,7 @@ std::ostream& ModbusRTU::operator<<(std::ostream& os, FileTransferRetMessage* m
std::ostream& ModbusTCP::operator<<(std::ostream& os, MBAPHeader& m )
{
// m.swapdata();
for( unsigned int i=0; i<sizeof(m); i++ )
for( size_t i=0; i<sizeof(m); i++ )
os << ((unsigned char*)(&m))[i];
// m.swapdata();
return os;
......
############################################################################
# This file is part of the UniSet library #
############################################################################
noinst_LTLIBRARIES = libTCP.la
libTCP_la_SOURCES = UTCPStream.cc TCPCheck.cc
libTCP_la_CXXFLAGS = $(SIGC_CFLAGS) $(COMCPP_CFLAGS)
libTCP_la_LIBADD = $(SIGC_LIBS) $(COMCPP_LIBS)
......@@ -2,7 +2,7 @@
#include <sstream>
#include "UniSetTypes.h"
#include "PassiveTimer.h"
#include "modbus/TCPCheck.h"
#include "TCPCheck.h"
// -----------------------------------------------------------------------------
using namespace std;
// -----------------------------------------------------------------------------
......
......@@ -4,7 +4,7 @@
#include <errno.h>
#include <cstring>
#include <cc++/socket.h>
#include "modbus/UTCPStream.h"
#include "UTCPStream.h"
#include "PassiveTimer.h"
#include "UniSetTypes.h"
// -------------------------------------------------------------------------
......
#include "DebugExtBuf.h"
#include "LogAgregator.h"
// -------------------------------------------------------------------------
LogAgregator::LogAgregator( char const * f, Debug::type t ):
DebugStream(f,t)
{
delete rdbuf(new teebuf(&internal->fbuf,&internal->sbuf));
}
// -------------------------------------------------------------------------
LogAgregator::LogAgregator( Debug::type t ):
DebugStream(t)
{
delete rdbuf(new teebuf(&internal->nbuf,&internal->sbuf));
}
// -------------------------------------------------------------------------
void LogAgregator::logFile( const std::string& f )
{
DebugStream::logFile(f);
if( !f.empty() )
delete rdbuf(new teebuf(&internal->fbuf,&internal->sbuf));
else
delete rdbuf(new teebuf(&internal->nbuf,&internal->sbuf));
}
// -------------------------------------------------------------------------
LogAgregator::~LogAgregator()
{
}
// -------------------------------------------------------------------------
void LogAgregator::logOnEvent( const std::string& s )
{
(*this) << s;
}
// -------------------------------------------------------------------------
void LogAgregator::add( DebugStream& l )
{
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;
}
llst.push_back(&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;
}
}
}
// -------------------------------------------------------------------------
void LogAgregator::delLevel( const std::string& logname, Debug::type t )
{
for( auto& i: llst )
{
if( i.log->getLogName() == logname )
{
i.log->delLevel(t);
break;
}
}
}
// -------------------------------------------------------------------------
void LogAgregator::level( const std::string& logname, Debug::type t )
{
for( auto& i: llst )
{
if( i.log->getLogName() == logname )
{
i.log->level(t);
break;
}
}
}
// -------------------------------------------------------------------------
DebugStream* LogAgregator::getLog( const std::string& logname )
{
if( logname.empty() )
return 0;
for( auto& i: llst )
{
if( i.log->getLogName() == logname )
return i.log;
}
return 0;
}
// -------------------------------------------------------------------------
LogAgregator::LogInfo LogAgregator::getLogInfo( const std::string& logname )
{
if( logname.empty() )
return LogInfo();
for( auto& i: llst )
{
if( i.log->getLogName() == logname )
return i;
}
return LogInfo();
}
// -------------------------------------------------------------------------
#include <string.h>
#include <errno.h>
#include <iostream>
#include <sstream>
#include "Exceptions.h"
#include "LogReader.h"
#include "UniSetTypes.h"
// -------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
// -------------------------------------------------------------------------
LogReader::LogReader():
tcp(0),
iaddr(""),
cmdonly(false)
{
}
// -------------------------------------------------------------------------
LogReader::~LogReader()
{
if( isConnection() )
disconnect();
}
// -------------------------------------------------------------------------
void LogReader::connect( const std::string& addr, ost::tpport_t _port, timeout_t msec )
{
ost::InetAddress ia(addr.c_str());
connect(ia,_port,msec);
}
// -------------------------------------------------------------------------
void LogReader::connect( ost::InetAddress addr, ost::tpport_t _port, timeout_t msec )
{
if( tcp )
{
(*tcp) << endl;
disconnect();
delete tcp;
tcp = 0;
}
// if( !tcp )
// {
ostringstream s;
s << addr;
iaddr = s.str();
port = _port;
if( rlog.is_info() )
rlog.info() << "(LogReader): connect to " << iaddr << ":" << port << endl;
ost::Thread::setException(ost::Thread::throwException);
try
{
tcp = new UTCPStream();
tcp->create(iaddr,port,true,500);
tcp->setTimeout(msec);
tcp->setKeepAlive(true);
}
catch( std::exception& e )
{
if( rlog.debugging(Debug::CRIT) )
{
ostringstream s;
s << "(LogReader): connection " << s.str() << " error: " << e.what();
rlog.crit() << s.str() << std::endl;
}
delete tcp;
tcp = 0;
}
catch( ... )
{
if( rlog.debugging(Debug::CRIT) )
{
ostringstream s;
s << "(LogReader): connection " << s.str() << " error: catch ...";
rlog.crit() << s.str() << std::endl;
}
}
// }
}
// -------------------------------------------------------------------------
void LogReader::disconnect()
{
if( !tcp )
return;
if( rlog.is_info() )
rlog.info() << iaddr << "(LogReader): disconnect." << endl;
tcp->disconnect();
delete tcp;
tcp = 0;
}
// -------------------------------------------------------------------------
bool LogReader::isConnection()
{
return tcp && tcp->isConnected();
}
// -------------------------------------------------------------------------
void LogReader::readlogs( const std::string& _addr, ost::tpport_t _port, LogServerTypes::Command cmd, int data, const std::string& logname, bool verbose )
{
LogServerTypes::lsMessage msg;
msg.cmd = cmd;
msg.data = data;
msg.setLogName(logname);
readlogs(_addr,_port,msg,verbose );
}
// -------------------------------------------------------------------------
void LogReader::readlogs( const std::string& _addr, ost::tpport_t _port, LogServerTypes::lsMessage& msg, bool verbose )
{
timeout_t inTimeout = 10000;
timeout_t outTimeout = 6000;
timeout_t reconDelay = 5000;
char buf[100001];
if( verbose )
rlog.addLevel(Debug::ANY);
bool send_ok = false;
while( true )
{
if( !isConnection() )
connect(_addr,_port,reconDelay);
if( !isConnection() )
{
rlog.warn() << "**** connection timeout.." << endl;
msleep(reconDelay);
continue;
}
if( !send_ok && msg.cmd != LogServerTypes::cmdNOP )
{
if( tcp->isPending(ost::Socket::pendingOutput,outTimeout) )
{
rlog.info() << "** send command: logname='" << msg.logname << "' cmd='" << msg.cmd << "' data='" << msg.data << "'" << endl;
// LogServerTypes::lsMessage msg;
// msg.cmd = cmd;
// msg.data = data;
for( size_t i=0; i<sizeof(msg); i++ )
(*tcp) << ((unsigned char*)(&msg))[i];
tcp->sync();
send_ok = true;
}
else
rlog.warn() << "**** SEND COMMAND ('" << msg.cmd << "' FAILED!" << endl;
if( cmdonly )
{
disconnect();
return;
}
}
while( !cmdonly && tcp->isPending(ost::Socket::pendingInput,inTimeout) )
{
int n = tcp->peek( buf,sizeof(buf)-1 );
if( n > 0 )
{
tcp->read(buf,n);
buf[n] = '\0';
cout << buf;
}
else
break;
}
rlog.warn() << "...connection timeout..." << endl;
send_ok = false; // ??!! делать ли?
disconnect();
}
if( isConnection() )
disconnect();
}
// -------------------------------------------------------------------------
#include <sstream>
#include "LogServer.h"
#include "UniSetTypes.h"
#include "Exceptions.h"
#include "LogSession.h"
// -------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
// -------------------------------------------------------------------------
LogServer::~LogServer()
{
cancelled = true;
if( thr )
{
thr->stop();
if( thr->isRunning() )
thr->join();
delete thr;
}
{
// uniset_rwmutex_wrlock l(mutSList);
for( auto& i: slist )
{
if( i->isRunning() )
delete i;
}
}
delete tcp;
}
// -------------------------------------------------------------------------
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),
cmdTimeout(2000),
outTimeout(2000),
cancelled(false),
thr(0),
tcp(0),
elog(&log),
oslog(0)
{
}
// -------------------------------------------------------------------------
LogServer::LogServer():
timeout(TIMEOUT_INF),
sessTimeout(3600000),
cmdTimeout(2000),
outTimeout(2000),
cancelled(false),
thr(0),
tcp(0),
elog(0)
{
}
// -------------------------------------------------------------------------
void LogServer::run( const std::string& addr, ost::tpport_t port, bool thread )
{
try
{
ost::InetAddress iaddr(addr.c_str());
tcp = new ost::TCPSocket(iaddr,port);
}
catch( ost::Socket *socket )
{
ost::tpport_t port;
int errnum = socket->getErrorNumber();
ost::InetAddress saddr = (ost::InetAddress)socket->getPeer(&port);
ostringstream err;
err << "socket error " << saddr.getHostname() << ":" << port << " = " << errnum;
if( errnum == ost::Socket::errBindingFailed )
err << "bind failed; port busy" << endl;
else
err << "client socket failed" << endl;
throw SystemError( err.str() );
}
if( !thread )
work();
else
{
thr = new ThreadCreator<LogServer>(this, &LogServer::work);
thr->start();
}
}
// -------------------------------------------------------------------------
void LogServer::work()
{
cancelled = false;
while( !cancelled )
{
try
{
while( !cancelled && tcp->isPendingConnection(timeout) )
{
LogSession* s = new LogSession(*tcp, elog, sessTimeout, cmdTimeout, outTimeout);
{
uniset_rwmutex_wrlock l(mutSList);
slist.push_back(s);
}
s->connectFinalSession( sigc::mem_fun(this, &LogServer::sessionFinished) );
s->detach();
}
}
catch( ost::Socket *socket )
{
ost::tpport_t port;
int errnum = socket->getErrorNumber();
ost::InetAddress saddr = (ost::InetAddress)socket->getPeer(&port);
cerr << "socket error " << saddr.getHostname() << ":" << port << " = " << errnum << endl;
if( errnum == ost::Socket::errBindingFailed )
{
cerr << "bind failed; port busy" << endl;
// ::exit(-1);
}
else
cerr << "client socket failed" << endl;
}
}
cerr << "*** LOG SERVER THREAD STOPPED.." << endl;
{
// uniset_rwmutex_wrlock l(mutSList);
for( auto& i: slist )
i->disconnect();
}
}
// -------------------------------------------------------------------------
void LogServer::sessionFinished( LogSession* s )
{
uniset_rwmutex_wrlock l(mutSList);
for( SessionList::iterator i=slist.begin(); i!=slist.end(); ++i )
{
if( (*i) == s )
{
slist.erase(i);
return;
}
}
}
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
#include "LogServerTypes.h"
// -------------------------------------------------------------------------
using namespace std;
// -------------------------------------------------------------------------
std::ostream& LogServerTypes::operator<<(std::ostream& os, LogServerTypes::Command cmd )
{
switch( cmd )
{
case LogServerTypes::cmdSetLevel:
return os << "cmdSetLevel";
case LogServerTypes::cmdAddLevel:
return os << "cmdAddLevel";
case LogServerTypes::cmdDelLevel:
return os << "cmdDelLevel";
case LogServerTypes::cmdRotate:
return os << "cmdRotate";
case LogServerTypes::cmdOffLogFile:
return os << "cmdOffLogFile";
case LogServerTypes::cmdOnLogFile:
return os << "cmdOnLogFile";
default:
return os << "Unknown";
}
return os;
}
// -------------------------------------------------------------------------
std::ostream& LogServerTypes::operator<<(std::ostream& os, LogServerTypes::lsMessage& m )
{
return os << " magic=" << m.magic << " cmd=" << m.cmd << " data=" << m.data;
}
// -------------------------------------------------------------------------
void LogServerTypes::lsMessage::setLogName( const std::string& name )
{
size_t s = name.size()> MAXLOGNAME ? MAXLOGNAME : name.size();
memcpy( &logname, name.c_str(), s );
logname[s] = '\0';
}
// -------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <sstream>
#include <fcntl.h>
#include <errno.h>
#include <cstring>
#include <cc++/socket.h>
#include "LogSession.h"
#include "UniSetTypes.h"
#include "LogServerTypes.h"
#include "LogAgregator.h"
// -------------------------------------------------------------------------
using namespace std;
using namespace UniSetTypes;
// -------------------------------------------------------------------------
LogSession::~LogSession()
{
cancelled = true;
if( isRunning() )
{
disconnect();
ost::Thread::join();
}
}
// -------------------------------------------------------------------------
LogSession::LogSession( ost::TCPSocket &server, DebugStream* _log, timeout_t _sessTimeout, timeout_t _cmdTimeout, timeout_t _outTimeout, timeout_t _delay ):
TCPSession(server),
peername(""),
caddr(""),
log(_log),
sessTimeout(_sessTimeout),
cmdTimeout(_cmdTimeout),
outTimeout(_outTimeout),
delayTime(_delay),
cancelled(false)
{
//slog.addLevel(Debug::ANY);
log->signal_stream_event().connect( sigc::mem_fun(this, &LogSession::logOnEvent) );
}
// -------------------------------------------------------------------------
void LogSession::logOnEvent( const std::string& s )
{
uniset_rwmutex_wrlock l(mLBuf);
lbuf.push_back(s);
}
// -------------------------------------------------------------------------
void LogSession::run()
{
if( cancelled )
return;
{
ost::tpport_t p;
ost::InetAddress iaddr = getIPV4Peer(&p);
// resolve..
caddr = string( iaddr.getHostname() );
ostringstream s;
s << iaddr << ":" << p;
peername = s.str();
}
if( slog.debugging(Debug::INFO) )
slog[Debug::INFO] << peername << "(run): run thread of sessions.." << endl;
ptSessionTimeout.setTiming(sessTimeout);
setKeepAlive(true);
// setTimeout(sessTimeout);
// Команды могут посылаться только в начале сессии..
if( isPending(Socket::pendingInput, cmdTimeout) )
{
LogServerTypes::lsMessage msg;
// проверяем канал..(если данных нет, значит "клиент отвалился"...
if( peek( (void*)(&msg),sizeof(msg)) > 0 )
{
ssize_t ret = readData( &msg,sizeof(msg) );
if( ret!=sizeof(msg) || msg.magic!=LogServerTypes::MAGICNUM )
slog.warn() << peername << "(run): BAD MESSAGE..." << endl;
else
{
slog.info() << peername << "(run): receive command: '" << msg.cmd << "'" << endl;
string cmdLogName(msg.logname);
DebugStream* cmdlog = log;
string logfile(log->getLogFile());
if( !cmdLogName.empty () )
{
LogAgregator* lag = dynamic_cast<LogAgregator*>(log);
if( lag )
{
LogAgregator::LogInfo inf = lag->getLogInfo(cmdLogName);
if( inf.log )
{
cmdlog = inf.log;
logfile = inf.logfile;
}
else
{
// если имя задали, но такого лога не нашлось
// то игнорируем команду
cmdlog = 0;
logfile = "";
}
}
else
{
// если имя лога задали, а оно не совпадает с текущим
// игнорируем команду
if( log->getLogFile() != cmdLogName )
{
cmdlog = 0;
logfile = "";
}
}
}
// обрабатываем команды только если нашли log
if( cmdlog )
{
// Обработка команд..
// \warning Работа с логом ведётся без mutex-а, хотя он разделяется отдельными потоками
switch( msg.cmd )
{
case LogServerTypes::cmdSetLevel:
cmdlog->level( (Debug::type)msg.data );
break;
case LogServerTypes::cmdAddLevel:
cmdlog->addLevel( (Debug::type)msg.data );
break;
case LogServerTypes::cmdDelLevel:
cmdlog->delLevel( (Debug::type)msg.data );
break;
case LogServerTypes::cmdRotate:
if( !logfile.empty() )
cmdlog->logFile(logfile,true);
break;
case LogServerTypes::cmdOffLogFile:
{
if( !logfile.empty() )
cmdlog->logFile("");
}
break;
case LogServerTypes::cmdOnLogFile:
{
if( !logfile.empty() )
cmdlog->logFile(logfile);
}
break;
default:
slog.warn() << peername << "(run): Unknown command '" << msg.cmd << "'" << endl;
break;
}
}
}
}
}
cancelled = false;
while( !cancelled && isConnected() ) // !ptSessionTimeout.checkTime()
{
// проверка только ради проверки "целостности" соединения
if( isPending(Socket::pendingInput, 10) )
{
char buf[10];
// проверяем канал..(если данных нет, значит "клиент отвалился"...
if( peek(buf,sizeof(buf)) <=0 )
break;
}
if( isPending(Socket::pendingOutput, outTimeout) )
{
//slog.info() << peername << "(run): send.." << endl;
// ptSessionTimeout.reset();
// чтобы не застревать на посылке в сеть..
// делаем через промежуточный буффер (stringstream)
ostringstream sbuf;
bool send = false;
{
uniset_rwmutex_wrlock l(mLBuf);
if( !lbuf.empty() )
{
slog.info() << peername << "(run): send messages.." << endl;
while( !lbuf.empty() )
{
sbuf << lbuf.front();
lbuf.pop_front();
}
send = true;
}
}
if( send )
{
*tcp() << sbuf.str();
tcp()->sync();
}
// чтобы постоянно не проверять... (надо переделать на condition)
sleep(delayTime);
}
}
if( slog.debugging(Debug::INFO) )
slog[Debug::INFO] << peername << "(run): stop thread of sessions..disconnect.." << endl;
disconnect();
if( slog.debugging(Debug::INFO) )
slog[Debug::INFO] << peername << "(run): thread stopping..." << endl;
}
// -------------------------------------------------------------------------
void LogSession::final()
{
tcp()->sync();
slFin(this);
delete this;
}
// -------------------------------------------------------------------------
void LogSession::connectFinalSession( FinalSlot sl )
{
slFin = sl;
}
// ---------------------------------------------------------------------
############################################################################
# This file is part of the UniSet library #
############################################################################
noinst_LTLIBRARIES = libLog.la
libLog_la_CPPFLAGS = $(SIGC_CFLAGS) $(COMCPP_CFLAGS)
libLog_la_LIBADD = $(SIGC_LIBS) $(COMCPP_LIBS)
libLog_la_SOURCES = DebugStream.cc Debug.cc LogServerTypes.cc LogServer.cc LogSession.cc LogReader.cc LogAgregator.cc
......@@ -2,5 +2,5 @@
# This file is part of the UniSet library #
############################################################################
SUBDIRS=ObjectRepository Processes Interfaces Timers Services Various Communications
SUBDIRS=ObjectRepository Processes Interfaces Timers Services Various Communications Log
......@@ -48,7 +48,7 @@ using namespace std;
В этом обработчике происходит вызов UniSetActivator::oaDestroy(int signo) для фактического
завершения работы и заказывается сигнал SIG_ALRM на время TERMINATE_TIMEOUT,
c обработчиком UniSetActivator::finishterm в котором происходит
"надежное" прибивание текущего процесса (raise(SIGKILL)). Это сделано на тот случай, если
"надежное" прибивание текущего процесса (kill(getpid(),SIGKILL)). Это сделано на тот случай, если
в oaDestroy произойдет зависание.
*/
// ------------------------------------------------------------------------------------------
......@@ -140,7 +140,7 @@ void UniSetActivator::init()
UniSetActivator::~UniSetActivator()
{
if(!procterm )
if( !procterm )
{
ulogsys << myname << "(destructor): ..."<< endl << flush;
if( !omDestroy )
......@@ -153,7 +153,27 @@ UniSetActivator::~UniSetActivator()
}
if( orbthr )
{
orbthr->stop();
if( orbthr->isRunning() )
orbthr->join();
delete orbthr;
orbthr = 0;
}
#if 0
try
{
if( !CORBA::is_nil(orb) )
{
ulogsys << myname << "(oaDestroy): orb destroy... " << endl;
orb->destroy();
ulogsys << myname << "(oaDestroy): orb destroy ok."<< endl;
}
}
catch(...){}
#endif
}
// ------------------------------------------------------------------------------------------
......@@ -175,25 +195,59 @@ void UniSetActivator::oaDestroy(int signo)
}
catch(...){}
try
{
deactivateObject();
}
catch(...){}
ulogsys << myname << "(oaDestroy): pman deactivate... " << endl;
pman->deactivate(false,true);
ulogsys << myname << "(oaDestroy): pman deactivate ok. " << endl;
ulogsys << myname << "(oaDestroy): orb destroy... " << endl;
ulogsys << myname << "(oaDestroy): orbthr=" << orbthr << endl;
if( orbthr )
{
try
{
orb->destroy();
ulogsys << myname << "(oaDestroy): orb thread stop... " << endl;
orbthr->stop();
if( orbthr->isRunning() )
orbthr->join();
ulogsys << myname << "(oaDestroy): orb thread stop ok. " << endl;
}
catch(...){}
}
ulogsys << myname << "(oaDestroy): orb destroy ok."<< endl;
try
{
ulogsys << myname << "(stop):: shutdown orb... "<<endl;
orb->shutdown(false);
}
catch(...){}
ulogsys << myname << "(stop): shutdown ok."<< endl;
#if 0
try
{
if( !CORBA::is_nil(orb) )
{
ulogsys << myname << "(oaDestroy): orb destroy... " << endl;
orb->destroy();
ulogsys << myname << "(oaDestroy): orb destroy ok."<< endl;
}
}
catch(...){}
#endif
/*
if( orbthr )
{
delete orbthr;
orbthr = 0;
}
*/
}
// waittermMutex.unlock();
}
......@@ -266,16 +320,15 @@ void UniSetActivator::stop()
ulogsys << myname << "(stop): discard request ok."<< endl;
/*
#if 1
try
{
ulogsys << myname << "(stop):: shutdown orb... "<<endl;
orb->shutdown(false);
orb->shutdown(true);
}
catch(...){}
ulogsys << myname << "(stop): shutdown ok."<< endl;
*/
#endif
}
}
......@@ -283,7 +336,7 @@ void UniSetActivator::stop()
void UniSetActivator::work()
{
ulogsys << myname << "(work): запускаем orb на обработку запросов..."<< endl;
ulogsys << myname << "(work): запускаем orb на обработку запросов...(orbthr=" << orbthr << ")" << endl;
try
{
if( orbthr )
......@@ -313,17 +366,17 @@ void UniSetActivator::work()
ucrit << myname << "(work): catch ..." << endl;
}
ulogsys << myname << "(work): orb стоп!!!"<< endl;
ulogsys << myname << "(work): orb thread stopped!" << endl;
/*
ulogsys << myname << "(oaDestroy): orb destroy... " << endl;
try
{
orb->destroy();
}
catch(...){}
ulogsys << myname << "(oaDestroy): orb destroy ok."<< endl;
*/
}
// ------------------------------------------------------------------------------------------
void UniSetActivator::getinfo()
......@@ -356,7 +409,7 @@ void UniSetActivator::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname = ulog.getLogFile();
if( !fname.empty() )
{
ulog.logFile(fname.c_str());
ulog.logFile(fname.c_str(),true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << endl;
}
}
......@@ -364,18 +417,8 @@ void UniSetActivator::sysCommand( const UniSetTypes::SystemMessage *sm )
}
}
// -------------------------------------------------------------------------
/*
void UniSetActivator::sig_child(int signo)
{
ulogsys << gActivator->getName() << "(sig_child): дочерний процесс закончил работу...(sig=" << signo << ")" << endl;
while( waitpid(-1, 0, WNOHANG) > 0);
}
*/
// ------------------------------------------------------------------------------------------
void UniSetActivator::set_signals(bool ask)
{
struct sigaction act, oact;
sigemptyset(&act.sa_mask);
sigemptyset(&oact.sa_mask);
......@@ -419,10 +462,15 @@ void UniSetActivator::finishterm( int signo )
sigset(SIGALRM, SIG_DFL);
doneterm = 1;
raise(SIGKILL);
kill(getpid(),SIGKILL);
}
}
// ------------------------------------------------------------------------------------------
UniSetActivator::TerminateEvent_Signal UniSetActivator::signal_terminate_event()
{
return s_term;
}
// ------------------------------------------------------------------------------------------
void UniSetActivator::terminated( int signo )
{
if( !signo || doneterm || !gActivator || procterm )
......@@ -447,17 +495,24 @@ void UniSetActivator::terminated( int signo )
alarm(TERMINATE_TIMEOUT);
sigrelse(SIGALRM);
if( gActivator )
{
ulogsys << ( gActivator ? gActivator->getName() : "" ) << "(terminated): call oaDestroy.." << endl;
gActivator->oaDestroy(SIGNO); // gActivator->term(SIGNO);
}
doneterm = 1;
ulogsys << ( gActivator ? gActivator->getName() : "" ) << "(terminated): завершаемся..."<< endl<< flush;
if( gActivator )
{
UniSetActivator::set_signals(false);
delete gActivator;
gActivator = 0;
}
sigset(SIGALRM, SIG_DFL);
raise(SIGNO);
kill(getpid(), SIGNO );
}
}
}
......@@ -467,6 +522,9 @@ void UniSetActivator::normalexit()
{
if( gActivator )
ulogsys << gActivator->getName() << "(default exit): good bye."<< endl << flush;
// std::exception_ptr p = std::current_exception();
// std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
}
void UniSetActivator::normalterminate()
......@@ -474,6 +532,9 @@ void UniSetActivator::normalterminate()
if( gActivator )
ucrit << gActivator->getName() << "(default exception terminate): Никто не выловил исключение!!! Good bye."<< endl<< flush;
// abort();
// std::exception_ptr p = std::current_exception();
// std::clog <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
}
// ------------------------------------------------------------------------------------------
void UniSetActivator::term( int signo )
......@@ -490,7 +551,7 @@ void UniSetActivator::term( int signo )
{
ulogsys << myname << "(term): вызываем sigterm()" << endl;
sigterm(signo);
s_term.emit(signo);
ulogsys << myname << "(term): sigterm() ok." << endl;
}
catch(Exception& ex)
......@@ -506,7 +567,5 @@ void UniSetActivator::waitDestroy()
{
while( !doneterm && gActivator )
msleep(50);
gActivator = 0;
}
// ------------------------------------------------------------------------------------------
......@@ -118,6 +118,25 @@ UniSetManager::~UniSetManager()
managers(deactiv);
}
catch(...){}
for( auto& i: olist )
{
try
{
delete i;
}
catch(...){}
}
for( auto& i: mlist )
{
try
{
delete i;
}
catch(...){}
}
olist.clear();
mlist.clear();
}
......@@ -350,8 +369,18 @@ bool UniSetManager::deactivateObject()
void UniSetManager::sigterm( int signo )
{
sig=signo;
try
{
objects(term);
}
catch(...){}
try
{
managers(term);
}
catch(...){}
UniSetObject::sigterm(signo);
}
// ------------------------------------------------------------------------------------------
......
......@@ -134,16 +134,27 @@ stCountOfQueueFull(0)
// ------------------------------------------------------------------------------------------
UniSetObject::~UniSetObject()
{
try
{
deactivate();
}
catch(...){}
try
{
tmr->terminate();
}
catch(...){}
if( thr )
{
try
{
thr->stop();
if( thr->isRunning() )
thr->join();
}
catch(...){}
delete thr;
}
......@@ -621,6 +632,7 @@ bool UniSetObject::deactivate()
}
setActive(false); // завершаем поток обработки сообщений
if( tmr )
tmr->stop();
// Очищаем очередь
......@@ -816,10 +828,17 @@ void UniSetObject::processingMessage( UniSetTypes::VoidMessage *msg )
<< " mesg: " << fe.errmsg() << endl;
}
}
catch(...)
catch( const std::exception& ex )
{
ucrit << myname << "(processingMessage): " << ex.what() << endl;
}
/*
catch( ... )
{
ucrit << myname << "(processingMessage): catch..." << endl;
std::exception_ptr p = std::current_exception();
ucrit <<(p ? p.__cxa_exception_type()->name() : "null") << std::endl;
}
*/
}
// ------------------------------------------------------------------------------------------
......
......@@ -701,7 +701,10 @@ xmlNode* Configuration::initDebug( DebugStream& deb, const string& _debname )
else
{
if( !getProp(dnode,"name").empty() )
{
debname = getProp(dnode,"name");
deb.setLogName(debname);
}
}
string no_deb("--"+debname+"-no-debug");
......@@ -978,6 +981,7 @@ UniversalIO::IOType Configuration::getIOType( const std::string& name )
void uniset_init( int argc, const char* const* argv, const std::string& xmlfile )
{
string confile = UniSetTypes::getArgParam( "--confile", argc, argv, xmlfile );
ulog.setLogName("ulog");
UniSetTypes::conf = new Configuration(argc, argv, confile);
}
// -------------------------------------------------------------------------
......
......@@ -5,7 +5,7 @@
noinst_LTLIBRARIES = libVarious.la
libVarious_la_CPPFLAGS = $(SIGC_CFLAGS) $(COMCPP_CFLAGS)
libVarious_la_LIBADD = $(SIGC_LIBS) $(COMCPP_LIBS)
libVarious_la_SOURCES = DebugStream.cc Debug.cc UniXML.cc MessageType.cc Configuration.cc \
libVarious_la_SOURCES = UniXML.cc MessageType.cc Configuration.cc \
Restorer_XML.cc RunLock.cc Mutex.cc SViewer.cc SMonitor.cc LT_Object.cc WDTInterface.cc
local-clean:
......
#include <time.h>
#include "Debug.h"
#include "UniSetTypes.h"
#include "LogAgregator.h"
using namespace std;
using namespace UniSetTypes;
std::ostringstream ss;
std::ostringstream ss1;
void check_log_signal( const string& s )
{
ss << s;
}
void check_alog_signal( const string& s )
{
ss1 << s;
}
int main( int argc, const char **argv )
{
DebugStream tlog;
tlog.signal_stream_event().connect(&check_log_signal);
tlog.addLevel(Debug::ANY);
tlog[Debug::INFO] << ": [info] ..." << endl;
......@@ -20,5 +37,41 @@ int main( int argc, const char **argv )
if( tlog.is_level1() )
tlog.level1() << ": is level1..." << endl;
cout << "===== Test 1 =====" << endl;
cout << ss.str();
cout << "==================" << endl;
DebugStream log1;
log1.setLogName("log1");
DebugStream log2;
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;
la << "la: test message.." << endl;
cout << "===== Test 2 =====" << endl;
cout << ss1.str();
cout << "==================" << endl;
DebugStream* 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;
}
......@@ -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