Browse Source

[trac555] Add file appender

Added the file appender, which routes output to a file (appending
to a file if one exists).
Stephen Morris 14 years ago
parent
commit
3d05a83e94

+ 5 - 1
src/lib/log/logger_manager.cc

@@ -105,6 +105,8 @@ LoggerManager::init(const std::string& root, const char* file,
 
 
 /// Read local message file
+// TODO This should be done after the configuration has been read so that
+// the file can be placed in the local configuration
 void
 LoggerManager::readLocalMessageFile(const char* file) {
 
@@ -113,7 +115,9 @@ LoggerManager::readLocalMessageFile(const char* file) {
     MessageDictionary& dictionary = MessageDictionary::globalDictionary();
     MessageReader reader(&dictionary);
     try {
-        logger.info(MSG_RDLOCMES).arg(file);
+
+        // FIXEM: commented out for testing
+        // logger.info(MSG_RDLOCMES).arg(file);
         reader.readFile(file, MessageReader::REPLACE);
 
         // File successfully read, list the duplicates

+ 25 - 0
src/lib/log/logger_manager_impl.cc

@@ -17,6 +17,7 @@
 #include <log4cplus/logger.h>
 #include <log4cplus/configurator.h>
 #include <log4cplus/consoleappender.h>
+#include <log4cplus/fileappender.h>
 
 #include "log/logger_level_impl.h"
 #include "log/logger_manager_impl.h"
@@ -119,6 +120,30 @@ LoggerManagerImpl::createConsoleAppender(log4cplus::Logger& logger,
     logger.addAppender(console);
 }
 
+// File appender.  Depending on whether a maximum size is given, either
+// a standard file appender or a rolling file appender will be created.
+void
+LoggerManagerImpl::createFileAppender(log4cplus::Logger& logger,
+                                         const OutputOption& opt)
+{
+    LOG4CPLUS_OPEN_MODE_TYPE mode = 
+        LOG4CPLUS_FSTREAM_NAMESPACE::ios::app;  // Append to existing file
+
+    log4cplus::SharedAppenderPtr fileapp;
+    if (opt.maxsize == 0) {
+        fileapp = log4cplus::SharedAppenderPtr(new log4cplus::FileAppender(
+            opt.filename, mode, opt.flush));
+    } else {
+        fileapp = log4cplus::SharedAppenderPtr(
+            new log4cplus::RollingFileAppender(opt.filename, opt.maxsize,
+                                               opt.maxver, opt.flush));
+    }
+
+    // use the same console layout for the files.
+    setConsoleAppenderLayout(fileapp);
+    logger.addAppender(fileapp);
+}
+
 
 // One-time initialization of the log4cplus system
 

+ 1 - 1
src/lib/log/logger_manager_impl.h

@@ -101,7 +101,7 @@ private:
     /// \param logger Log4cplus logger to which the appender must be attached.
     /// \param opt Output options for this appender.
     void createFileAppender(log4cplus::Logger& logger,
-                            const OutputOption& opt) {}
+                            const OutputOption& opt);
 
     /// \brief Create syslog appender
     ///

+ 0 - 3
src/lib/log/tests/logger_manager_unittest.cc

@@ -131,15 +131,12 @@ public:
         }
         filename << "/bind10_logger_manager_test_XXXXXX";
 
-        cout << "*** file name before call is " << filename.str() << "\n";
-
         // Copy into writeable storage for the call to mkstemp
         boost::scoped_array<char> tname(new char[filename.str().size() + 1]);
         strcpy(tname.get(), filename.str().c_str());
 
         // Create file, close and delete it, and store the name for later.
         int filenum = mkstemp(tname.get());
-        cout << "*** file name after call is " << tname.get() << "\n";
         if (filenum == -1) {
             isc_throw(Exception, "Unable to obtain unique filename");
         }

+ 21 - 5
src/lib/log/tests/logger_support_test.cc

@@ -42,17 +42,21 @@ using namespace std;
 
 void usage() {
     cout <<
-"logger_support_test [-h] [-s severity] [-d dbglevel] [-c stream] [localfile]\n"
+"logger_support_test [-h] [-c stream] [-d dbglevel] [-f file]\n"
+"                    [-s severity] [localfile]\n"
 "\n"
 "   -h              Print this message and exit\n"
-"   -s severity     Set the severity of messages output.  'severity' is one\n"
-"                   of 'debug', 'info', 'warn', 'error', 'fatal', the default\n"
-"                   being 'info'.\n"
+"\n"
 "   -d dbglevel     Debug level.  Only interpreted if the severity is 'debug'\n"
 "                   this is a number between 0 and 99.\n"
 "   -c stream       Send output to the console.  'stream' is one of 'stdout'\n"
 "                   of 'stderr'.  The '-c' switch is incompatible with '-f'\n"
 "                   and '-l'\n"
+"   -f file         Send output to specified file, appending to existing file\n"
+"                   if one exists.  Incompatible with -c and -l switches.\n"
+"   -s severity     Set the severity of messages output.  'severity' is one\n"
+"                   of 'debug', 'info', 'warn', 'error', 'fatal', the default\n"
+"                   being 'info'.\n"
 "\n"
 "If none of -c, -f or -l is given, by default, output is sent to stdout\n";
 }
@@ -80,7 +84,7 @@ int main(int argc, char** argv) {
     Logger rootLogger(ROOT_NAME);
 
     // Parse options
-    while ((option = getopt(argc, argv, "hc:d:s:")) != -1) {
+    while ((option = getopt(argc, argv, "hc:d:f:s:")) != -1) {
         switch (option) {
         case 'c':
             if (f_found || l_found) {
@@ -107,6 +111,18 @@ int main(int argc, char** argv) {
             spec.setDbglevel(boost::lexical_cast<int>(optarg));
             break;
 
+        case 'f':
+            if (c_found || l_found) {
+                cerr << "Cannot specify -f with -c or -l\n";
+                return (1);
+            }
+
+            f_found = true;
+
+            outopt.destination = OutputOption::DEST_FILE;
+            outopt.filename = optarg;
+            break;
+
         case 'h':
             usage();
             return (0);