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
+49 -32
View File
@@ -35,6 +35,7 @@ using std::string;
#include "debuglog.h"
#include "pathut.h"
#include "smallut.h"
#include "ptmutex.h"
#ifndef freeZ
#define freeZ(X) {if (X) {free(X);X=0;}}
@@ -57,17 +58,6 @@ class DebugLogWriter {
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 {
char *filename;
FILE *fp;
@@ -116,7 +106,9 @@ class DLFWImpl {
public:
DLFWImpl() : filename(0), fp(0), truncate(1) {
DLFWImpl()
: filename(0), fp(0), truncate(1)
{
setfilename("stderr", 0);
}
~DLFWImpl() {
@@ -126,6 +118,7 @@ class DLFWImpl {
maybeclosefp();
filename = strdup(fn);
truncate = trnc;
maybeopenfp();
return 0;
}
const char *getfilename() {
@@ -139,30 +132,46 @@ class DLFWImpl {
}
};
DebugLogFileWriter::DebugLogFileWriter()
{
impl = new DLFWImpl;
}
class DebugLogFileWriter : public DebugLogWriter {
DLFWImpl *impl;
PTMutexInit loglock;
public:
DebugLogFileWriter()
{
impl = new DLFWImpl;
}
DebugLogFileWriter::~DebugLogFileWriter()
{
delete impl;
}
virtual ~DebugLogFileWriter()
{
delete impl;
}
int DebugLogFileWriter::setfilename(const char *fn, int trnc) {
return impl ? impl->setfilename(fn, trnc) : -1;
}
const char *DebugLogFileWriter::getfilename()
{
return impl ? impl->getfilename() : 0;
}
int DebugLogFileWriter::put(const char *s)
{
return impl ? impl->put(s) : -1;
virtual int setfilename(const char *fn, int trnc)
{
PTMutexLocker lock(loglock);
return impl ? impl->setfilename(fn, trnc) : -1;
}
virtual const char *getfilename()
{
PTMutexLocker lock(loglock);
return impl ? impl->getfilename() : 0;
}
virtual int reopen()
{
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 void initfiles()
{
@@ -300,15 +309,23 @@ void DebugLog::poplevel()
//////////////////////////////////////
static DebugLogFileWriter lwriter;
static DebugLogFileWriter *theWriter = &lwriter;
const char *getfilename()
{
return theWriter ? theWriter->getfilename() : 0;
}
int setfilename(const char *fname, int trnc)
{
return theWriter ? theWriter->setfilename(fname, trnc) : -1;
}
int reopen()
{
return theWriter ? theWriter->reopen() : -1;
}
#if DEBUGLOG_USE_THREADS
#include <pthread.h>
static pthread_key_t dbl_key;
+2
View File
@@ -66,6 +66,8 @@ class DebugLog {
extern DebugLog *getdbl();
extern const char *getfilename();
extern int setfilename(const char *fname, int trnc = 1);
extern int reopen();
#if STATICVERBOSITY >= DEBFATAL
#define LOGFATAL(X) {if (DebugLog::getdbl()->getlevel()>=DEBFATAL){DebugLog::getdbl()->prolog(DEBFATAL,__FILE__,__LINE__) ;DebugLog::getdbl()->log X;}}
#else