Skip to content

Commit

Permalink
Merge pull request #35 from sy-c/master
Browse files Browse the repository at this point in the history
simplelog rotate
  • Loading branch information
ktf authored Mar 9, 2020
2 parents 3ed18ce + 4eedd2a commit 3f92059
Show file tree
Hide file tree
Showing 6 changed files with 414 additions and 203 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ set_source_files_properties(test/testDataFormat.c PROPERTIES LANGUAGE CXX)
add_test(NAME testDataFormat COMMAND testDataFormat)
set_tests_properties(testDataFormat PROPERTIES TIMEOUT 60)

add_executable(testSimpleLog test/testSimpleLog.cxx)
target_link_libraries(testSimpleLog Common)

set(TEST_SRCS
test/TestBasicThread.cxx
test/testFifo.cxx
Expand Down
3 changes: 3 additions & 0 deletions include/Common/Daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class DaemonConfigParameters {
std::string userName; // user name under which should run the process. Not changed if empty.
int redirectOutput=0; // flag set to redirect stdout/stderr to /dev/null
std::string logFile; // log file (leave empty to keep stdout/stderr)
int logRotateMaxBytes = 0; // log file max size (0: unlimited)
int logRotateMaxFiles = 0; // log file max number of files kept (0:unlimited)
int logRotateNow = 0; // log file rotate now (0: append current, 1: create new)
};


Expand Down
47 changes: 25 additions & 22 deletions include/Common/SimpleLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,32 @@
///
/// \author Sylvain Chapeland, CERN


#ifndef SRC_SIMPLE_LOG_H
#define SRC_SIMPLE_LOG_H

#include <stdio.h>
#include <stdarg.h>
#include <memory>

class SimpleLog {
class SimpleLog
{

public:

public:
// constructor
// \param logFilePath Path to log file. If NULL, using stdout/stderr.
SimpleLog(const char* logFilePath=NULL);
SimpleLog(const char* logFilePath = NULL);

// destructor
~SimpleLog();

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

// Change file descriptors used for stdout/stderr with provided ones
// They must be valid for the lifetime of this object (or until overwritten), and are not closed.
Expand All @@ -36,33 +40,32 @@ class SimpleLog {
ShowSeveritySymbol = 0x4,
ShowMessage = 0x8
};

// Set output format based on (possibly OR-ed) format options from FormatOption enum
void setOutputFormat(int opts);

// Log an info message.
// The message is formatted with timestamp and severity.
//
//
// \param message Message, in a printf-like compatible format (with associated extra arguments)
// NB: attribute format for printf-like argument check -> 1st (invisible) arg is 'this'
int info(const char *message, ...) __attribute__ ((format (printf, 2,3)));
int info(const char* message, ...) __attribute__((format(printf, 2, 3)));

// Log an error message. See info().
int error(const char *message, ...) __attribute__ ((format (printf, 2,3)));
int error(const char* message, ...) __attribute__((format(printf, 2, 3)));

// Log a warning message. See info().
int warning(const char *message, ...) __attribute__ ((format (printf, 2,3)));


int warning(const char* message, ...) __attribute__((format(printf, 2, 3)));

// explicitely disable automatically generated methods
// disable copy constructor
SimpleLog(const SimpleLog&) =delete;
SimpleLog(const SimpleLog&) = delete;
// disable copy assignment operator
SimpleLog& operator=(const SimpleLog&) =delete;
private:
class Impl; // private class for implementation
std::unique_ptr<Impl> pImpl; // handle to private class instance at runtime
SimpleLog& operator=(const SimpleLog&) = delete;

private:
class Impl; // private class for implementation
std::unique_ptr<Impl> pImpl; // handle to private class instance at runtime
};

#endif /* SRC_SIMPLE_LOG_H */
Loading

0 comments on commit 3f92059

Please sign in to comment.