@TaG: [[id]] [[privacy.fr]] [[fai]] [[isp]] [[log.fr]] [[live]]
#ifndef LOGMANAGER_H
#define LOGMANAGER_H
#include
#include
#include
#include
#include
class LogManager
{
public:
class UnregisterLogThread
{
public:
UnregisterLogThread();
virtual ~UnregisterLogThread();
private:
UnregisterLogThread(const UnregisterLogThread &);
UnregisterLogThread & operator=(const UnregisterLogThread &);
};
static void clean();
static void init(const std::string & LogFilename, const int & Capacity);
static void print(const std::string & Message);
static void indent();
static void unindent();
static void removeLogThread();
static void setVersion(const std::string & Version);
private:
struct LogThread
{
int tid;
struct timeval previousTime;
std::string indentPrefix;
};
class GuardMutex
{
public:
GuardMutex(pthread_mutex_t & Mutex);
virtual ~GuardMutex();
private:
GuardMutex(const GuardMutex &);
GuardMutex & operator=(const GuardMutex &);
pthread_mutex_t & _mutex;
};
LogManager();
LogManager(const LogManager &);
static void _cleanTS();
static void _initTS(const std::string & LogFilename, const int & Capacity);
static void _printTS(std::string Message);
static void _indentTS();
static void _unindentTS();
static void _removeLogThreadTS();
static void _print2(std::string Message);
static void _setCurrentLogThread();
static void _writeHeader();
static std::ofstream _fileLog;
static unsigned int _lineNbr;
static unsigned int _capacity;
static std::string _logFilename;
static bool _initialized;
static std::stringstream _tmp;
static std::list _logThreadSet;
static struct timeval _previousTime;
static pthread_mutex_t _mutex;
static struct timeval _currentTime;
static struct timeval _deltaPreviousTime;
static struct timeval _deltaThreadPreviousTime;
static std::string _version;
};
std::ostream & operator<<(std::ostream & tmpOss, struct timeval & tv);
#define TRACE(Message) \
{ \
std::stringstream trace_sstream; \
trace_sstream << __PRETTY_FUNCTION__ << " " << __FILE__ << ":" << __LINE__ << " " << Message; \
LogManager::print(trace_sstream.str()); \
}
#endif
#include
#include
#include
#include
#include "LogManager.h"
unsigned int LogManager::_lineNbr = 0;
unsigned int LogManager::_capacity = 0;
std::ofstream LogManager::_fileLog;
bool LogManager::_initialized = false;
std::string LogManager::_logFilename = "";
std::stringstream LogManager::_tmp;
std::list LogManager::_logThreadSet;
struct timeval LogManager::_previousTime = {0,0};
pthread_mutex_t LogManager::_mutex = PTHREAD_MUTEX_INITIALIZER;
struct timeval LogManager::_currentTime = {0,0};
struct timeval LogManager::_deltaPreviousTime = {0,0};
struct timeval LogManager::_deltaThreadPreviousTime = {0,0};
std::string LogManager::_version = "version";
std::ostream & operator<<(std::ostream & tmpOss, struct timeval & tv)
{
tmpOss << tv.tv_sec << "." << std::setfill('0') << std::setw(6) << tv.tv_usec;
return(tmpOss);
}
LogManager::UnregisterLogThread::UnregisterLogThread()
{
}
LogManager::UnregisterLogThread::~UnregisterLogThread()
{
LogManager::removeLogThread();
}
LogManager::GuardMutex::GuardMutex(pthread_mutex_t & Mutex):
_mutex(Mutex)
{
pthread_mutex_lock(&_mutex);
}
LogManager::GuardMutex::~GuardMutex()
{
pthread_mutex_unlock(&_mutex);
}
void LogManager::clean()
{
GuardMutex GuardMutex(_mutex);
LogManager::_cleanTS();
}
void LogManager::init(const std::string & LogFilename, const int & Capacity)
{
GuardMutex GuardMutex(_mutex);
LogManager::_initTS(LogFilename, Capacity);
}
void LogManager::print(const std::string & Message)
{
GuardMutex GuardMutex(_mutex);
gettimeofday(&_currentTime, NULL);
_setCurrentLogThread();
timersub(&_currentTime, &_previousTime, &_deltaPreviousTime);
timersub(&_currentTime, &(_logThreadSet.begin()->previousTime), &_deltaThreadPreviousTime);
LogManager::_printTS(Message);
_previousTime = _currentTime;
_logThreadSet.begin()->previousTime = _currentTime;
}
void LogManager::indent()
{
GuardMutex GuardMutex(_mutex);
_setCurrentLogThread();
LogManager::_indentTS();
}
void LogManager::unindent()
{
GuardMutex GuardMutex(_mutex);
LogManager::_unindentTS();
}
void LogManager::removeLogThread()
{
GuardMutex GuardMutex(_mutex);
LogManager::_removeLogThreadTS();
}
void LogManager::setVersion(const std::string & Version)
{
_version = Version;
}
void LogManager::_cleanTS()
{
if (_initialized)
{
_fileLog.close();
_fileLog.clear();
unlink((_logFilename + ".bkp").c_str());
_fileLog.open(_logFilename.c_str(), std::ios_base::trunc);
_writeHeader();
}
}
void LogManager::_initTS(const std::string & LogFilename, const int & Capacity)
{
_capacity = Capacity;
if (!_initialized)
{
_fileLog.open(LogFilename.c_str(), std::ios_base::trunc);
if (_fileLog.good())
{
_writeHeader();
_logFilename = LogFilename;
unlink((_logFilename + ".bkp").c_str());
_print2(_tmp.str());
_initialized = true;
}
}
else
{
if (LogFilename != _logFilename)
{
_fileLog.close();
_fileLog.clear();
_fileLog.open(LogFilename.c_str(), std::ios_base::app);
if (_fileLog.good())
{
_fileLog.close();
_fileLog.clear();
rename(_logFilename.c_str(), LogFilename.c_str());
rename((_logFilename + ".bkp").c_str(), (LogFilename + ".bkp").c_str());
_fileLog.open(LogFilename.c_str(), std::ios_base::app);
_logFilename = LogFilename;
}
else
{
_fileLog.clear();
_fileLog.open(_logFilename.c_str(), std::ios_base::app);
}
}
}
}
void LogManager::_printTS(std::string Message)
{
std::stringstream buffer1;
buffer1 << _lineNbr++ << " " << (_logThreadSet.begin())->indentPrefix << (_logThreadSet.begin())->tid << " " << _currentTime <<
" " << _deltaThreadPreviousTime << " " << _deltaPreviousTime << " ";
size_t pos = 0;
_indentTS();
while ((pos = Message.find("\n", pos)) != std::string::npos)
{
std::stringstream buffer2;
buffer2 << _lineNbr++ << " " << (_logThreadSet.begin())->indentPrefix;
++pos;
Message.insert(pos, buffer2.str());
}
_unindentTS();
buffer1 << Message << std::endl;
if (_initialized)
{
_print2(buffer1.str());
}
else
{
_tmp << buffer1.str();
}
}
void LogManager::_indentTS()
{
(_logThreadSet.begin())->indentPrefix += std::string("\t");
}
void LogManager::_unindentTS()
{
int tid = syscall(__NR_gettid);
std::list::iterator currentLogThread;
for (currentLogThread=_logThreadSet.begin(); currentLogThread!=_logThreadSet.end(); ++currentLogThread)
{
if (currentLogThread->tid == tid)
{
currentLogThread->indentPrefix.erase(0,1);
break;
}
}
}
void LogManager::_removeLogThreadTS()
{
int tid = syscall(__NR_gettid);
std::list::iterator currentLogThread;
for (currentLogThread=_logThreadSet.begin(); currentLogThread!=_logThreadSet.end(); ++currentLogThread)
{
if (currentLogThread->tid == tid)
{
_logThreadSet.erase(currentLogThread);
break;
}
}
}
void LogManager::_print2(std::string Message)
{
if (Message.size() + _fileLog.tellp() > (_capacity/2))
{
if (Message.size() <= (_capacity/2))
{
_fileLog.close();
_fileLog.clear();
std::ifstream fileSrc(_logFilename.c_str());
std::ofstream fileDst((_logFilename + ".bkp").c_str(), std::ios_base::trunc);
fileDst << fileSrc.rdbuf();
_fileLog.open(_logFilename.c_str(), std::ios_base::trunc);
_writeHeader();
}
else
{
std::stringstream tmp;
tmp << "Message lost (too long) ! msgSz(" << Message.size() << ") capacity/2(" << _capacity/2 << ")" << std::endl;
if (tmp.str().size() <= (_capacity/2))
{
_print2(tmp.str());
}
else
{
std::cerr << tmp.str() << std::endl;
}
return;
}
}
_fileLog << Message;
_fileLog.flush();
}
void LogManager::_setCurrentLogThread()
{
if (_logThreadSet.empty())
{
_previousTime = _currentTime;
_deltaPreviousTime = _currentTime;
_deltaThreadPreviousTime = _currentTime;
}
int tid = syscall(__NR_gettid);
std::list::iterator currentLogThread;
for (currentLogThread=_logThreadSet.begin(); currentLogThread!=_logThreadSet.end(); ++currentLogThread)
{
if (currentLogThread->tid == tid)
{
_logThreadSet.push_front(*currentLogThread);
_logThreadSet.erase(currentLogThread);
break;
}
}
if (currentLogThread == _logThreadSet.end())
{
struct LogThread logThread;
logThread.tid = tid;
logThread.previousTime = _currentTime;
logThread.indentPrefix = "";
_logThreadSet.push_front(logThread);
}
}
void LogManager::_writeHeader()
{
_fileLog << _version << std::endl;
_fileLog << "log_line_number thread_id time(s.us) delta_time_with_last_msg_from_same_thread(s.us) delta_time_with_last_msg_from_any_thread(s.us) function_name src_file src_line msg" << std::endl;
_fileLog.flush();
}
#ifndef LOGSCOPE_H
#define LOGSCOPE_H
#include
#include
class LogScope
{
public:
LogScope(std::string Message);
~LogScope();
};
#define TRACE_SCOPE(Message) \
std::stringstream trace_sstream; \
trace_sstream << __PRETTY_FUNCTION__ << " " << __FILE__ << " " << __LINE__ << " " << Message; \
LogScope logScope(trace_sstream.str());
#endif
#include "LogScope.h"
#include "LogManager.h"
LogScope::LogScope(std::string Message)
{
LogManager::print(Message);
LogManager::indent();
}
LogScope::~LogScope()
{
LogManager::unindent();
}
#!/bin/bash
color_set=(
'\E[31m'
'\E[34m'
)
tmp_file="$(mktemp -t ${0##*/}.XXXXXXXXXX)"
cat "${@}" > "${tmp_file}"
head -2 "${tmp_file}"
sed -i '1,2d' "${tmp_file}"
tid_set=($(awk '{print $2}' "${tmp_file}"))
idx1=0
idx2=1
tid1=-1
while read ligne
do
if [ "${tid_set[idx1]}" -ne 0 2>/dev/null ] ||
[ "${tid_set[idx1]}" -eq 0 2>/dev/null ] &&
[ "${tid1}" != "${tid_set[idx1]}" ]
then
((idx2=++idx2%2))
echo -en "${color_set[idx2]}"
tid1="${tid_set[idx1]}"
fi
echo "${ligne}"
((++idx1))
done < "${tmp_file}"
echo -e '\E[0m'
rm -f "${tmp_file}"
Application v1.2 2009/04/??
log_line_number thread_id time(s.us) delta_time_with_last_msg_from_same_thread(s.us) delta_time_with_last_msg_from_any_thread(s.us) function_name src_file src_line msg
0 12757 1239022156.769629 0.000000 0.000000 int main(int, char**) src/main.cpp 154
1 12757 1239022156.915337 0.145708 0.145708 static const QString& ConfigBox::getApplicationHome() src/configbox.cpp 17
2 12757 1239022156.916039 0.000702 0.000702 int main(int, char**) src/main.cpp 176 License status :
3 Evaluation, expires on 26.04.2009.
4 12757 1239022156.923098 0.007059 0.007059 ConfigBox::ConfigBox() src/configbox.cpp 26
5 12757 1239022157.162456 0.239358 0.239358 void ConfigBox::readSettings() src/configbox.cpp 257
6 12757 1239022157.166141 0.003685 0.003685 void ConfigBox::writeSettings() src/configbox.cpp 239
7 12757 1239022157.166321 0.000180 0.000180 soapStatus::soapStatus() src/status_thr.cpp 10
8 12757 1239022157.166358 0.000037 0.000037 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
9 12757 1239022157.166360 0.000002 0.000002 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
10 12757 1239022157.166423 0.000063 0.000063 projectManager::projectManager() src/projectManager.cpp 12
11 12757 1239022157.166459 0.000036 0.000036 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
12 12757 1239022157.166810 0.000351 0.000351 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
13 12757 1239022157.166862 0.000052 0.000052 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
14 12757 1239022157.166904 0.000042 0.000042 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
15 12757 1239022157.166945 0.000041 0.000041 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
16 12757 1239022157.167029 0.000084 0.000084 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
17 12757 1239022157.167089 0.000060 0.000060 k7encoder::k7encoder() src/k7encoder.cpp 21
18 12757 1239022157.211220 0.044131 0.044131 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
19 12757 1239022157.211292 0.000072 0.000072 static const QSettings& ConfigBox::getSettings() src/configbox.cpp 11
20 12757 1239022157.212910 0.001618 0.001618 void k7encoder::createActions() src/k7encoder.cpp 69
21 12758 1239022157.219646 0.000000 0.006736 virtual void soapStatus::run() src/status_thr.cpp 24
22 12757 1239022157.249270 0.036360 0.029624 void k7encoder::setStatus(QString) src/k7encoder.cpp 115
23 12758 1239022159.222108 2.002462 1.972838 void soapStatus::getStatus(QString&) src/status_thr.cpp 50
==== misc ====
* http://ernstfamily.ch/jonathan/2009/03/dechiffrer-son-dossier-personnel-lors-de-lidentification-avec-pam-mount/# [[pam]]
* http://www.debian-administration.org/articles/492# FreezE LenovO
* https://github.com/jerome-pouiller/reredirect# SysCall
==== 20090105 : L'alerte orange ====
Je vous invite a changer vos mots de passe sur le site d orange / wanadoo car il y a une faille actuellement :
* http://www.zataz.com/news/18324/orange--faille--dossier--clients--suivi-de-commandes.html
Peut etre l'occasion de tester un autre [[FAI]] ? ie http://fdn.fr
{{http://nion.modprobe.de/bimg/101101.jpg}}