Skip to content

Commit 06842d6

Browse files
committed
Implement include directive for properties files.
Initial patch provided by: Jukka Lantto <jukka dot lantto at outokumpu dot com>
1 parent 755de80 commit 06842d6

File tree

4 files changed

+70
-35
lines changed

4 files changed

+70
-35
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ Jens Rehsack <[email protected]>
1818
Siva Chandran P <[email protected]>
1919
Chernyshev Vyacheslav <[email protected]>
2020
Chris Steenwyk <[email protected]>
21+
Jukka Lantto <[email protected]>

include/log4cplus/configurator.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,23 @@ namespace log4cplus
4646
/**
4747
* Provides configuration from an external file. See configure() for
4848
* the expected format.
49-
*
49+
*
5050
* <em>All option values admit variable substitution.</em> For
5151
* example, if <code>userhome</code> environment property is set to
5252
* <code>/home/xyz</code> and the File option is set to the string
5353
* <code>${userhome}/test.log</code>, then File option will be
5454
* interpreted as the string <code>/home/xyz/test.log</code>.
55-
*
55+
*
5656
* The syntax of variable substitution is similar to that of UNIX
5757
* shells. The string between an opening <b>&quot;${&quot;</b> and
5858
* closing <b>&quot;}&quot;</b> is interpreted as a key. Its value is
5959
* searched in the environment properties. The corresponding value replaces
6060
* the ${variableName} sequence.
61+
*
62+
* Configuration files also recognize <code>include
63+
* <i>file.properties</i></code> directive that allow composing
64+
* configuration from multiple files. There is no cyclic includes
65+
* detection mechanism to stop unbound recursion.
6166
*/
6267
class LOG4CPLUS_EXPORT PropertyConfigurator
6368
{

include/log4cplus/helpers/property.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
namespace log4cplus {
4040
namespace helpers {
4141

42+
//! \sa log4cplus::PropertyConfigurator
4243
class LOG4CPLUS_EXPORT Properties {
4344
public:
4445
enum PFlags
@@ -141,6 +142,7 @@ namespace log4cplus {
141142

142143
// Data
143144
StringMap data;
145+
unsigned flags;
144146

145147
private:
146148
template <typename StringType>

src/property.cxx

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -102,38 +102,13 @@ trim_ws (tstring & str)
102102
}
103103

104104

105-
} // namespace
106-
107-
108-
109-
///////////////////////////////////////////////////////////////////////////////
110-
// Properties ctors and dtor
111-
///////////////////////////////////////////////////////////////////////////////
112-
113-
Properties::Properties()
114-
{
115-
}
116-
117-
118-
119-
Properties::Properties(tistream& input)
120-
{
121-
init(input);
122-
}
123-
124-
125-
126-
Properties::Properties(const tstring& inputFile, unsigned flags)
105+
void
106+
imbue_file_from_flags (tistream & file, unsigned flags)
127107
{
128-
if (inputFile.empty ())
129-
return;
130-
131-
tifstream file;
132-
133-
switch (flags & (fEncodingMask << fEncodingShift))
108+
switch (flags & (Properties::fEncodingMask << Properties::fEncodingShift))
134109
{
135110
#if defined (LOG4CPLUS_HAVE_CODECVT_UTF8_FACET) && defined (UNICODE)
136-
case fUTF8:
111+
case Properties::fUTF8:
137112
file.imbue (
138113
std::locale (file.getloc (),
139114
new std::codecvt_utf8<tchar, 0x10FFFF,
@@ -142,15 +117,15 @@ Properties::Properties(const tstring& inputFile, unsigned flags)
142117
#endif
143118

144119
#if defined (LOG4CPLUS_HAVE_CODECVT_UTF16_FACET) && defined (UNICODE)
145-
case fUTF16:
120+
case Properties::fUTF16:
146121
file.imbue (
147122
std::locale (file.getloc (),
148123
new std::codecvt_utf16<tchar, 0x10FFFF,
149124
static_cast<std::codecvt_mode>(std::consume_header | std::little_endian)>));
150125
break;
151126

152127
#elif defined (UNICODE) && defined (WIN32)
153-
case fUTF16:
128+
case Properties::fUTF16:
154129
file.imbue (
155130
std::locale (file.getloc (),
156131
// TODO: This should actually be a custom "null" facet
@@ -161,19 +136,53 @@ Properties::Properties(const tstring& inputFile, unsigned flags)
161136
#endif
162137

163138
#if defined (LOG4CPLUS_HAVE_CODECVT_UTF32_FACET) && defined (UNICODE)
164-
case fUTF32:
139+
case Properties::fUTF32:
165140
file.imbue (
166141
std::locale (file.getloc (),
167142
new std::codecvt_utf32<tchar, 0x10FFFF,
168143
static_cast<std::codecvt_mode>(std::consume_header | std::little_endian)>));
169144
break;
170145
#endif
171146

172-
case fUnspecEncoding:;
147+
case Properties::fUnspecEncoding:;
173148
default:
174149
// Do nothing.
175150
;
176151
}
152+
}
153+
154+
155+
} // namespace
156+
157+
158+
159+
///////////////////////////////////////////////////////////////////////////////
160+
// Properties ctors and dtor
161+
///////////////////////////////////////////////////////////////////////////////
162+
163+
Properties::Properties()
164+
: flags (0)
165+
{
166+
}
167+
168+
169+
170+
Properties::Properties(tistream& input)
171+
: flags (0)
172+
{
173+
init(input);
174+
}
175+
176+
177+
178+
Properties::Properties(const tstring& inputFile, unsigned f)
179+
: flags (f)
180+
{
181+
if (inputFile.empty ())
182+
return;
183+
184+
tifstream file;
185+
imbue_file_from_flags (file, flags);
177186

178187
file.open(LOG4CPLUS_FSTREAM_PREFERED_FILE_NAME(inputFile).c_str(),
179188
std::ios::binary);
@@ -216,6 +225,24 @@ Properties::init(tistream& input)
216225
trim_ws (value);
217226
setProperty(key, value);
218227
}
228+
else if (buffer.compare (0, 7, LOG4CPLUS_TEXT ("include")) == 0
229+
&& buffer.size () >= 7 + 1 + 1
230+
&& is_space (buffer[7]))
231+
{
232+
tstring included (buffer, 8) ;
233+
trim_ws (included);
234+
235+
tifstream file;
236+
imbue_file_from_flags (file, flags);
237+
238+
file.open (LOG4CPLUS_FSTREAM_PREFERED_FILE_NAME(included).c_str(),
239+
std::ios::binary);
240+
if (! file.good ())
241+
helpers::getLogLog ().error (
242+
LOG4CPLUS_TEXT ("could not open file ") + included);
243+
244+
init (file);
245+
}
219246
}
220247
}
221248

0 commit comments

Comments
 (0)