Skip to content

Commit f90356a

Browse files
committed
added context light copy
1 parent 9e4fe09 commit f90356a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

include/InfoLogger/InfoLogger.hxx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ class InfoLoggerContext final
8080
/// \param listOfKeyValuePairs A list of fieldName / value pairs to be set. Fields which were set automatically from environment are overwritten.
8181
InfoLoggerContext(const std::list<std::pair<FieldName, const std::string&>>& listOfKeyValuePairs);
8282

83+
/// Create new context.
84+
/// The context is created as a copy of an existing context.
85+
/// Additionnaly, listed fields are set with provided value.
86+
/// \param listOfKeyValuePairs A list of fieldName / value pairs to be set. Fields which were set from copy source are overwritten.
87+
InfoLoggerContext(const InfoLoggerContext &sourceContext, const std::list<std::pair<FieldName, const std::string&>>& listOfKeyValuePairs);
88+
8389
/// Update context with default values
8490
/// All fields are cleared with default values.
8591
void reset();

src/InfoLoggerContext.cxx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ InfoLoggerContext::InfoLoggerContext(const std::list<std::pair<FieldName, const
3030
setField(listOfKeyValuePairs);
3131
}
3232

33+
InfoLoggerContext::InfoLoggerContext(const InfoLoggerContext &sourceContext, const std::list<std::pair<FieldName, const std::string&>>& listOfKeyValuePairs)
34+
{
35+
// members initialization from source context
36+
facility = sourceContext.facility;
37+
role = sourceContext.role;
38+
system = sourceContext.system;
39+
detector = sourceContext.detector;
40+
partition = sourceContext.partition;
41+
run = sourceContext.run;
42+
processId = sourceContext.processId;
43+
hostName = sourceContext.hostName;
44+
userName = sourceContext.userName;
45+
46+
// now set fields provided as arguments
47+
setField(listOfKeyValuePairs);
48+
}
49+
3350
InfoLoggerContext::~InfoLoggerContext()
3451
{
3552
}

test/testInfoLogger.cxx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ int main()
4848
theLog.log({ InfoLogger::Severity::Info, 123, 456, __FILE__, __LINE__ }, "infoLogger message test with source code info");
4949
theLog.log(LOGINFO(123), "infoLogger message test with source code info");
5050

51+
// example use of context
52+
InfoLoggerContext ctxt({{InfoLoggerContext::FieldName::Facility, std::string("test1")}});
53+
theLog.log({{}},ctxt,"infoLogger message - facility test1");
54+
55+
// reuse a context and overwrite some fields
56+
theLog.log({{}},InfoLoggerContext(ctxt,{{InfoLoggerContext::FieldName::Facility, std::string("test2")}}),"infoLogger message - facility test2");
57+
58+
// c++ style
5159
theLog << "another test message " << InfoLogger::endm;
5260
theLog << InfoLogger::Severity::Error << "another (stream error) message " << InfoLogger::endm;
5361
theLog << InfoLogger::Severity::Warning << "another (stream warning) message " << InfoLogger::endm;

0 commit comments

Comments
 (0)