EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
log.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/log.h: Common logging functions. */
21 
22 #ifndef ETCPAL_LOG_H_
23 #define ETCPAL_LOG_H_
24 
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include "etcpal/common.h"
30 
145 /* clang-format off */
146 
151 #define ETCPAL_LOG_KERN (0 << 3)
152 #define ETCPAL_LOG_USER (1 << 3)
153 #define ETCPAL_LOG_MAIL (2 << 3)
154 #define ETCPAL_LOG_DAEMON (3 << 3)
155 #define ETCPAL_LOG_AUTH (4 << 3)
156 #define ETCPAL_LOG_SYSLOG (5 << 3)
157 #define ETCPAL_LOG_LPR (6 << 3)
158 #define ETCPAL_LOG_NEWS (7 << 3)
159 #define ETCPAL_LOG_UUCP (8 << 3)
160 #define ETCPAL_LOG_CRON (9 << 3)
161 #define ETCPAL_LOG_AUTHPRIV (10 << 3)
162 #define ETCPAL_LOG_FTP (11 << 3)
164 #define ETCPAL_LOG_LOCAL0 (16 << 3)
165 #define ETCPAL_LOG_LOCAL1 (17 << 3)
166 #define ETCPAL_LOG_LOCAL2 (18 << 3)
167 #define ETCPAL_LOG_LOCAL3 (19 << 3)
168 #define ETCPAL_LOG_LOCAL4 (20 << 3)
169 #define ETCPAL_LOG_LOCAL5 (21 << 3)
170 #define ETCPAL_LOG_LOCAL6 (22 << 3)
171 #define ETCPAL_LOG_LOCAL7 (23 << 3)
176 #define ETCPAL_LOG_NFACILITIES 24
178 #define ETCPAL_LOG_FACMASK 0x03f8 /* Mask off the facility part of a prival */
180 #define ETCPAL_LOG_FAC(p) (((p) & ETCPAL_LOG_FACMASK) >> 3)
186 #define ETCPAL_LOG_EMERG 0
187 #define ETCPAL_LOG_ALERT 1
188 #define ETCPAL_LOG_CRIT 2
189 #define ETCPAL_LOG_ERR 3
190 #define ETCPAL_LOG_WARNING 4
191 #define ETCPAL_LOG_NOTICE 5
192 #define ETCPAL_LOG_INFO 6
193 #define ETCPAL_LOG_DEBUG 7
199 #define ETCPAL_LOG_PRIMASK 0x07 /* mask off the priority part of a prival */
201 #define ETCPAL_LOG_PRI(p) ((p) & ETCPAL_LOG_PRIMASK)
202 #define ETCPAL_LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
204 #define ETCPAL_LOG_MASK(pri) (1 << (pri))
205 #define ETCPAL_LOG_UPTO(pri) ((1 << ((pri) + 1)) - 1)
207 #define ETCPAL_LOG_HOSTNAME_MAX_LEN 256u
208 #define ETCPAL_LOG_APP_NAME_MAX_LEN 49u
209 #define ETCPAL_LOG_PROCID_MAX_LEN 129u
212 #define ETCPAL_RAW_LOG_MSG_MAX_LEN 480u
213 
214 /* clang-format on */
215 
216 /*
217  * Timestamp length:
218  * Date: 10 (1970-01-01)
219  * "T" or " ": 1
220  * Time: 12 (00:00:00.001)
221  * UTC Offset: 6 (-05:00)
222  * Nullterm: 1
223  * --------------
224  * Total: 30
225  */
227 #define ETCPAL_LOG_TIMESTAMP_LEN 30u
228 
229 /*
230  * Syslog header length (from RFC 5424):
231  * PRIVAL: 5
232  * VERSION: 3
233  * SP: 1
234  * TIMESTAMP: [Referenced]
235  * SP: 1
236  * HOSTNAME: [Referenced]
237  * SP: 1
238  * APP-NAME: [Referenced]
239  * SP: 1
240  * PROCID: [Referenced]
241  * SP: 1
242  * MSGID (not used): 1
243  * SP: 1
244  * STRUCTURED-DATA (not used): 1
245  * SP: 1
246  * -----------------------------
247  * Total non-referenced: 17
248  */
250 #define ETCPAL_SYSLOG_HEADER_MAX_LEN \
251  (17u + (ETCPAL_LOG_TIMESTAMP_LEN - 1u) + (ETCPAL_LOG_HOSTNAME_MAX_LEN - 1u) + (ETCPAL_LOG_APP_NAME_MAX_LEN - 1u) + \
252  (ETCPAL_LOG_PROCID_MAX_LEN - 1u))
253 
255 #define ETCPAL_SYSLOG_STR_MIN_LEN ETCPAL_SYSLOG_HEADER_MAX_LEN
257 #define ETCPAL_LOG_STR_MIN_LEN (ETCPAL_LOG_TIMESTAMP_LEN + 1u /*SP*/ + 6u /*pri*/ + 1u /*SP*/)
258 
260 #define ETCPAL_SYSLOG_STR_MAX_LEN (ETCPAL_SYSLOG_HEADER_MAX_LEN + ETCPAL_RAW_LOG_MSG_MAX_LEN)
261 
262 /*
263  * Human-reaadable log string max length:
264  * Timestamp: [Referenced]
265  * Space: 1
266  * Priority: 6 ([CRIT])
267  * Space: 1
268  * Message length: [Referenced]
269  * ----------------------------
270  * Total non-referenced: 8
271  */
276 #define ETCPAL_LOG_STR_MAX_LEN (8u + (ETCPAL_LOG_TIMESTAMP_LEN - 1u) + ETCPAL_RAW_LOG_MSG_MAX_LEN)
277 
282 typedef struct EtcPalLogTimestamp
283 {
284  unsigned int year;
285  unsigned int month;
286  unsigned int day;
287  unsigned int hour;
288  unsigned int minute;
289  unsigned int second;
290  unsigned int msec;
293 
298 typedef struct EtcPalLogStrings
299 {
301  const char* syslog;
303  const char* legacy_syslog;
305  const char* human_readable;
310  const char* raw;
315  int priority;
317 
337 typedef void (*EtcPalLogCallback)(void* context, const EtcPalLogStrings* strings);
338 
348 typedef void (*EtcPalLogTimeFn)(void* context, EtcPalLogTimestamp* timestamp);
349 
351 #define ETCPAL_LOG_CREATE_HUMAN_READABLE 0x01
353 #define ETCPAL_LOG_CREATE_SYSLOG 0x02
355 #define ETCPAL_LOG_CREATE_LEGACY_SYSLOG 0x04
356 
358 typedef struct EtcPalSyslogParams
359 {
361  int facility;
363  char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]; // NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
365  char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]; // NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
367  char procid[ETCPAL_LOG_PROCID_MAX_LEN]; // NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
369 
379 #define ETCPAL_SYSLOG_PARAMS_INIT \
380  { \
381  0, {'\0'}, {'\0'}, { '\0' } \
382  }
383 
385 typedef struct EtcPalLogParams
386 {
388  int action;
394  int log_mask;
401  void* context;
403 
413 #define ETCPAL_LOG_PARAMS_INIT \
414  { \
415  0, NULL, ETCPAL_SYSLOG_PARAMS_INIT, 0, NULL, NULL \
416  }
417 
418 #ifdef __cplusplus
419 extern "C" {
420 #endif
421 
423 
424 /*
425  * The various compiler ifdefs are to use each compiler's method of checking printf-style
426  * arguments.
427  */
428 
429 #ifdef __ICCARM__
430 #pragma __printf_args
431 #endif
432 bool etcpal_create_log_str(char* buf,
433  size_t buflen,
434  const EtcPalLogTimestamp* timestamp,
435  int pri,
436  const char* format,
437  ...)
438 #ifdef __GNUC__
439  __attribute__((__format__(__printf__, 5, 6)))
440 #endif
441  ;
442 
443 bool etcpal_vcreate_log_str(char* buf,
444  size_t buflen,
445  const EtcPalLogTimestamp* timestamp,
446  int pri,
447  const char* format,
448  va_list args);
449 
450 #ifdef __ICCARM__
451 #pragma __printf_args
452 #endif
453 bool etcpal_create_syslog_str(char* buf,
454  size_t buflen,
455  const EtcPalLogTimestamp* timestamp,
456  const EtcPalSyslogParams* syslog_params,
457  int pri,
458  const char* format,
459  ...)
460 #ifdef __GNUC__
461  __attribute__((__format__(__printf__, 6, 7)))
462 #endif
463  ;
464 
465 bool etcpal_vcreate_syslog_str(char* buf,
466  size_t buflen,
467  const EtcPalLogTimestamp* timestamp,
468  const EtcPalSyslogParams* syslog_params,
469  int pri,
470  const char* format,
471  va_list args);
472 
473 #ifdef __ICCARM__
474 #pragma __printf_args
475 #endif
476 bool etcpal_create_legacy_syslog_str(char* buf,
477  size_t buflen,
478  const EtcPalLogTimestamp* timestamp,
479  const EtcPalSyslogParams* syslog_params,
480  int pri,
481  const char* format,
482  ...)
483 #ifdef __GNUC__
484  __attribute__((__format__(__printf__, 6, 7)))
485 #endif
486  ;
487 
488 bool etcpal_vcreate_legacy_syslog_str(char* buf,
489  size_t buflen,
490  const EtcPalLogTimestamp* timestamp,
491  const EtcPalSyslogParams* syslog_params,
492  int pri,
493  const char* format,
494  va_list args);
495 
499 
500 bool etcpal_can_log(const EtcPalLogParams* params, int pri);
501 
502 #ifdef __ICCARM__
503 #pragma __printf_args
504 #endif
505 void etcpal_log(const EtcPalLogParams* params, int pri, const char* format, ...)
506 #ifdef __GNUC__
507  __attribute__((__format__(__printf__, 3, 4)))
508 #endif
509  ;
510 
511 void etcpal_vlog(const EtcPalLogParams* params, int pri, const char* format, va_list args);
512 
513 #ifdef __cplusplus
514 }
515 #endif
516 
521 #endif /* ETCPAL_LOG_H_ */
etcpal_error_t
A set of error codes that can be returned by library functions.
Definition: error.h:49
#define ETCPAL_LOG_PROCID_MAX_LEN
Max length of the procid param.
Definition: log.h:209
struct EtcPalLogStrings EtcPalLogStrings
The set of log strings passed with a call to an etcpal_log_callback function.
#define ETCPAL_LOG_APP_NAME_MAX_LEN
Max length of the app_name param.
Definition: log.h:208
void etcpal_vlog(const EtcPalLogParams *params, int pri, const char *format, va_list args)
Log a message with the list of format arguments already generated.
Definition: log.c:478
bool etcpal_vcreate_legacy_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format, va_list args)
Create a log a message with RFC 3164 syslog header in the given buffer.
Definition: log.c:349
struct EtcPalLogParams EtcPalLogParams
A set of parameters used for the etcpal_*log() functions.
etcpal_error_t etcpal_init_log_handler(const EtcPalLogParams *log_params)
Register the handler for log messages from EtcPal.
Definition: log.c:164
bool etcpal_create_legacy_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format,...)
Create a log message with RFC 3164 syslog header in the given buffer.
Definition: log.c:314
struct EtcPalSyslogParams EtcPalSyslogParams
A set of parameters for the syslog header.
void etcpal_sanitize_syslog_params(EtcPalSyslogParams *params)
Ensure that the given syslog parameters are compliant with the syslog RFC (modifying them if necessar...
Definition: log.c:373
bool etcpal_validate_log_timestamp(const EtcPalLogTimestamp *timestamp)
Determine whether the given EtcPalLogTimestamp is valid.
Definition: log.c:417
struct EtcPalLogTimestamp EtcPalLogTimestamp
A set of parameters which represent the current local time with millisecond resolution.
bool etcpal_vcreate_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format, va_list args)
Create a log a message with RFC 5424 syslog header in the given buffer.
Definition: log.c:286
bool etcpal_validate_log_params(EtcPalLogParams *params)
Ensure that the given EtcPalLogParams are valid.
Definition: log.c:395
bool etcpal_create_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format,...)
Create a log message with RFC 5424 syslog header in the given buffer.
Definition: log.c:251
void etcpal_log(const EtcPalLogParams *params, int pri, const char *format,...)
Log a message.
Definition: log.c:456
bool etcpal_can_log(const EtcPalLogParams *params, int pri)
Determine whether a priority level can be logged given the mask present in the log params.
Definition: log.c:439
bool etcpal_create_log_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, int pri, const char *format,...)
Create a log message with a human-readable prefix in the given buffer.
Definition: log.c:191
bool etcpal_vcreate_log_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, int pri, const char *format, va_list args)
Create a log a message with a human-readable prefix in the given buffer.
Definition: log.c:224
void(* EtcPalLogTimeFn)(void *context, EtcPalLogTimestamp *timestamp)
Time callback function.
Definition: log.h:348
void(* EtcPalLogCallback)(void *context, const EtcPalLogStrings *strings)
Log callback function.
Definition: log.h:337
#define ETCPAL_LOG_HOSTNAME_MAX_LEN
Max length of the hostname param.
Definition: log.h:207
A set of parameters used for the etcpal_*log() functions.
Definition: log.h:386
EtcPalSyslogParams syslog_params
The syslog header parameters.
Definition: log.h:392
EtcPalLogCallback log_fn
A callback function for the finished log string(s).
Definition: log.h:390
int action
What should be done when etcpal_log() or etcpal_vlog() is called.
Definition: log.h:388
int log_mask
A mask value that determines which priority messages can be logged.
Definition: log.h:394
EtcPalLogTimeFn time_fn
A callback function for the etcpal_log() and etcpal_vlog() functions to obtain the time from the appl...
Definition: log.h:399
void * context
Application context that will be passed back with the log callback function.
Definition: log.h:401
The set of log strings passed with a call to an etcpal_log_callback function.
Definition: log.h:299
const char * human_readable
Log string formatted for readability per ETC convention.
Definition: log.h:305
const char * syslog
Log string formatted compliant to RFC 5424.
Definition: log.h:301
const char * legacy_syslog
Log string formatted compliant to RFC 3164.
Definition: log.h:303
const char * raw
The original log string that was passed to etcpal_log() or etcpal_vlog().
Definition: log.h:310
int priority
The original log priority that was passed to etcpal_log() or etcpal_vlog().
Definition: log.h:315
A set of parameters which represent the current local time with millisecond resolution.
Definition: log.h:283
int utc_offset
The local offset from UTC in minutes.
Definition: log.h:291
unsigned int day
Day of the month.
Definition: log.h:286
unsigned int second
Seconds past the current minute.
Definition: log.h:289
unsigned int minute
Minutes past the current hour.
Definition: log.h:288
unsigned int month
Month of the year.
Definition: log.h:285
unsigned int year
Absolute year.
Definition: log.h:284
unsigned int hour
Hours since midnight.
Definition: log.h:287
unsigned int msec
Milliseconds past the current second.
Definition: log.h:290
A set of parameters for the syslog header.
Definition: log.h:359
int facility
Syslog Facility; see RFC 5424 § 6.2.1.
Definition: log.h:361
char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]
Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:365
char procid[ETCPAL_LOG_PROCID_MAX_LEN]
Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:367
char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]
Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:363