Switch to unified view

a/src/utils/debuglog.cpp b/src/utils/debuglog.cpp
...
...
33
using std::string;
33
using std::string;
34
34
35
#include "debuglog.h"
35
#include "debuglog.h"
36
#include "pathut.h"
36
#include "pathut.h"
37
#include "smallut.h"
37
#include "smallut.h"
38
#include "ptmutex.h"
38
39
39
#ifndef freeZ 
40
#ifndef freeZ 
40
#define freeZ(X) {if (X) {free(X);X=0;}}
41
#define freeZ(X) {if (X) {free(X);X=0;}}
41
#endif
42
#endif
42
43
...
...
53
54
54
class DebugLogWriter {
55
class DebugLogWriter {
55
  public:
56
  public:
56
    virtual ~DebugLogWriter() {}
57
    virtual ~DebugLogWriter() {}
57
    virtual int put(const char *s) = 0;
58
    virtual int put(const char *s) = 0;
58
};
59
60
class DLFWImpl;
61
class DebugLogFileWriter : public DebugLogWriter {
62
    DLFWImpl *impl;
63
  public:
64
    DebugLogFileWriter();
65
    ~DebugLogFileWriter();
66
    virtual const char *getfilename();
67
    virtual int setfilename(const char *fname, int trnc = 1);
68
    virtual int put(const char *s);
69
};
59
};
70
60
71
class DLFWImpl {
61
class DLFWImpl {
72
    char *filename;
62
    char *filename;
73
    FILE *fp;
63
    FILE *fp;
...
...
114
    freeZ(filename);
104
    freeZ(filename);
115
    }
105
    }
116
106
117
 public:
107
 public:
118
108
109
    DLFWImpl() 
119
    DLFWImpl() : filename(0), fp(0), truncate(1) {
110
  : filename(0), fp(0), truncate(1) 
111
    {
120
    setfilename("stderr", 0);
112
    setfilename("stderr", 0);
121
    }
113
    }
122
    ~DLFWImpl() { 
114
    ~DLFWImpl() { 
123
    maybeclosefp();
115
    maybeclosefp();
124
    }
116
    }
125
    int setfilename(const char *fn, int trnc) {
117
    int setfilename(const char *fn, int trnc) {
126
    maybeclosefp();
118
    maybeclosefp();
127
    filename = strdup(fn);
119
    filename = strdup(fn);
128
    truncate = trnc;
120
    truncate = trnc;
121
  maybeopenfp();
129
    return 0;
122
    return 0;
130
    }
123
    }
131
    const char *getfilename() {
124
    const char *getfilename() {
132
    return filename;
125
    return filename;
133
    }
126
    }
...
...
137
        return fputs(s, fp);
130
        return fputs(s, fp);
138
    return -1;
131
    return -1;
139
    }
132
    }
140
};
133
};
141
134
142
DebugLogFileWriter::DebugLogFileWriter()
135
class DebugLogFileWriter : public DebugLogWriter {
143
{
136
    DLFWImpl *impl;
137
    PTMutexInit loglock;
138
  public:
139
    DebugLogFileWriter()
140
    {
144
    impl = new DLFWImpl;
141
  impl = new DLFWImpl;
145
}
142
    }
146
143
147
DebugLogFileWriter::~DebugLogFileWriter() 
144
    virtual ~DebugLogFileWriter() 
148
{ 
145
    { 
149
    delete impl;
146
  delete impl;
150
}
147
    }
151
148
152
int DebugLogFileWriter::setfilename(const char *fn, int trnc) {
149
    virtual int setfilename(const char *fn, int trnc) 
150
    {
151
  PTMutexLocker lock(loglock);
153
    return impl ? impl->setfilename(fn, trnc) : -1;
152
  return impl ? impl->setfilename(fn, trnc) : -1;
154
}
153
    }
155
154
    virtual const char *getfilename() 
156
const char *DebugLogFileWriter::getfilename() 
155
    {
157
{
156
  PTMutexLocker lock(loglock);
158
    return impl ? impl->getfilename() : 0;
157
  return impl ? impl->getfilename() : 0;
159
}
158
    }
160
159
    virtual int reopen()
161
int DebugLogFileWriter::put(const char *s) 
160
    {
162
{
161
  PTMutexLocker lock(loglock);
162
  if (!impl)
163
      return -1;
164
  string fn = impl->getfilename();
165
  return impl->setfilename(fn.c_str(), 1);
166
    }
167
    virtual int put(const char *s) 
168
    {
169
  PTMutexLocker lock(loglock);
163
    return impl ? impl->put(s) : -1;
170
  return impl ? impl->put(s) : -1;
171
    };
164
};
172
};
173
165
174
166
static set<string> yesfiles;
175
static set<string> yesfiles;
167
static void initfiles()
176
static void initfiles()
168
{
177
{
169
    const char *cp = getenv("DEBUGLOG_FILES");
178
    const char *cp = getenv("DEBUGLOG_FILES");
...
...
298
////////////////////////////////////////////////////////////
307
////////////////////////////////////////////////////////////
299
// Global functions
308
// Global functions
300
//////////////////////////////////////
309
//////////////////////////////////////
301
static DebugLogFileWriter lwriter;
310
static DebugLogFileWriter lwriter;
302
static DebugLogFileWriter *theWriter = &lwriter;
311
static DebugLogFileWriter *theWriter = &lwriter;
312
303
const char *getfilename() 
313
const char *getfilename() 
304
{
314
{
305
    return theWriter ? theWriter->getfilename() : 0;
315
    return theWriter ? theWriter->getfilename() : 0;
306
}
316
}
317
307
int setfilename(const char *fname, int trnc)
318
int setfilename(const char *fname, int trnc)
308
{
319
{
309
    return theWriter ? theWriter->setfilename(fname, trnc) : -1;
320
    return theWriter ? theWriter->setfilename(fname, trnc) : -1;
321
}
322
323
int reopen()
324
{
325
    return theWriter ? theWriter->reopen() : -1;
326
310
}
327
}
311
328
312
#if DEBUGLOG_USE_THREADS
329
#if DEBUGLOG_USE_THREADS
313
#include <pthread.h>
330
#include <pthread.h>
314
static pthread_key_t dbl_key;
331
static pthread_key_t dbl_key;