Skip to content

Commit 69b097d

Browse files
committed
added set context from given pid. moved static string util functions
1 parent fab70f6 commit 69b097d

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

include/InfoLogger/InfoLogger.hxx

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class InfoLoggerContext final
4747
/// Definition of field names (keys) which are available/settable in InfoLoggerContext
4848
enum class FieldName {Facility, Role, System, Detector, Partition, Run};
4949

50+
/// Function to parse input string and find matching FieldName.
51+
/// On success, output variable is set accordingly.
52+
/// \return 0 on success, -1 if no match found.
53+
static int getFieldNameFromString(const std::string &input, FieldName &output);
54+
5055
/// Create new context.
5156
/// The context is updated from available environment information, if any.
5257
InfoLoggerContext();
@@ -61,10 +66,18 @@ class InfoLoggerContext final
6166
/// All fields are cleared with default values.
6267
void reset();
6368

64-
/// Update context from current environment information.
69+
/// Update context from current environment information (of current process).
6570
/// Fields previously set by user may be overwritten.
6671
void refresh();
67-
72+
73+
74+
/// Update context from current environment information of a different process.
75+
/// This is used internally by command line 'log' utility to tag messages issued by parent process
76+
/// or on stdout by a program which output is piped to stdin of current process and injected in infologger.
77+
/// Fields previously set by user may be overwritten.
78+
void refresh(pid_t pid);
79+
80+
6881
/// Destroy context
6982
~InfoLoggerContext();
7083

@@ -84,8 +97,8 @@ class InfoLoggerContext final
8497

8598
// todo ?
8699
// int getField(FieldName key, std::string &value);
87-
88-
100+
101+
89102
private:
90103
// field undefined: empty string (for strings) or -1 (for integers)
91104

@@ -102,6 +115,8 @@ class InfoLoggerContext final
102115
std::string hostName; // name of host running the message source process
103116
std::string userName; // user running the message source process
104117

118+
// ideas of other possible fields: thread id, exe name, ...
119+
105120
friend class InfoLogger;
106121
};
107122

@@ -196,6 +211,20 @@ class InfoLogger final
196211
};
197212

198213

214+
/// Convert a string to an infologger severity
215+
/// \param text NUL-terminated word to convert to InfoLogger severity type. Current implementation is not exact-match, it takes closest based on first-letter value
216+
/// \return Corresponding severity (InfoLogger::Undefined if no match found)
217+
static InfoLogger::Severity getSeverityFromString(const char *text);
218+
219+
220+
/// Set a field in a message option struct based on its name.
221+
/// If input fieldName valid, and input fieldValue can be parsed, ouput variable is modified accordingly.
222+
/// \return 0 on success, an error code otherwise.
223+
static int setMessageOption(const char *fieldName, const char *fieldValue, InfoLoggerMessageOption &output);
224+
225+
226+
227+
199228
/// extended log function, with all extra fields, including a specific context
200229
/// \return 0 on success, an error code otherwise (but never throw exceptions)..
201230
int log(const InfoLoggerMessageOption &options, const InfoLoggerContext& context, const char *message, ...) __attribute__((format(printf, 4,5)));
@@ -240,6 +269,7 @@ class InfoLogger final
240269
}
241270

242271

272+
243273
///////////////////////
244274
/// internals
245275
///////////////////////
@@ -251,12 +281,6 @@ class InfoLogger final
251281

252282

253283

254-
/// Convert a string to an infologger severity
255-
/// \param text NUL-terminated word to convert to InfoLogger severity type. Current implementation is not exact-match, it takes closest based on first-letter value
256-
/// \return Corresponding severity (InfoLogger::Undefined if no match found)
257-
InfoLogger::Severity getSeverityFromString(const char *text);
258-
259-
260284
}
261285
}
262286

0 commit comments

Comments
 (0)