Commit 0217b4b1 authored by Pavel Vainerman's avatar Pavel Vainerman

(Log): отладил обработку команд отключения и включения записи файла логов.

(Log): исправил ошибку в работе команды LogRotate для DebugStream. (Log): написал небольшие комментарии
parent a20dcb5f
...@@ -319,7 +319,7 @@ void <xsl:value-of select="$CLASSNAME"/>_SK::sysCommand( const SystemMessage* _s ...@@ -319,7 +319,7 @@ void <xsl:value-of select="$CLASSNAME"/>_SK::sysCommand( const SystemMessage* _s
string fname( mylog.getLogFile() ); string fname( mylog.getLogFile() );
if( !fname.empty() ) 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; mylog &lt;&lt; myname &lt;&lt; "(sysCommand): ***************** mylog LOG ROTATE *****************" &lt;&lt; endl;
} }
} }
......
...@@ -1226,7 +1226,7 @@ void IOControl::sysCommand( const SystemMessage* sm ) ...@@ -1226,7 +1226,7 @@ void IOControl::sysCommand( const SystemMessage* sm )
string fname( ulog.getLogFile() ); string fname( ulog.getLogFile() );
if( !fname.empty() ) if( !fname.empty() )
{ {
ulog.logFile(fname); ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << endl; ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << endl;
} }
...@@ -1234,7 +1234,7 @@ void IOControl::sysCommand( const SystemMessage* sm ) ...@@ -1234,7 +1234,7 @@ void IOControl::sysCommand( const SystemMessage* sm )
fname = dlog.getLogFile(); fname = dlog.getLogFile();
if( !fname.empty() ) if( !fname.empty() )
{ {
dlog.logFile(fname); dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** GGDEB LOG ROTATE *****************" << endl; dlog << myname << "(sysCommand): ***************** GGDEB LOG ROTATE *****************" << endl;
} }
} }
......
...@@ -148,7 +148,7 @@ void PassiveLProcessor::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -148,7 +148,7 @@ void PassiveLProcessor::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname (ulog.getLogFile() ); string fname (ulog.getLogFile() );
if( !fname.empty() ) if( !fname.empty() )
{ {
ulog.logFile(fname); ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl; ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
} }
...@@ -156,7 +156,7 @@ void PassiveLProcessor::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -156,7 +156,7 @@ void PassiveLProcessor::sysCommand( const UniSetTypes::SystemMessage *sm )
fname = dlog.getLogFile(); fname = dlog.getLogFile();
if( !fname.empty() ) if( !fname.empty() )
{ {
dlog.logFile(fname); dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl; dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
} }
} }
......
...@@ -2562,14 +2562,14 @@ void MBExchange::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -2562,14 +2562,14 @@ void MBExchange::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname(ulog.getLogFile()); string fname(ulog.getLogFile());
if( !fname.empty() ) if( !fname.empty() )
{ {
ulog.logFile(fname); ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl; ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
} }
dlog << myname << "(sysCommand): logRotate" << std::endl; dlog << myname << "(sysCommand): logRotate" << std::endl;
fname = dlog.getLogFile(); fname = dlog.getLogFile();
if( !fname.empty() ) if( !fname.empty() )
{ {
dlog.logFile(fname); dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl; dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
} }
} }
......
...@@ -623,7 +623,7 @@ void MBSlave::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -623,7 +623,7 @@ void MBSlave::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname(ulog.getLogFile()); string fname(ulog.getLogFile());
if( !fname.empty() ) if( !fname.empty() )
{ {
ulog.logFile(fname); ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl; ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
} }
...@@ -631,7 +631,7 @@ void MBSlave::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -631,7 +631,7 @@ void MBSlave::sysCommand( const UniSetTypes::SystemMessage *sm )
fname = dlog.getLogFile(); fname = dlog.getLogFile();
if( !fname.empty() ) if( !fname.empty() )
{ {
dlog.logFile(fname); dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl; dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
} }
} }
......
...@@ -72,8 +72,8 @@ int main( int argc, const char **argv ) ...@@ -72,8 +72,8 @@ int main( int argc, const char **argv )
string logfilename = conf->getArgParam("--logfile", "smemory-plus.log"); string logfilename = conf->getArgParam("--logfile", "smemory-plus.log");
string logname( conf->getLogDir() + logfilename ); string logname( conf->getLogDir() + logfilename );
// UniSetExtensions::dlog.logFile( logname ); UniSetExtensions::dlog.logFile( logname );
// ulog.logFile( logname ); ulog.logFile( logname );
conf->initDebug(UniSetExtensions::dlog,"dlog"); conf->initDebug(UniSetExtensions::dlog,"dlog");
UniSetActivator* act = UniSetActivator::Instance(); UniSetActivator* act = UniSetActivator::Instance();
......
...@@ -523,7 +523,7 @@ void UNetExchange::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -523,7 +523,7 @@ void UNetExchange::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname(ulog.getLogFile()); string fname(ulog.getLogFile());
if( !fname.empty() ) if( !fname.empty() )
{ {
ulog.logFile(fname); ulog.logFile(fname,true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl; ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << std::endl;
} }
...@@ -531,7 +531,7 @@ void UNetExchange::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -531,7 +531,7 @@ void UNetExchange::sysCommand( const UniSetTypes::SystemMessage *sm )
fname = dlog.getLogFile(); fname = dlog.getLogFile();
if( !fname.empty() ) if( !fname.empty() )
{ {
dlog.logFile(fname); dlog.logFile(fname,true);
dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl; dlog << myname << "(sysCommand): ***************** dlog LOG ROTATE *****************" << std::endl;
} }
} }
......
...@@ -273,7 +273,7 @@ void UObject_SK::sysCommand( const SystemMessage* _sm ) ...@@ -273,7 +273,7 @@ void UObject_SK::sysCommand( const SystemMessage* _sm )
string fname( mylog.getLogFile() ); string fname( mylog.getLogFile() );
if( !fname.empty() ) if( !fname.empty() )
{ {
mylog.logFile(fname.c_str()); mylog.logFile(fname.c_str(),true);
mylog << myname << "(sysCommand): ***************** mylog LOG ROTATE *****************" << endl; mylog << myname << "(sysCommand): ***************** mylog LOG ROTATE *****************" << endl;
} }
} }
......
...@@ -89,7 +89,7 @@ public: ...@@ -89,7 +89,7 @@ public:
/// Constructor, sets the log file to f, and the debug level to t. /// Constructor, sets the log file to f, and the debug level to t.
explicit explicit
DebugStream(char const * f, Debug::type t = Debug::NONE); DebugStream(char const * f, Debug::type t = Debug::NONE, bool truncate=false );
/// ///
virtual ~DebugStream(); virtual ~DebugStream();
...@@ -118,7 +118,7 @@ public: ...@@ -118,7 +118,7 @@ public:
} }
/// Sets the debugstreams' logfile to f. /// Sets the debugstreams' logfile to f.
virtual void logFile( const std::string& f ); virtual void logFile( const std::string& f, bool truncate=false );
inline std::string getLogFile(){ return fname; } inline std::string getLogFile(){ return fname; }
......
...@@ -25,13 +25,24 @@ class LogAgregator: ...@@ -25,13 +25,24 @@ class LogAgregator:
void delLevel( 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 ); 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 ); DebugStream* getLog( const std::string& logname );
LogInfo getLogInfo( const std::string& logname );
protected: protected:
void logOnEvent( const std::string& s ); void logOnEvent( const std::string& s );
private: private:
typedef std::list<DebugStream*> LogList; typedef std::list<LogInfo> LogList;
LogList llst; LogList llst;
}; };
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
......
...@@ -10,6 +10,40 @@ ...@@ -10,6 +10,40 @@
#include "ThreadCreator.h" #include "ThreadCreator.h"
class LogSession; 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 class LogServer
{ {
public: public:
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "DebugStream.h" #include "DebugStream.h"
#include "PassiveTimer.h" #include "PassiveTimer.h"
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
/*! Реализация "сессии" для клиентов LogServer. */
class LogSession: class LogSession:
public ost::TCPSession public ost::TCPSession
{ {
......
...@@ -55,7 +55,7 @@ DebugStream::DebugStream(Debug::type t) ...@@ -55,7 +55,7 @@ DebugStream::DebugStream(Debug::type t)
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/// Constructor, sets the log file to f, and the debug level to t. /// Constructor, sets the log file to f, and the debug level to t.
DebugStream::DebugStream(char const * f, Debug::type t) DebugStream::DebugStream(char const * f, Debug::type t, bool truncate )
: ostream(new debugbuf(cerr.rdbuf())), : ostream(new debugbuf(cerr.rdbuf())),
dt(t), nullstream(new nullbuf), dt(t), nullstream(new nullbuf),
internal(new debugstream_internal), internal(new debugstream_internal),
...@@ -63,7 +63,10 @@ DebugStream::DebugStream(char const * f, Debug::type t) ...@@ -63,7 +63,10 @@ DebugStream::DebugStream(char const * f, Debug::type t)
fname(""), fname(""),
logname("") logname("")
{ {
internal->fbuf.open(f, ios::out|ios::app); std::ios_base::openmode mode = ios::out;
mode |= truncate ? ios::trunc : ios::app;
internal->fbuf.open(f, mode);
delete rdbuf(new threebuf(cerr.rdbuf(), delete rdbuf(new threebuf(cerr.rdbuf(),
&internal->fbuf,&internal->sbuf)); &internal->fbuf,&internal->sbuf));
...@@ -99,7 +102,7 @@ const DebugStream& DebugStream::operator=( const DebugStream& r ) ...@@ -99,7 +102,7 @@ const DebugStream& DebugStream::operator=( const DebugStream& r )
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/// Sets the debugstreams' logfile to f. /// Sets the debugstreams' logfile to f.
void DebugStream::logFile( const std::string& f ) void DebugStream::logFile( const std::string& f, bool truncate )
{ {
fname = f; fname = f;
if( internal ) { if( internal ) {
...@@ -110,7 +113,10 @@ void DebugStream::logFile( const std::string& f ) ...@@ -110,7 +113,10 @@ void DebugStream::logFile( const std::string& f )
if( !f.empty() ) if( !f.empty() )
{ {
internal->fbuf.open(f.c_str(), ios::out|ios::app); std::ios_base::openmode mode = ios::out;
mode |= truncate ? ios::trunc : ios::app;
internal->fbuf.open(f.c_str(), mode);
delete rdbuf(new threebuf(cerr.rdbuf(), delete rdbuf(new threebuf(cerr.rdbuf(),
&internal->fbuf,&internal->sbuf)); &internal->fbuf,&internal->sbuf));
} }
......
...@@ -16,7 +16,7 @@ LogAgregator::LogAgregator( Debug::type t ): ...@@ -16,7 +16,7 @@ LogAgregator::LogAgregator( Debug::type t ):
void LogAgregator::logFile( const std::string& f ) void LogAgregator::logFile( const std::string& f )
{ {
DebugStream::logFile(f); DebugStream::logFile(f);
if( f.empty() ) if( !f.empty() )
delete rdbuf(new teebuf(&internal->fbuf,&internal->sbuf)); delete rdbuf(new teebuf(&internal->fbuf,&internal->sbuf));
else else
delete rdbuf(new teebuf(&internal->nbuf,&internal->sbuf)); delete rdbuf(new teebuf(&internal->nbuf,&internal->sbuf));
...@@ -36,7 +36,7 @@ void LogAgregator::add( DebugStream& l ) ...@@ -36,7 +36,7 @@ void LogAgregator::add( DebugStream& l )
l.signal_stream_event().connect( sigc::mem_fun(this, &LogAgregator::logOnEvent) ); l.signal_stream_event().connect( sigc::mem_fun(this, &LogAgregator::logOnEvent) );
for( LogList::iterator i=llst.begin(); i!=llst.end(); i++ ) for( LogList::iterator i=llst.begin(); i!=llst.end(); i++ )
{ {
if( &l == (*i) ) if( &l == i->log )
return; return;
} }
...@@ -46,10 +46,10 @@ void LogAgregator::add( DebugStream& l ) ...@@ -46,10 +46,10 @@ void LogAgregator::add( DebugStream& l )
void LogAgregator::addLevel( const std::string& logname, Debug::type t ) void LogAgregator::addLevel( const std::string& logname, Debug::type t )
{ {
for( auto& i: llst ) for( auto& i: llst )
{ {
if( i->getLogName() == logname ) if( i.log->getLogName() == logname )
{ {
i->addLevel(t); i.log->addLevel(t);
break; break;
} }
} }
...@@ -58,10 +58,10 @@ void LogAgregator::addLevel( const std::string& logname, Debug::type t ) ...@@ -58,10 +58,10 @@ void LogAgregator::addLevel( const std::string& logname, Debug::type t )
void LogAgregator::delLevel( const std::string& logname, Debug::type t ) void LogAgregator::delLevel( const std::string& logname, Debug::type t )
{ {
for( auto& i: llst ) for( auto& i: llst )
{ {
if( i->getLogName() == logname ) if( i.log->getLogName() == logname )
{ {
i->delLevel(t); i.log->delLevel(t);
break; break;
} }
} }
...@@ -70,10 +70,10 @@ void LogAgregator::delLevel( const std::string& logname, Debug::type t ) ...@@ -70,10 +70,10 @@ void LogAgregator::delLevel( const std::string& logname, Debug::type t )
void LogAgregator::level( const std::string& logname, Debug::type t ) void LogAgregator::level( const std::string& logname, Debug::type t )
{ {
for( auto& i: llst ) for( auto& i: llst )
{ {
if( i->getLogName() == logname ) if( i.log->getLogName() == logname )
{ {
i->level(t); i.log->level(t);
break; break;
} }
} }
...@@ -85,11 +85,25 @@ DebugStream* LogAgregator::getLog( const std::string& logname ) ...@@ -85,11 +85,25 @@ DebugStream* LogAgregator::getLog( const std::string& logname )
return 0; return 0;
for( auto& i: llst ) for( auto& i: llst )
{ {
if( i->getLogName() == logname ) if( i.log->getLogName() == logname )
return i; return i.log;
} }
return 0; return 0;
} }
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
\ No newline at end of file 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();
}
// -------------------------------------------------------------------------
...@@ -66,8 +66,6 @@ void LogSession::run() ...@@ -66,8 +66,6 @@ void LogSession::run()
ptSessionTimeout.setTiming(sessTimeout); ptSessionTimeout.setTiming(sessTimeout);
string oldLogFile( log->getLogFile() );
setKeepAlive(true); setKeepAlive(true);
// setTimeout(sessTimeout); // setTimeout(sessTimeout);
...@@ -87,12 +85,38 @@ void LogSession::run() ...@@ -87,12 +85,38 @@ void LogSession::run()
slog.info() << peername << "(run): receive command: '" << msg.cmd << "'" << endl; slog.info() << peername << "(run): receive command: '" << msg.cmd << "'" << endl;
string cmdLogName(msg.logname); string cmdLogName(msg.logname);
DebugStream* cmdlog = 0; DebugStream* cmdlog = log;
string logfile(log->getLogFile());
if( !cmdLogName.empty () ) if( !cmdLogName.empty () )
{ {
LogAgregator* lag = dynamic_cast<LogAgregator*>(log); LogAgregator* lag = dynamic_cast<LogAgregator*>(log);
cmdlog = lag ? lag->getLog( cmdLogName ) : 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 // обрабатываем команды только если нашли log
...@@ -106,35 +130,31 @@ void LogSession::run() ...@@ -106,35 +130,31 @@ void LogSession::run()
cmdlog->level( (Debug::type)msg.data ); cmdlog->level( (Debug::type)msg.data );
break; break;
case LogServerTypes::cmdAddLevel: case LogServerTypes::cmdAddLevel:
cmdlog->addLevel((Debug::type)msg.data ); cmdlog->addLevel( (Debug::type)msg.data );
break; break;
case LogServerTypes::cmdDelLevel: case LogServerTypes::cmdDelLevel:
cmdlog->delLevel( (Debug::type)msg.data ); cmdlog->delLevel( (Debug::type)msg.data );
break; break;
case LogServerTypes::cmdRotate: case LogServerTypes::cmdRotate:
{ if( !logfile.empty() )
string lfile( cmdlog->getLogFile() ); cmdlog->logFile(logfile,true);
if( !lfile.empty() )
cmdlog->logFile(lfile);
}
break; break;
case LogServerTypes::cmdOffLogFile: case LogServerTypes::cmdOffLogFile:
{ {
string lfile( cmdlog->getLogFile() ); if( !logfile.empty() )
if( !lfile.empty() )
cmdlog->logFile(""); cmdlog->logFile("");
} }
break; break;
case LogServerTypes::cmdOnLogFile: case LogServerTypes::cmdOnLogFile:
{ {
if( !oldLogFile.empty() && oldLogFile != cmdlog->getLogFile() ) if( !logfile.empty() )
cmdlog->logFile(oldLogFile); cmdlog->logFile(logfile);
} }
break; break;
default: default:
slog.warn() << peername << "(run): Unknown command '" << msg.cmd << "'" << endl; slog.warn() << peername << "(run): Unknown command '" << msg.cmd << "'" << endl;
break; break;
......
...@@ -409,7 +409,7 @@ void UniSetActivator::sysCommand( const UniSetTypes::SystemMessage *sm ) ...@@ -409,7 +409,7 @@ void UniSetActivator::sysCommand( const UniSetTypes::SystemMessage *sm )
string fname = ulog.getLogFile(); string fname = ulog.getLogFile();
if( !fname.empty() ) if( !fname.empty() )
{ {
ulog.logFile(fname.c_str()); ulog.logFile(fname.c_str(),true);
ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << endl; ulog << myname << "(sysCommand): ***************** ulog LOG ROTATE *****************" << endl;
} }
} }
......
...@@ -63,5 +63,15 @@ int main( int argc, const char **argv ) ...@@ -63,5 +63,15 @@ int main( int argc, const char **argv )
if( l != &log1 ) if( l != &log1 )
cout << "**** TEST FAILED: LogAgregator::getLog() " << endl; 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; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment