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
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;
}
}
......
......@@ -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;
}
}
......
......@@ -2562,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;
}
}
......
......@@ -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;
}
}
......
......@@ -72,8 +72,8 @@ int main( int argc, const char **argv )
string logfilename = conf->getArgParam("--logfile", "smemory-plus.log");
string logname( conf->getLogDir() + logfilename );
// UniSetExtensions::dlog.logFile( logname );
// ulog.logFile( logname );
UniSetExtensions::dlog.logFile( logname );
ulog.logFile( logname );
conf->initDebug(UniSetExtensions::dlog,"dlog");
UniSetActivator* act = UniSetActivator::Instance();
......
......@@ -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;
}
}
......
......@@ -89,7 +89,7 @@ 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 );
///
virtual ~DebugStream();
......@@ -118,7 +118,7 @@ public:
}
/// 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; }
......
......@@ -25,13 +25,24 @@ class LogAgregator:
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<DebugStream*> LogList;
typedef std::list<LogInfo> LogList;
LogList llst;
};
// -------------------------------------------------------------------------
......
......@@ -10,6 +10,40 @@
#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:
......
......@@ -9,6 +9,7 @@
#include "DebugStream.h"
#include "PassiveTimer.h"
// -------------------------------------------------------------------------
/*! Реализация "сессии" для клиентов LogServer. */
class LogSession:
public ost::TCPSession
{
......
......@@ -55,7 +55,7 @@ DebugStream::DebugStream(Debug::type 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())),
dt(t), nullstream(new nullbuf),
internal(new debugstream_internal),
......@@ -63,7 +63,10 @@ DebugStream::DebugStream(char const * f, Debug::type t)
fname(""),
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(),
&internal->fbuf,&internal->sbuf));
......@@ -99,7 +102,7 @@ const DebugStream& DebugStream::operator=( const DebugStream& r )
}
//--------------------------------------------------------------------------
/// Sets the debugstreams' logfile to f.
void DebugStream::logFile( const std::string& f )
void DebugStream::logFile( const std::string& f, bool truncate )
{
fname = f;
if( internal ) {
......@@ -110,7 +113,10 @@ void DebugStream::logFile( const std::string& f )
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(),
&internal->fbuf,&internal->sbuf));
}
......
......@@ -16,7 +16,7 @@ LogAgregator::LogAgregator( Debug::type t ):
void LogAgregator::logFile( const std::string& f )
{
DebugStream::logFile(f);
if( f.empty() )
if( !f.empty() )
delete rdbuf(new teebuf(&internal->fbuf,&internal->sbuf));
else
delete rdbuf(new teebuf(&internal->nbuf,&internal->sbuf));
......@@ -36,7 +36,7 @@ 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) )
if( &l == i->log )
return;
}
......@@ -46,10 +46,10 @@ void LogAgregator::add( DebugStream& l )
void LogAgregator::addLevel( const std::string& logname, Debug::type t )
{
for( auto& i: llst )
{
if( i->getLogName() == logname )
{
if( i.log->getLogName() == logname )
{
i->addLevel(t);
i.log->addLevel(t);
break;
}
}
......@@ -58,10 +58,10 @@ void LogAgregator::addLevel( const std::string& logname, Debug::type t )
void LogAgregator::delLevel( const std::string& logname, Debug::type t )
{
for( auto& i: llst )
{
if( i->getLogName() == logname )
{
if( i.log->getLogName() == logname )
{
i->delLevel(t);
i.log->delLevel(t);
break;
}
}
......@@ -70,10 +70,10 @@ void LogAgregator::delLevel( const std::string& logname, Debug::type t )
void LogAgregator::level( const std::string& logname, Debug::type t )
{
for( auto& i: llst )
{
if( i->getLogName() == logname )
{
if( i.log->getLogName() == logname )
{
i->level(t);
i.log->level(t);
break;
}
}
......@@ -85,11 +85,25 @@ DebugStream* LogAgregator::getLog( const std::string& logname )
return 0;
for( auto& i: llst )
{
if( i->getLogName() == logname )
return i;
{
if( i.log->getLogName() == logname )
return i.log;
}
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()
ptSessionTimeout.setTiming(sessTimeout);
string oldLogFile( log->getLogFile() );
setKeepAlive(true);
// setTimeout(sessTimeout);
......@@ -87,12 +85,38 @@ void LogSession::run()
slog.info() << peername << "(run): receive command: '" << msg.cmd << "'" << endl;
string cmdLogName(msg.logname);
DebugStream* cmdlog = 0;
DebugStream* cmdlog = log;
string logfile(log->getLogFile());
if( !cmdLogName.empty () )
{
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
......@@ -106,35 +130,31 @@ void LogSession::run()
cmdlog->level( (Debug::type)msg.data );
break;
case LogServerTypes::cmdAddLevel:
cmdlog->addLevel((Debug::type)msg.data );
cmdlog->addLevel( (Debug::type)msg.data );
break;
case LogServerTypes::cmdDelLevel:
cmdlog->delLevel( (Debug::type)msg.data );
break;
case LogServerTypes::cmdRotate:
{
string lfile( cmdlog->getLogFile() );
if( !lfile.empty() )
cmdlog->logFile(lfile);
}
if( !logfile.empty() )
cmdlog->logFile(logfile,true);
break;
case LogServerTypes::cmdOffLogFile:
{
string lfile( cmdlog->getLogFile() );
if( !lfile.empty() )
if( !logfile.empty() )
cmdlog->logFile("");
}
break;
case LogServerTypes::cmdOnLogFile:
{
if( !oldLogFile.empty() && oldLogFile != cmdlog->getLogFile() )
cmdlog->logFile(oldLogFile);
if( !logfile.empty() )
cmdlog->logFile(logfile);
}
break;
default:
slog.warn() << peername << "(run): Unknown command '" << msg.cmd << "'" << endl;
break;
......
......@@ -409,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;
}
}
......
......@@ -63,5 +63,15 @@ int main( int argc, const char **argv )
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;
}
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