reopen log file on SIGHUP

This commit is contained in:
Jean-Francois Dockes 2013-03-22 11:19:33 +01:00
parent df49598a8d
commit 5245f1f9d4
4 changed files with 87 additions and 35 deletions

View File

@ -33,8 +33,15 @@
#include "smallut.h" #include "smallut.h"
#include "execmd.h" #include "execmd.h"
static const int catchedSigs[] = {SIGHUP, SIGINT, SIGQUIT, SIGTERM, static const int catchedSigs[] = {SIGINT, SIGQUIT, SIGTERM, SIGUSR1, SIGUSR2};
SIGUSR1, SIGUSR2};
static pthread_t mainthread_id;
static void siglogreopen(int)
{
if (recoll_ismainthread())
DebugLog::reopen();
}
RclConfig *recollinit(RclInitFlags flags, RclConfig *recollinit(RclInitFlags flags,
void (*cleanup)(void), void (*sigcleanup)(int), void (*cleanup)(void), void (*sigcleanup)(int),
@ -54,7 +61,7 @@ RclConfig *recollinit(RclInitFlags flags,
// We would like to block SIGCHLD globally, but we can't because // We would like to block SIGCHLD globally, but we can't because
// QT uses it. Have to block it inside execmd.cpp // QT uses it. Have to block it inside execmd.cpp
// Install signal handler // Install app signal handler
if (sigcleanup) { if (sigcleanup) {
struct sigaction action; struct sigaction action;
action.sa_handler = sigcleanup; action.sa_handler = sigcleanup;
@ -105,11 +112,26 @@ RclConfig *recollinit(RclInitFlags flags,
int lev = atoi(loglevel.c_str()); int lev = atoi(loglevel.c_str());
DebugLog::getdbl()->setloglevel(lev); DebugLog::getdbl()->setloglevel(lev);
} }
// Install log rotate sig handler
{
struct sigaction action;
action.sa_handler = siglogreopen;
action.sa_flags = 0;
sigemptyset(&action.sa_mask);
if (signal(SIGHUP, SIG_IGN) != SIG_IGN) {
if (sigaction(SIGHUP, &action, 0) < 0) {
perror("Sigaction failed");
}
}
}
// Make sure the locale charset is initialized (so that multiple // Make sure the locale charset is initialized (so that multiple
// threads don't try to do it at once). // threads don't try to do it at once).
config->getDefCharset(); config->getDefCharset();
mainthread_id = pthread_self();
// Init unac locking // Init unac locking
unac_init_mt(); unac_init_mt();
// Init smallut and pathut static values // Init smallut and pathut static values
@ -160,5 +182,13 @@ void recoll_threadinit()
for (unsigned int i = 0; i < sizeof(catchedSigs) / sizeof(int); i++) for (unsigned int i = 0; i < sizeof(catchedSigs) / sizeof(int); i++)
sigaddset(&sset, catchedSigs[i]); sigaddset(&sset, catchedSigs[i]);
sigaddset(&sset, SIGHUP);
pthread_sigmask(SIG_BLOCK, &sset, 0); pthread_sigmask(SIG_BLOCK, &sset, 0);
} }
bool recoll_ismainthread()
{
return pthread_equal(pthread_self(), mainthread_id);
}

View File

@ -54,4 +54,7 @@ inline RclConfig *recollinit(void (*cleanup)(void), void (*sigcleanup)(int),
// The main thread handles all signals. // The main thread handles all signals.
extern void recoll_threadinit(); extern void recoll_threadinit();
// Check if main thread
extern bool recoll_ismainthread();
#endif /* _RCLINIT_H_INCLUDED_ */ #endif /* _RCLINIT_H_INCLUDED_ */

View File

@ -35,6 +35,7 @@ using std::string;
#include "debuglog.h" #include "debuglog.h"
#include "pathut.h" #include "pathut.h"
#include "smallut.h" #include "smallut.h"
#include "ptmutex.h"
#ifndef freeZ #ifndef freeZ
#define freeZ(X) {if (X) {free(X);X=0;}} #define freeZ(X) {if (X) {free(X);X=0;}}
@ -57,17 +58,6 @@ class DebugLogWriter {
virtual int put(const char *s) = 0; virtual int put(const char *s) = 0;
}; };
class DLFWImpl;
class DebugLogFileWriter : public DebugLogWriter {
DLFWImpl *impl;
public:
DebugLogFileWriter();
~DebugLogFileWriter();
virtual const char *getfilename();
virtual int setfilename(const char *fname, int trnc = 1);
virtual int put(const char *s);
};
class DLFWImpl { class DLFWImpl {
char *filename; char *filename;
FILE *fp; FILE *fp;
@ -116,7 +106,9 @@ class DLFWImpl {
public: public:
DLFWImpl() : filename(0), fp(0), truncate(1) { DLFWImpl()
: filename(0), fp(0), truncate(1)
{
setfilename("stderr", 0); setfilename("stderr", 0);
} }
~DLFWImpl() { ~DLFWImpl() {
@ -126,6 +118,7 @@ class DLFWImpl {
maybeclosefp(); maybeclosefp();
filename = strdup(fn); filename = strdup(fn);
truncate = trnc; truncate = trnc;
maybeopenfp();
return 0; return 0;
} }
const char *getfilename() { const char *getfilename() {
@ -139,30 +132,46 @@ class DLFWImpl {
} }
}; };
DebugLogFileWriter::DebugLogFileWriter() class DebugLogFileWriter : public DebugLogWriter {
{ DLFWImpl *impl;
impl = new DLFWImpl; PTMutexInit loglock;
} public:
DebugLogFileWriter()
{
impl = new DLFWImpl;
}
DebugLogFileWriter::~DebugLogFileWriter() virtual ~DebugLogFileWriter()
{ {
delete impl; delete impl;
} }
int DebugLogFileWriter::setfilename(const char *fn, int trnc) { virtual int setfilename(const char *fn, int trnc)
return impl ? impl->setfilename(fn, trnc) : -1; {
} PTMutexLocker lock(loglock);
return impl ? impl->setfilename(fn, trnc) : -1;
const char *DebugLogFileWriter::getfilename() }
{ virtual const char *getfilename()
return impl ? impl->getfilename() : 0; {
} PTMutexLocker lock(loglock);
return impl ? impl->getfilename() : 0;
int DebugLogFileWriter::put(const char *s) }
{ virtual int reopen()
return impl ? impl->put(s) : -1; {
PTMutexLocker lock(loglock);
if (!impl)
return -1;
string fn = impl->getfilename();
return impl->setfilename(fn.c_str(), 1);
}
virtual int put(const char *s)
{
PTMutexLocker lock(loglock);
return impl ? impl->put(s) : -1;
};
}; };
static set<string> yesfiles; static set<string> yesfiles;
static void initfiles() static void initfiles()
{ {
@ -300,15 +309,23 @@ void DebugLog::poplevel()
////////////////////////////////////// //////////////////////////////////////
static DebugLogFileWriter lwriter; static DebugLogFileWriter lwriter;
static DebugLogFileWriter *theWriter = &lwriter; static DebugLogFileWriter *theWriter = &lwriter;
const char *getfilename() const char *getfilename()
{ {
return theWriter ? theWriter->getfilename() : 0; return theWriter ? theWriter->getfilename() : 0;
} }
int setfilename(const char *fname, int trnc) int setfilename(const char *fname, int trnc)
{ {
return theWriter ? theWriter->setfilename(fname, trnc) : -1; return theWriter ? theWriter->setfilename(fname, trnc) : -1;
} }
int reopen()
{
return theWriter ? theWriter->reopen() : -1;
}
#if DEBUGLOG_USE_THREADS #if DEBUGLOG_USE_THREADS
#include <pthread.h> #include <pthread.h>
static pthread_key_t dbl_key; static pthread_key_t dbl_key;

View File

@ -66,6 +66,8 @@ class DebugLog {
extern DebugLog *getdbl(); extern DebugLog *getdbl();
extern const char *getfilename(); extern const char *getfilename();
extern int setfilename(const char *fname, int trnc = 1); extern int setfilename(const char *fname, int trnc = 1);
extern int reopen();
#if STATICVERBOSITY >= DEBFATAL #if STATICVERBOSITY >= DEBFATAL
#define LOGFATAL(X) {if (DebugLog::getdbl()->getlevel()>=DEBFATAL){DebugLog::getdbl()->prolog(DEBFATAL,__FILE__,__LINE__) ;DebugLog::getdbl()->log X;}} #define LOGFATAL(X) {if (DebugLog::getdbl()->getlevel()>=DEBFATAL){DebugLog::getdbl()->prolog(DEBFATAL,__FILE__,__LINE__) ;DebugLog::getdbl()->log X;}}
#else #else