summaryrefslogtreecommitdiff
path: root/src/main/cpp/rollingfileappender.cpp
blob: 777d0f86eb4fe8cdcf8a4361399b34a6dee02b6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if defined(_MSC_VER)
#pragma warning ( disable: 4231 4251 4275 4786 )
#endif

#ifdef LOG4CXX_MULTI_PROCESS
#include <apr_portable.h>
#include <libgen.h>
#include <apr_file_io.h>
#include <apr_atomic.h>
#include <apr_mmap.h>
#ifndef MAX_FILE_LEN
#define MAX_FILE_LEN 2048
#endif
#include <log4cxx/pattern/filedatepatternconverter.h>
#include <log4cxx/helpers/date.h>
#endif

#include <log4cxx/rolling/rollingfileappender.h>
#include <log4cxx/helpers/loglog.h>
#include <log4cxx/helpers/synchronized.h>
#include <log4cxx/rolling/rolloverdescription.h>
#include <log4cxx/helpers/fileoutputstream.h>
#include <log4cxx/helpers/bytebuffer.h>
#include <log4cxx/rolling/fixedwindowrollingpolicy.h>
#include <log4cxx/rolling/manualtriggeringpolicy.h>

using namespace log4cxx;
using namespace log4cxx::rolling;
using namespace log4cxx::helpers;
using namespace log4cxx::spi;


IMPLEMENT_LOG4CXX_OBJECT(RollingFileAppenderSkeleton)
IMPLEMENT_LOG4CXX_OBJECT(RollingFileAppender)


/**
 * Construct a new instance.
 */
RollingFileAppenderSkeleton::RollingFileAppenderSkeleton() : _event(NULL){
}

RollingFileAppender::RollingFileAppender() {
}

/**
 * Prepare instance of use.
 */
void RollingFileAppenderSkeleton::activateOptions(Pool &p) {
  if (rollingPolicy == NULL) {
   FixedWindowRollingPolicy* fwrp = new FixedWindowRollingPolicy();
    fwrp->setFileNamePattern(getFile() + LOG4CXX_STR(".%i"));
    rollingPolicy = fwrp;
  }

  //
  //  if no explicit triggering policy and rolling policy is both.
  //
  if (triggeringPolicy == NULL) {
     TriggeringPolicyPtr trig(rollingPolicy);
     if (trig != NULL) {
         triggeringPolicy = trig;
     }
  }

  if (triggeringPolicy == NULL) {
    triggeringPolicy = new ManualTriggeringPolicy();
  }

  {
     synchronized sync(mutex);
     triggeringPolicy->activateOptions(p);
     rollingPolicy->activateOptions(p);

     try {
      RolloverDescriptionPtr rollover1 =
        rollingPolicy->initialize(getFile(), getAppend(), p);

      if (rollover1 != NULL) {
        ActionPtr syncAction(rollover1->getSynchronous());

        if (syncAction != NULL) {
          syncAction->execute(p);
        }

        setFile(rollover1->getActiveFileName());
        setAppend(rollover1->getAppend());

        //
        //  async action not yet implemented
        //
        ActionPtr asyncAction(rollover1->getAsynchronous());
        if (asyncAction != NULL) {
            asyncAction->execute(p);
        }
      }

      File activeFile;
      activeFile.setPath(getFile());

      if (getAppend()) {
        fileLength = activeFile.length(p);
      } else {
        fileLength = 0;
      }

      FileAppender::activateOptions(p);
    } catch (std::exception& ex) {
      LogLog::warn(
         LogString(LOG4CXX_STR("Exception will initializing RollingFileAppender named "))
             + getName());
    }
  }
}

#ifdef LOG4CXX_MULTI_PROCESS
void RollingFileAppenderSkeleton::releaseFileLock(apr_file_t* lock_file){
    if (lock_file){
        apr_status_t stat = apr_file_unlock(lock_file);
        if (stat != APR_SUCCESS){
            LogLog::warn(LOG4CXX_STR("flock: unlock failed"));
        }
        apr_file_close(lock_file);
        lock_file = NULL;
    }
}
#endif
/**
   Implements the usual roll over behaviour.

   <p>If <code>MaxBackupIndex</code> is positive, then files
   {<code>File.1</code>, ..., <code>File.MaxBackupIndex -1</code>}
   are renamed to {<code>File.2</code>, ...,
   <code>File.MaxBackupIndex</code>}. Moreover, <code>File</code> is
   renamed <code>File.1</code> and closed. A new <code>File</code> is
   created to receive further log output.

   <p>If <code>MaxBackupIndex</code> is equal to zero, then the
   <code>File</code> is truncated with no backup files created.

 * @return true if rollover performed.
 */
bool RollingFileAppenderSkeleton::rollover(Pool& p) {
    //
    //   can't roll without a policy
    //
    if (rollingPolicy != NULL) {

        {
            synchronized sync(mutex);

#ifdef LOG4CXX_MULTI_PROCESS
            std::string fileName(getFile());
            RollingPolicyBase *basePolicy = dynamic_cast<RollingPolicyBase* >(&(*rollingPolicy));
            apr_time_t n = apr_time_now();
            ObjectPtr obj(new Date(n));
            LogString fileNamePattern;
            if (basePolicy){
                if (basePolicy->getPatternConverterList().size()){
                    (*(basePolicy->getPatternConverterList().begin()))->format(obj, fileNamePattern, p);
                    fileName = std::string(fileNamePattern);
                }
            }

            bool bAlreadyRolled = true;
            char szDirName[MAX_FILE_LEN] = {'\0'};
            char szBaseName[MAX_FILE_LEN] = {'\0'};
            char szUid[MAX_FILE_LEN] = {'\0'};
            memcpy(szDirName, fileName.c_str(), fileName.size() > MAX_FILE_LEN ? MAX_FILE_LEN : fileName.size());
            memcpy(szBaseName, fileName.c_str(), fileName.size() > MAX_FILE_LEN ? MAX_FILE_LEN : fileName.size());
            apr_uid_t uid;
            apr_gid_t groupid;
            apr_status_t stat = apr_uid_current(&uid, &groupid, pool.getAPRPool());
            if (stat == APR_SUCCESS){
                snprintf(szUid, MAX_FILE_LEN, "%u", uid);
            }

            const std::string lockname = std::string(::dirname(szDirName)) + "/." + ::basename(szBaseName) + szUid + ".lock";
            apr_file_t* lock_file;
            stat = apr_file_open(&lock_file, lockname.c_str(), APR_CREATE | APR_READ | APR_WRITE, APR_OS_DEFAULT, p.getAPRPool());
            if (stat != APR_SUCCESS) {
                std::string err = "lockfile return error: open lockfile failed. ";
                err += (strerror(errno));
                LogLog::warn(LOG4CXX_STR(err.c_str()));
                bAlreadyRolled = false;
                lock_file = NULL;
            }else{
                stat = apr_file_lock(lock_file, APR_FLOCK_EXCLUSIVE);
                if (stat != APR_SUCCESS){
                    std::string err = "apr_file_lock: lock failed. ";
                    err += (strerror(errno));
                    LogLog::warn(LOG4CXX_STR(err.c_str()));
                    bAlreadyRolled = false;
                }
                else {
                    if (_event)
                        triggeringPolicy->isTriggeringEvent(this, *_event, getFile(), getFileLength());
                }
            }

            if (bAlreadyRolled){
                apr_finfo_t finfo1, finfo2;
                apr_status_t st1, st2;
                apr_file_t* _fd = getWriter()->getOutPutStreamPtr()->getFileOutPutStreamPtr().getFilePtr();
                st1 = apr_file_info_get(&finfo1, APR_FINFO_IDENT, _fd);
                if (st1 != APR_SUCCESS){
                    LogLog::warn(LOG4CXX_STR("apr_file_info_get failed"));
                }

                st2 = apr_stat(&finfo2, std::string(getFile()).c_str(), APR_FINFO_IDENT, p.getAPRPool());
                if (st2 != APR_SUCCESS){
                    LogLog::warn(LOG4CXX_STR("apr_stat failed."));
                }

                bAlreadyRolled = ((st1 == APR_SUCCESS) && (st2 == APR_SUCCESS)
                    && ((finfo1.device != finfo2.device) || (finfo1.inode != finfo2.inode)));
            }

            if (!bAlreadyRolled){
#endif
                try {
                    RolloverDescriptionPtr rollover1(rollingPolicy->rollover(this->getFile(), this->getAppend(), p));
                    if (rollover1 != NULL) {
                        if (rollover1->getActiveFileName() == getFile()) {
                            closeWriter();

                            bool success = true;
                            if (rollover1->getSynchronous() != NULL) {
                                success = false;

                                try {
                                    success = rollover1->getSynchronous()->execute(p);
                                } catch (std::exception& ex) {
                                    LogLog::warn(LOG4CXX_STR("Exception on rollover"));
                                }
                            }

                            if (success) {
                                if (rollover1->getAppend()) {
                                    fileLength = File().setPath(rollover1->getActiveFileName()).length(p);
                                } else {
                                    fileLength = 0;
                                }

                                //
                                //  async action not yet implemented
                                //
                                ActionPtr asyncAction(rollover1->getAsynchronous());
                                if (asyncAction != NULL) {
                                    asyncAction->execute(p);
                                }

                                setFile(
                                    rollover1->getActiveFileName(), rollover1->getAppend(),
                                    bufferedIO, bufferSize, p);
                            } else {
                                setFile(
                                    rollover1->getActiveFileName(), true, bufferedIO, bufferSize, p);
                            }
                        } else {
                            OutputStreamPtr os(new FileOutputStream(
                                    rollover1->getActiveFileName(), rollover1->getAppend()));
                            WriterPtr newWriter(createWriter(os));
                            closeWriter();
                            setFile(rollover1->getActiveFileName());
                            setWriter(newWriter);

                            bool success = true;

                            if (rollover1->getSynchronous() != NULL) {
                                success = false;

                                try {
                                    success = rollover1->getSynchronous()->execute(p);
                                } catch (std::exception& ex) {
                                    LogLog::warn(LOG4CXX_STR("Exception during rollover"));
                                }
                            }

                            if (success) {
                                if (rollover1->getAppend()) {
                                    fileLength = File().setPath(rollover1->getActiveFileName()).length(p);
                                } else {
                                    fileLength = 0;
                                }

                                //
                                //   async action not yet implemented
                                //
                                ActionPtr asyncAction(rollover1->getAsynchronous());
                                if (asyncAction != NULL) {
                                    asyncAction->execute(p);
                                }
                            }

                            writeHeader(p);
                        }

#ifdef LOG4CXX_MULTI_PROCESS
                        releaseFileLock(lock_file);
#endif
                        return true;
                    }
                } catch (std::exception& ex) {
                    LogLog::warn(LOG4CXX_STR("Exception during rollover"));
                }
#ifdef LOG4CXX_MULTI_PROCESS
            }else{
                reopenLatestFile(p);
            }
            releaseFileLock(lock_file);
#endif
        }
    }
    return false;
}

#ifdef LOG4CXX_MULTI_PROCESS
/**
 * re-open current file when its own handler has been renamed
 */
void RollingFileAppenderSkeleton::reopenLatestFile(Pool& p){
    closeWriter();
    OutputStreamPtr os(new FileOutputStream(getFile(), true));
    WriterPtr newWriter(createWriter(os));
    setFile(getFile());
    setWriter(newWriter);
    fileLength = File().setPath(getFile()).length(p);
    writeHeader(p);
}

#endif

/**
 * {@inheritDoc}
*/
void RollingFileAppenderSkeleton::subAppend(const LoggingEventPtr& event, Pool& p) {
  // The rollover check must precede actual writing. This is the
  // only correct behavior for time driven triggers.
  if (
    triggeringPolicy->isTriggeringEvent(
        this, event, getFile(), getFileLength())) {
    //
    //   wrap rollover request in try block since
    //    rollover may fail in case read access to directory
    //    is not provided.  However appender should still be in good
    //     condition and the append should still happen.
    try {
        _event = &(const_cast<LoggingEventPtr &>(event));
        rollover(p);
    } catch (std::exception& ex) {
        LogLog::warn(LOG4CXX_STR("Exception during rollover attempt."));
    }
  }

#ifdef LOG4CXX_MULTI_PROCESS
  //do re-check before every write
  //
  apr_finfo_t finfo1, finfo2;
  apr_status_t st1, st2;
  apr_file_t* _fd = getWriter()->getOutPutStreamPtr()->getFileOutPutStreamPtr().getFilePtr();
  st1 = apr_file_info_get(&finfo1, APR_FINFO_IDENT, _fd);
  if (st1 != APR_SUCCESS){
      LogLog::warn(LOG4CXX_STR("apr_file_info_get failed"));
  }

  st2 = apr_stat(&finfo2, std::string(getFile()).c_str(), APR_FINFO_IDENT, p.getAPRPool());
  if (st2 != APR_SUCCESS){
      std::string err = "apr_stat failed. file:" + std::string(getFile());
      LogLog::warn(LOG4CXX_STR(err.c_str()));
  }

  bool bAlreadyRolled = ((st1 == APR_SUCCESS) && (st2 == APR_SUCCESS)
      && ((finfo1.device != finfo2.device) || (finfo1.inode != finfo2.inode)));

  if (bAlreadyRolled){
      reopenLatestFile(p);
  }
#endif

  FileAppender::subAppend(event, p);
}

/**
 * Get rolling policy.
 * @return rolling policy.
 */
RollingPolicyPtr RollingFileAppenderSkeleton::getRollingPolicy() const {
  return rollingPolicy;
}

/**
 * Get triggering policy.
 * @return triggering policy.
 */
TriggeringPolicyPtr RollingFileAppenderSkeleton::getTriggeringPolicy() const {
  return triggeringPolicy;
}

/**
 * Sets the rolling policy.
 * @param policy rolling policy.
 */
void RollingFileAppenderSkeleton::setRollingPolicy(const RollingPolicyPtr& policy) {
  rollingPolicy = policy;
}

/**
 * Set triggering policy.
 * @param policy triggering policy.
 */
void RollingFileAppenderSkeleton::setTriggeringPolicy(const TriggeringPolicyPtr& policy) {
  triggeringPolicy = policy;
}

/**
 * Close appender.  Waits for any asynchronous file compression actions to be completed.
 */
void RollingFileAppenderSkeleton::close() {
  FileAppender::close();
}

namespace log4cxx {
  namespace rolling {
/**
 * Wrapper for OutputStream that will report all write
 * operations back to this class for file length calculations.
 */
class CountingOutputStream : public OutputStream {
  /**
   * Wrapped output stream.
   */
  private:
  OutputStreamPtr os;

  /**
   * Rolling file appender to inform of stream writes.
   */
  RollingFileAppenderSkeleton* rfa;

  public:
  /**
   * Constructor.
   * @param os output stream to wrap.
   * @param rfa rolling file appender to inform.
   */
  CountingOutputStream(
    OutputStreamPtr& os1, RollingFileAppenderSkeleton* rfa1) :
      os(os1), rfa(rfa1) {
  }

  /**
   * {@inheritDoc}
   */
  void close(Pool& p)  {
    os->close(p);
    rfa = 0;
  }

  /**
   * {@inheritDoc}
   */
  void flush(Pool& p)  {
    os->flush(p);
  }

  /**
   * {@inheritDoc}
   */
  void write(ByteBuffer& buf, Pool& p) {
    os->write(buf, p);
    if (rfa != 0) {
#ifndef LOG4CXX_MULTI_PROCESS
        rfa->incrementFileLength(buf.limit());
#else
        rfa->setFileLength(File().setPath(rfa->getFile()).length(p));
#endif
    }
  }

#ifdef LOG4CXX_MULTI_PROCESS
  OutputStream& getFileOutPutStreamPtr() { return *os;}
#endif
};
  }
}

/**
   Returns an OutputStreamWriter when passed an OutputStream.  The
   encoding used will depend on the value of the
   <code>encoding</code> property.  If the encoding value is
   specified incorrectly the writer will be opened using the default
   system encoding (an error message will be printed to the loglog.
 @param os output stream, may not be null.
 @return new writer.
 */
WriterPtr RollingFileAppenderSkeleton::createWriter(OutputStreamPtr& os) {
  OutputStreamPtr cos(new CountingOutputStream(os, this));
  return FileAppender::createWriter(cos);
}

/**
 * Get byte length of current active log file.
 * @return byte length of current active log file.
 */
size_t RollingFileAppenderSkeleton::getFileLength() const {
  return fileLength;
}

#ifdef LOG4CXX_MULTI_PROCESS
void RollingFileAppenderSkeleton::setFileLength(size_t length){
    fileLength = length;
}
#endif

/**
 * Increments estimated byte length of current active log file.
 * @param increment additional bytes written to log file.
 */
void RollingFileAppenderSkeleton::incrementFileLength(size_t increment) {
  fileLength += increment;
}