Skip to content

Commit 3f92059

Browse files
authored
Merge pull request #35 from sy-c/master
simplelog rotate
2 parents 3ed18ce + 4eedd2a commit 3f92059

File tree

6 files changed

+414
-203
lines changed

6 files changed

+414
-203
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ set_source_files_properties(test/testDataFormat.c PROPERTIES LANGUAGE CXX)
126126
add_test(NAME testDataFormat COMMAND testDataFormat)
127127
set_tests_properties(testDataFormat PROPERTIES TIMEOUT 60)
128128

129+
add_executable(testSimpleLog test/testSimpleLog.cxx)
130+
target_link_libraries(testSimpleLog Common)
131+
129132
set(TEST_SRCS
130133
test/TestBasicThread.cxx
131134
test/testFifo.cxx

include/Common/Daemon.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class DaemonConfigParameters {
1818
std::string userName; // user name under which should run the process. Not changed if empty.
1919
int redirectOutput=0; // flag set to redirect stdout/stderr to /dev/null
2020
std::string logFile; // log file (leave empty to keep stdout/stderr)
21+
int logRotateMaxBytes = 0; // log file max size (0: unlimited)
22+
int logRotateMaxFiles = 0; // log file max number of files kept (0:unlimited)
23+
int logRotateNow = 0; // log file rotate now (0: append current, 1: create new)
2124
};
2225

2326

include/Common/SimpleLog.h

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,32 @@
33
///
44
/// \author Sylvain Chapeland, CERN
55

6-
76
#ifndef SRC_SIMPLE_LOG_H
87
#define SRC_SIMPLE_LOG_H
98

109
#include <stdio.h>
1110
#include <stdarg.h>
1211
#include <memory>
1312

14-
class SimpleLog {
13+
class SimpleLog
14+
{
1515

16-
public:
17-
16+
public:
1817
// constructor
1918
// \param logFilePath Path to log file. If NULL, using stdout/stderr.
20-
SimpleLog(const char* logFilePath=NULL);
19+
SimpleLog(const char* logFilePath = NULL);
2120

2221
// destructor
2322
~SimpleLog();
24-
23+
2524
// Set (or change) the log file name. Previous file used is closed. New file is appended when already existing.
25+
// Optionnaly, an automatic log rotation can be defined. Older files are renamed, appending .1, .2, .3, etc.
2626
// \param logFilePath Path to log file. If NULL, using stdout/stderr.
27-
int setLogFile(const char* logFilePath=NULL);
27+
// \param rotateMaxBytes Maximum file size, after which a new file is created. If zero, no limit.
28+
// \param rotateMaxFiles Maximum number of files to keep (including the "current" file). If zero, no limit.
29+
// \param rotateNow If non-zero, the file is immediately rotated (independently of its size), otherwise it is appended.
30+
int setLogFile(const char* logFilePath = NULL,
31+
unsigned long rotateMaxBytes = 0, unsigned int rotateMaxFiles = 0, unsigned int rotateNow = 0);
2832

2933
// Change file descriptors used for stdout/stderr with provided ones
3034
// They must be valid for the lifetime of this object (or until overwritten), and are not closed.
@@ -36,33 +40,32 @@ class SimpleLog {
3640
ShowSeveritySymbol = 0x4,
3741
ShowMessage = 0x8
3842
};
39-
43+
4044
// Set output format based on (possibly OR-ed) format options from FormatOption enum
4145
void setOutputFormat(int opts);
42-
46+
4347
// Log an info message.
4448
// The message is formatted with timestamp and severity.
45-
//
49+
//
4650
// \param message Message, in a printf-like compatible format (with associated extra arguments)
4751
// NB: attribute format for printf-like argument check -> 1st (invisible) arg is 'this'
48-
int info(const char *message, ...) __attribute__ ((format (printf, 2,3)));
52+
int info(const char* message, ...) __attribute__((format(printf, 2, 3)));
4953

5054
// Log an error message. See info().
51-
int error(const char *message, ...) __attribute__ ((format (printf, 2,3)));
52-
55+
int error(const char* message, ...) __attribute__((format(printf, 2, 3)));
56+
5357
// Log a warning message. See info().
54-
int warning(const char *message, ...) __attribute__ ((format (printf, 2,3)));
55-
56-
58+
int warning(const char* message, ...) __attribute__((format(printf, 2, 3)));
59+
5760
// explicitely disable automatically generated methods
5861
// disable copy constructor
59-
SimpleLog(const SimpleLog&) =delete;
62+
SimpleLog(const SimpleLog&) = delete;
6063
// disable copy assignment operator
61-
SimpleLog& operator=(const SimpleLog&) =delete;
62-
63-
private:
64-
class Impl; // private class for implementation
65-
std::unique_ptr<Impl> pImpl; // handle to private class instance at runtime
64+
SimpleLog& operator=(const SimpleLog&) = delete;
65+
66+
private:
67+
class Impl; // private class for implementation
68+
std::unique_ptr<Impl> pImpl; // handle to private class instance at runtime
6669
};
6770

6871
#endif /* SRC_SIMPLE_LOG_H */

0 commit comments

Comments
 (0)