SimpleConfigurator.cpp
Go to the documentation of this file.
1 /*
2  * SimpleConfigurator.cpp
3  *
4  * Copyright 2001, Glen Scott. All rights reserved.
5  *
6  * See the COPYING file for the terms of usage and distribution.
7  */
8 #include "PortabilityImpl.hh"
9 
10 #ifdef LOG4CPP_HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #ifdef LOG4CPP_HAVE_IO_H
14 # include <io.h>
15 #endif
16 
17 #include <stdio.h>
18 #include <iostream>
19 #include <string>
20 #include <fstream>
21 #include <stdio.h>
22 
23 #include <log4cpp/Category.hh>
24 #include <log4cpp/Appender.hh>
26 #include <log4cpp/FileAppender.hh>
29 #include <log4cpp/Layout.hh>
30 #include <log4cpp/BasicLayout.hh>
31 #include <log4cpp/SimpleLayout.hh>
32 #include <log4cpp/Priority.hh>
33 #include <log4cpp/NDC.hh>
34 #include <log4cpp/PatternLayout.hh>
36 #ifdef LOG4CPP_HAVE_SYSLOG
38 #endif
39 #ifndef LOG4CPP_DISABLE_REMOTE_SYSLOG
41 #endif
42 #ifdef WIN32
44 #endif
45 
46 namespace log4cpp {
47 
48  void SimpleConfigurator::configure(const std::string& initFileName) throw (ConfigureFailure) {
49  std::ifstream initFile(initFileName.c_str());
50 
51  if (!initFile) {
52  throw ConfigureFailure(std::string("Config File ") + initFileName + " does not exist or is unreadable");
53  }
54 
55  configure(initFile);
56  }
57 
58  void SimpleConfigurator::configure(std::istream& initFile) throw (ConfigureFailure) {
59  std::string nextCommand;
60  std::string categoryName;
61 
62  while (initFile >> nextCommand) {
63  /* skip comment lines */
64  if (nextCommand[0] == '#') {
65  std::string dummy;
66  std::getline(initFile, dummy);
67  continue;
68  }
69  /* stop on missing categoryName */
70  if (!(initFile >> categoryName))
71  break;
72 
73  log4cpp::Category& category =
74  (categoryName.compare("root") == 0) ?
76  log4cpp::Category::getInstance(categoryName);
77 
78  if (nextCommand.compare("appender") == 0) {
79  std::string layout;
80  std::string appenderName;
81 
82  if (initFile >> layout >> appenderName) {
83  log4cpp::Appender* appender;
84  if (appenderName.compare("file") == 0) {
85  std::string logFileName;
86  if (!(initFile >> logFileName)) {
87  throw ConfigureFailure("Missing filename for log file logging configuration file for category: " + categoryName);
88  }
89  appender = new log4cpp::FileAppender(categoryName, logFileName);
90  }
91  else if (appenderName.compare("rolling") == 0) {
92  std::string logFileName;
93  size_t maxFileSize;
94  unsigned int maxBackupIndex=1;
95  if (!(initFile >> logFileName)) {
96  throw ConfigureFailure("Missing filename for log file logging configuration file for category: " + categoryName);
97  }
98  if (!(initFile >> maxFileSize)) {
99  throw ConfigureFailure("Missing maximum size for log file logging configuration file for category: " + categoryName);
100  }
101  if (!(initFile >> maxBackupIndex)) {
102  throw ConfigureFailure("Missing maximum backup index for log file logging configuration file for category: " + categoryName);
103  }
104  appender = new log4cpp::RollingFileAppender(categoryName, logFileName, maxFileSize, maxBackupIndex);
105  }
106  else if (appenderName.compare("dailyrolling") == 0) {
107  std::string logFileName;
108  unsigned int maxKeepDays=1;
109  if (!(initFile >> logFileName)) {
110  throw ConfigureFailure("Missing filename for log file logging configuration file for category: " + categoryName);
111  }
112  if (!(initFile >> maxKeepDays)) {
113  throw ConfigureFailure("Missing maximum keep days for log file logging configuration file for category: " + categoryName);
114  }
115  appender = new log4cpp::DailyRollingFileAppender(categoryName, logFileName, maxKeepDays);
116  }
117  else if (appenderName.compare("console") == 0) {
118  appender =
119  new log4cpp::OstreamAppender(categoryName, &std::cout);
120  }
121  else if (appenderName.compare("stdout") == 0) {
122  appender =
123  new log4cpp::FileAppender(categoryName, ::dup(fileno(stdout)));
124  }
125  else if (appenderName.compare("stderr") == 0) {
126  appender =
127  new log4cpp::FileAppender(categoryName, ::dup(fileno(stderr)));
128  }
129 #ifdef LOG4CPP_HAVE_SYSLOG
130  else if (appenderName.compare("syslog") == 0) {
131  std::string syslogName;
132  int facility;
133  if (!(initFile >> syslogName)) {
134  throw ConfigureFailure("Missing syslogname for SysLogAppender for category: " + categoryName);
135  }
136  if (!(initFile >> facility)) {
137  facility = LOG_USER;
138  } else {
139  // * 8
140  facility *= 8;
141  }
142  appender =
143  new log4cpp::SyslogAppender(categoryName, syslogName, facility);
144  }
145 #endif
146 #if defined(WIN32)
147  else if (appenderName.compare("nteventlog") == 0) {
148  std::string source;
149  if (!(initFile >> source)) {
150  throw ConfigureFailure("Missing source for NTEventLogAppender for category: " + categoryName);
151  }
152  appender =
153  new log4cpp::NTEventLogAppender(categoryName, source);
154  }
155 #endif
156 #if !defined(LOG4CPP_DISABLE_REMOTE_SYSLOG)
157  else if (appenderName.compare("remotesyslog") == 0) {
158  std::string syslogName;
159  std::string relayer;
160  int facility;
161  int portNumber;
162  if (!(initFile >> syslogName)) {
163  throw ConfigureFailure("Missing syslogname for SysLogAppender for category: " + categoryName);
164  }
165  if (!(initFile >> relayer)) {
166  throw ConfigureFailure("Missing syslog host for SysLogAppender for category: " + categoryName);
167  }
168  if (!(initFile >> facility)) {
169  facility = LOG_USER;
170  }
171  if (!(initFile >> portNumber)) {
172  portNumber = 514;
173  }
174  appender =
175  new log4cpp::RemoteSyslogAppender(categoryName, syslogName, relayer, facility, portNumber);
176  }
177 #endif // LOG4CPP_DISABLE_REMOTE_SYSLOG
178  else {
179  throw ConfigureFailure("Invalid appender name (" +
180  appenderName +
181  ") in logging configuration file for category: " +
182  categoryName);
183  }
184  if (layout.compare("basic") == 0)
185  appender->setLayout(new log4cpp::BasicLayout());
186  else if (layout.compare("simple") == 0)
187  appender->setLayout(new log4cpp::SimpleLayout());
188  else if (layout.compare("pattern") == 0) {
189  log4cpp::PatternLayout *layout =
191  initFile >> std::ws; // skip whitespace
192  char pattern[1000];
193  initFile.getline(pattern, 1000);
194  layout->setConversionPattern(std::string(pattern));
195  appender->setLayout(layout);
196  }
197  else {
198  throw ConfigureFailure("Invalid layout (" + layout +
199  ") in logging configuration file for category: " +
200  categoryName);
201  }
202  category.addAppender(appender);
203  }
204  }
205  else if (nextCommand.compare("priority") == 0) {
206  std::string priority;
207  if (!(initFile >> priority)) {
208  throw ConfigureFailure("Missing priority in logging configuration file for category: " + categoryName);
209  }
210 
211  try {
213  } catch(std::invalid_argument) {
214  throw ConfigureFailure("Invalid priority ("+priority+") in logging configuration file for category: "+categoryName);
215  }
216  }
217  else if (nextCommand.compare("category") == 0) {
218  /*
219  This command means we should "refer" to the category
220  (in order to have it created). We've already done this
221  in common setup code for all commands.
222  */
223  }
224  else {
225  throw ConfigureFailure("Invalid format in logging configuration file. Command: " + nextCommand);
226  }
227  }
228  }
229 }
230 
231 
232 
static void configure(const std::string &initFileName)
static Category & getInstance(const std::string &name)
Definition: Category.cpp:35
virtual void setConversionPattern(const std::string &conversionPattern)
static Category & getRoot()
Definition: Category.cpp:23
virtual void setPriority(Priority::Value priority)
Definition: Category.cpp:71
virtual void addAppender(Appender *appender)
Definition: Category.cpp:94
virtual void setLayout(Layout *layout)=0
static Value getPriorityValue(const std::string &priorityName)
Definition: Priority.cpp:44
random user-level messages


log4cpp
Author(s): Stephen Roderick, Bastiaan Bakker, Cedric Le Goater, Steve Ostlind, Marcel Harkema, Walter Stroebel, Glenn Scott and Tony Cheung
autogenerated on Sun Jun 23 2019 19:10:00