Commit d1f6eb74 authored by Pavel Vainerman's avatar Pavel Vainerman

(ULogServer): работа над универсальный логсервером (обёрткой)

parent 549a3dc5
bin_PROGRAMS = @PACKAGE@-log bin_PROGRAMS = @PACKAGE@-log @PACKAGE@-log-stdin @PACKAGE@-logserver-wrap
noinst_PROGRAMS = @PACKAGE@-test-logserver noinst_PROGRAMS = @PACKAGE@-test-logserver
@PACKAGE@_test_logserver_SOURCES = logserver.cc @PACKAGE@_test_logserver_SOURCES = logserver.cc
...@@ -8,3 +8,11 @@ noinst_PROGRAMS = @PACKAGE@-test-logserver ...@@ -8,3 +8,11 @@ noinst_PROGRAMS = @PACKAGE@-test-logserver
@PACKAGE@_log_SOURCES = log.cc @PACKAGE@_log_SOURCES = log.cc
@PACKAGE@_log_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS) @PACKAGE@_log_LDADD = $(top_builddir)/lib/libUniSet2.la $(COMCPP_LIBS)
@PACKAGE@_log_CPPFLAGS = $(COMCPP_CFLAGS) @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(0, 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
{
/* Create a child to run command. */
switch( pid = fork() )
{
case -1:
{
perror("Can't fork");
exit(1);
}
case 0:
{
/* Child. */
close(1); /* Close current stdout. */
dup(cp[1]); /* Make stdout go to write end of pipe. */
// close(0); /* Close current stdin. */
// dup( pc[0]); /* Make stdin come from read end of pipe. */
close( cp[0]);
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;
}
// --------------------------------------------------------------------------
...@@ -11,7 +11,7 @@ using namespace std; ...@@ -11,7 +11,7 @@ using namespace std;
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
static struct option longopts[] = { static struct option longopts[] = {
{ "help", no_argument, 0, 'h' }, { "help", no_argument, 0, 'h' },
{ "iaddr", required_argument, 0, 'a' }, { "iaddr", required_argument, 0, 'i' },
{ "port", required_argument, 0, 'p' }, { "port", required_argument, 0, 'p' },
{ "verbose", no_argument, 0, 'v' }, { "verbose", no_argument, 0, 'v' },
{ "delay", required_argument, 0, 'd' }, { "delay", required_argument, 0, 'd' },
...@@ -23,7 +23,7 @@ static void print_help() ...@@ -23,7 +23,7 @@ static void print_help()
printf("-h|--help - this message\n"); printf("-h|--help - this message\n");
// printf("[-t|--timeout] msec - Timeout. Default: 2000.\n"); // printf("[-t|--timeout] msec - Timeout. Default: 2000.\n");
printf("[-v|--verbose] - Print all messages to stdout\n"); printf("[-v|--verbose] - Print all messages to stdout\n");
printf("[-a|--iaddr] addr - Inet address for listen connections.\n"); printf("[-i|--iaddr] addr - Inet address for listen connections.\n");
printf("[-p|--port] port - Bind port.\n"); printf("[-p|--port] port - Bind port.\n");
printf("[-d|--delay] msec - Delay for generate message. Default 5000.\n"); printf("[-d|--delay] msec - Delay for generate message. Default 5000.\n");
} }
...@@ -41,7 +41,7 @@ int main( int argc, char **argv ) ...@@ -41,7 +41,7 @@ int main( int argc, char **argv )
try try
{ {
while( (opt = getopt_long(argc, argv, "hva:p:d:",longopts,&optindex)) != -1 ) while( (opt = getopt_long(argc, argv, "hvi:p:d:",longopts,&optindex)) != -1 )
{ {
switch (opt) switch (opt)
{ {
...@@ -49,7 +49,7 @@ int main( int argc, char **argv ) ...@@ -49,7 +49,7 @@ int main( int argc, char **argv )
print_help(); print_help();
return 0; return 0;
case 'a': case 'i':
addr = string(optarg); addr = string(optarg);
break; break;
......
#!/bin/sh
for i in `seq 1 60`; do
echo "MSG$i"
sleep 1
done
\ No newline at end of file
...@@ -33,7 +33,7 @@ outTimeout(_outTimeout), ...@@ -33,7 +33,7 @@ outTimeout(_outTimeout),
delayTime(_delay), delayTime(_delay),
cancelled(false) cancelled(false)
{ {
// slog.addLevel(Debug::ANY); //slog.addLevel(Debug::ANY);
log->signal_stream_event().connect( sigc::mem_fun(this, &LogSession::logOnEvent) ); log->signal_stream_event().connect( sigc::mem_fun(this, &LogSession::logOnEvent) );
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -252,26 +252,26 @@ protected: ...@@ -252,26 +252,26 @@ protected:
#ifdef MODERN_STL_STREAMS #ifdef MODERN_STL_STREAMS
/// ///
virtual int sync() { virtual int sync() {
UniSetTypes::uniset_mutex_lock l(mut); UniSetTypes::uniset_rwmutex_wrlock 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); UniSetTypes::uniset_rwmutex_wrlock l(mut);
return sb->sputn(p, n); streamsize r = sb->sputn(p, n);
s_overflow.emit( sb->str() );
sb->str("");
return r;
} }
/// ///
virtual int_type overflow(int_type c = traits_type::eof()) { virtual int_type overflow(int_type c = traits_type::eof()) {
int_type r = sb->sputc(c); int_type r = sb->sputc(c);
if( r == '\n' ) if( r == '\n' )
{ {
UniSetTypes::uniset_mutex_lock l(mut); UniSetTypes::uniset_rwmutex_wrlock l(mut);
s_overflow.emit( sb->str() ); s_overflow.emit( sb->str() );
sb->str(""); sb->str("");
// stringbuf* old = sb;
// sb = new stringbuf();
// delete old;
} }
return r; return r;
} }
...@@ -291,16 +291,9 @@ protected: ...@@ -291,16 +291,9 @@ protected:
int_type r = sb->overflow(c); int_type r = sb->overflow(c);
if( r == '\n' ) if( r == '\n' )
{ {
UniSetTypes::uniset_rwmutex_wrlock l(mut);
s_overflow.emit( sb->str() ); s_overflow.emit( sb->str() );
if( r == '\n' ) sb->str("");
{
s_overflow.emit( sb->str() );
uniset_mutex_lock l(mut);
// из-за многопоточности.. сперва переприсвоим указатель на новый поток..
// а потом уже удалим старый..
stringbuf* old = sb;
sb = new stringbuf();
delete old;
} }
return r; return r;
} }
...@@ -309,7 +302,7 @@ private: ...@@ -309,7 +302,7 @@ private:
/// ///
StrBufOverflow_Signal s_overflow; StrBufOverflow_Signal s_overflow;
stringbuf* sb; stringbuf* sb;
UniSetTypes::uniset_mutex mut; UniSetTypes::uniset_rwmutex mut;
}; };
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
......
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