aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/logging/parc_Log.h
blob: 29e401e075608c2a325e710061a5cf1571244dfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * @file parc_Log.h
 * @brief Event logging.
 *
 * This is an logging mechanism patterned after the Syslog logging protocol (RFC 5424),
 * and influenced by `java.util.logging` and Apache Log4J.
 *
 * The lifecycle of a `PARCLog` starts with creating an instance via `parcLog_Create`
 * and calling the various functions to emit log messages.
 *
 * Finally the log is released via `parcLog_Release` which ensures
 * that any queued log messages are transmitted and resources are released.
 *
 * Every PARCLog instance has a logging level, a threshold that is set via `parcLog_SetLevel`
 * that determines what kind of PARCLogEntry instances are actually logged.
 * The PARCLogLevel PARCLogLevel_Emergency is always logged regardless of the current logging level.
 *
 */
#ifndef libparc_parc_Logger_h
#define libparc_parc_Logger_h

#include <stdarg.h>

#include <parc/logging/parc_LogReporter.h>
#include <parc/logging/parc_LogEntry.h>
#include <parc/logging/parc_LogLevel.h>

struct PARCLog;
typedef struct PARCLog PARCLog;

/**
 * Create a valid PARCLog instance.
 *
 * The initial instance's log level is set to `PARCLogLevel_Off`.
 *
 * @param [in] hostName A pointer to a nul-terminated C string, or NULL (See {@link PARCLogEntry}).
 * @param [in] applicationName A pointer to a nul-terminated C string, or NULL (See {@link PARCLogEntry}).
 * @param [in] processId A pointer to a nul-terminated C string, or NULL (See {@link PARCLogEntry}).
 * @param [in] reporter A pointer to a valid `PARCLogReporter` instance.
 *
 * @return non-NULL A valid PARCLog instance.
 * @return NULL An error occurred.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
 *
 *     PARCLogReporter *reporter = parcLogReporterFile_Create(output);
 *
 *     parcOutputStream_Release(&output);
 *     parcFileOutputStream_Release(&fileOutput);
 *
 *     PARCLog *log = parcLog_Create("localhost", "myApp", "daemon", reporter);
 *     parcLogReporter_Release(&reporter);
 * }
 * @endcode
 */
PARCLog *parcLog_Create(const char *hostName, const char *applicationName, const char *processId, PARCLogReporter *reporter);

/**
 * Increase the number of references to a `PARCLog`.
 *
 * Note that new `PARCLog` is not created,
 * only that the given `PARCLog` reference count is incremented.
 * Discard the reference by invoking `parcLog_Release`.
 *
 * @param [in] parcLog A pointer to a `PARCLog` instance.
 *
 * @return The input `PARCLog` pointer.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
 *
 *     PARCLogReporter *reporter = parcLogReporterFile_Create(output);
 *
 *     parcOutputStream_Release(&output);
 *     parcFileOutputStream_Release(&fileOutput);
 *
 *     PARCLog *log = parcLog_Create("localhost", "myApp", "daemon", reporter);
 *
 *     PARCLog *x_2 = parcLog_Acquire(log);
 *
 *     parcLog_Release(&log);
 *     parcLog_Release(&x_2);
 * }
 * @endcode
 */
PARCLog *parcLog_Acquire(const PARCLog *parcLog);

/**
 * Release a previously acquired reference to the specified instance,
 * decrementing the reference count for the instance.
 *
 * The pointer to the instance is set to NULL as a side-effect of this function.
 *
 * If the invocation causes the last reference to the instance to be released,
 * the instance is deallocated and the instance's implementation will perform
 * additional cleanup and release other privately held references.
 *
 * @param [in,out] logPtr A pointer to a PARCLog instance pointer, which will be set to zero on return.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
 *
 *     PARCLogReporter *reporter = parcLogReporterFile_Create(output);
 *
 *     parcOutputStream_Release(&output);
 *     parcFileOutputStream_Release(&fileOutput);
 *
 *     PARCLog *log = parcLog_Create("localhost", "myApp", "daemon", reporter);
 *     parcLogReporter_Release(&reporter);
 *
 *     parcLog_Release(&log);
 * }
 * @endcode
 */
void parcLog_Release(PARCLog **logPtr);

/**
 * Set the log severity level to the given value.
 *
 * The level is the maximum severity that will be logged via the PARCLogReporter.
 * The log severity PARCLogLevel_Emergency cannot be blocked.
 *
 * @param [in] log A pointer to valid instance of PARCLog.
 * @param [in] level A pointer to valid instance of PARCLogLevel.
 *
 * @return The previous value of the threshold.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
 *
 *     PARCLogReporter *reporter = parcLogReporterFile_Create(output);
 *
 *     parcOutputStream_Release(&output);
 *     parcFileOutputStream_Release(&fileOutput);
 *
 *     PARCLog *log = parcLog_Create("localhost", "myApp", "daemon", reporter);
 *     parcLogReporter_Release(&reporter);
 *
 *     PARCLogLevel old = parcLog_SetLevel(log, PARCLogLevel_Warning);
 *
 *     parcLog_SetLevel(log, old);
 *
 *     parcLog_Release(&log);
 * }
 * @endcode
 */
PARCLogLevel parcLog_SetLevel(PARCLog *log, const PARCLogLevel level);

/**
 * Get the severity level of the given PARCLog instance.
 *
 * The level is the maximum severity that will be logged via the PARCLogReporter.
 * The log severity PARCLogLevel_Emergency cannot be blocked.
 *
 * @param [in] log A pointer to valid instance of PARCLog.
 *
 * @return The severity level of the given PARCLog instance.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
 *
 *     PARCLogReporter *reporter = parcLogReporterFile_Create(output);
 *
 *     parcOutputStream_Release(&output);
 *     parcFileOutputStream_Release(&fileOutput);
 *
 *     PARCLog *log = parcLog_Create("localhost", "myApp", "daemon", reporter);
 *     parcLogReporter_Release(&reporter);
 *
 *     PARCLogLevel level = parcLog_GetLevel(log, PARCLogLevel_Warning);
 *
 *     parcLog_Release(&log);
 * }
 * @endcode
 */
PARCLogLevel parcLog_GetLevel(const PARCLog *log);

/**
 * Test if a PARCLogLevel would be logged by the current state of the given PARCLog instance.
 *
 * @param [in] log A pointer to valid instance of PARCLog.
 * @param [in] level An instance of PARCLogLevel.
 *
 * @return true A PARCLogEntry of the given level would be logged.
 * @return false A PARCLogEntry of the given level would be logged.
 *
 * Example:
 * @code
 * {
 *     PARCFileOutputStream *fileOutput = parcFileOutputStream_Create(1);
 *     PARCOutputStream *output = parcFileOutputStream_AsOutputStream(fileOutput);
 *
 *     PARCLogReporter *reporter = parcLogReporterFile_Create(output);
 *
 *     parcOutputStream_Release(&output);
 *     parcFileOutputStream_Release(&fileOutput);
 *
 *     PARCLog *log = parcLog_Create("localhost", "myApp", "daemon", reporter);
 *     parcLogReporter_Release(&reporter);
 *
 *     if (parcLog_IsLoggable(log, PARCLogLevel_Warning)) {
 *         printf("Logging is set to Warning severity level\n");
 *     }
 *
 *     parcLog_Release(&log);
 * }
 * @endcode
 */

#define parcLog_IsLoggable(_log_, _level_) \
    (_level_ == PARCLogLevel_Emergency) || (parcLogLevel_Compare(parcLog_GetLevel(_log_), _level_) >= 0)
//bool parcLog_IsLoggable(const PARCLog *log, const PARCLogLevel level);

/**
 * Compose and emit a log message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] level An instance of PARCLogLevel.
 * @param [in] messageId A value for the message identifier.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ap A `va_list` representing the parameters for the format specification.
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower than the specified PARCLogLevel.
 *
 * Example:
 * @code
 *     parcLog_MessageVaList(log, PARCLogLevel_Warning, 123, "This is a warning message.");
 * @endcode
 */
bool parcLog_MessageVaList(PARCLog *log, PARCLogLevel level, uint64_t messageId, const char *format, va_list ap);

/**
 * Compose and emit a log message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] level An instance of PARCLogLevel.
 * @param [in] messageId A value for the message identifier.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower than the specified PARCLogLevel.
 *
 * Example:
 * @code
 *     parcLog_Message(log, PARCLogLevel_Warning, "This is a warning message.");
 * @endcode
 */
bool parcLog_Message(PARCLog *log, PARCLogLevel level, uint64_t messageId, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Warning message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_Warning(log, "This is a warning message.");
 * @endcode
 */
bool parcLog_Warning(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Message level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_Info(log, "This is an info message.");
 * @endcode
 */
bool parcLog_Info(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Notice level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_Notice(log, "This is a notice message.");
 * @endcode
 */
bool parcLog_Notice(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Debug level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_DebugMessage(log, "This is a debug message.");
 * @endcode
 */
bool parcLog_Debug(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Error level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_ErrorMessage(log, "This is an error message.");
 * @endcode
 */
bool parcLog_Error(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Critical level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_CriticalMessage(log, "This is a critical message.");
 * @endcode
 */
bool parcLog_Critical(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Alert level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_AlertMessage(log, "This is an alert message.");
 * @endcode
 */
bool parcLog_Alert(PARCLog *log, const char *restrict format, ...);

/**
 * Compose and emit a PARCLogLevel_Emergency level message.
 *
 * @param [in] log A pointer to a valid PARCLog instance.
 * @param [in] format A pointer to a nul-terminated C string containing a printf format specification.
 * @param [in] ... Zero or more parameters as input for the format specification).
 *
 * @return true The message was logged.
 * @return false The message was not logged because the log severity threshold level is lower.
 *
 * Example:
 * @code
 *     parcLog_EmergencyMessage(log, "This is an emergency message.");
 * @endcode
 */
bool parcLog_Emergency(PARCLog *log, const char *restrict format, ...);
#endif // libparc_parc_Logger_h