Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
nix/afw_os_log.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Implementation of afw_log interface for *nix systems
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw.h"
15 #include "afw_log_impl.h"
16 
17 /* Declares and rti/inf defines for interface afw_log_factory and afw_log. */
18 #define AFW_IMPLEMENTATION_ID "syslog"
20 #include "afw_log_impl_declares.h"
21 #include "afw_log_impl.h"
22 
23 #include <syslog.h>
24 
25 static const int afw_syslog_map[] = {
26  [afw_log_priority_emerg] = LOG_EMERG,
27  [afw_log_priority_alert] = LOG_ALERT,
28  [afw_log_priority_crit] = LOG_CRIT,
29  [afw_log_priority_err] = LOG_ERR,
30  [afw_log_priority_warning] = LOG_WARNING,
31  [afw_log_priority_notice] = LOG_NOTICE,
32  [afw_log_priority_info] = LOG_INFO,
33  [afw_log_priority_debug] = LOG_DEBUG,
34  [afw_log_priority_trace1] = LOG_DEBUG,
35  [afw_log_priority_trace2] = LOG_DEBUG,
36  [afw_log_priority_trace3] = LOG_DEBUG,
37  [afw_log_priority_trace4] = LOG_DEBUG,
38  [afw_log_priority_trace5] = LOG_DEBUG,
39  [afw_log_priority_trace6] = LOG_DEBUG,
40  [afw_log_priority_trace7] = LOG_DEBUG,
41  [afw_log_priority_trace8] = LOG_DEBUG
42 };
43 
44 /* Typedef for afw_log self. */
45 typedef struct impl_afw_os_log_self_s {
46  afw_log_t pub;
47 
48  /* Private implementation variables */
49 
51 
52 
53 static const afw_utf8_t impl_factory_description =
54 AFW_UTF8_LITERAL("Log type for writing to syslog.");
55 
56 /* Factory singleton instance. */
57 static const afw_log_factory_t impl_afw_log_factory =
58 {
59  &impl_afw_log_factory_inf,
61  &impl_factory_description
62 };
63 
64 
65 /* Get the factory for log type file. */
68 {
69  return &impl_afw_log_factory;
70 }
71 
72 
73 /* Create for os_log */
74 AFW_DEFINE(const afw_log_t *)
76  const afw_object_t *properties,
77  const afw_pool_t *p, afw_xctx_t *xctx)
78 {
80 
82  &impl_afw_log_inf, sizeof(impl_afw_os_log_self_t),
83  properties, p, xctx);
84  int facility = LOG_DAEMON;
85  char *identifier = NULL;
86 
87  /* FIXME Finish processing parameters implementation specific parameters. */
88 
89  /* open for logging */
90  openlog(identifier, LOG_PID, facility);
91 
92  /* Return new instance. */
93  return (afw_log_t *)self;
94 }
95 
96 
97 
98 /*
99  * Implementation of method create_log_cede_p of interface afw_log_factory.
100  */
101 const afw_log_t *
102 impl_afw_log_factory_create_log_cede_p (
103  const afw_log_factory_t * instance,
104  const afw_object_t * properties,
105  const afw_pool_t * p,
106  afw_xctx_t *xctx)
107 {
108  return afw_os_log_create(properties, p, xctx);
109 }
110 
111 
112 
113 /*
114  * Implementation of method destroy of interface afw_log.
115  */
116 void
117 impl_afw_log_destroy(
118  const afw_log_t * instance,
119  afw_xctx_t *xctx)
120 {
121  /*
122  If you are writing shared library code that uses openlog to
123  generate custom syslog output, you should use closelog to drop
124  the GNU C Library’s internal reference to the ident pointer
125  when you are done.
126  */
127  closelog();
128 
129  afw_pool_release(instance->p, xctx);
130 }
131 
132 
133 
134 /*
135  * Implementation of method set_mask of interface afw_log.
136  */
137 void
138 impl_afw_log_set_own_mask(
139  const afw_log_t * instance,
141  afw_xctx_t *xctx)
142 {
143  afw_log_impl_t *impl = (afw_log_impl_t *)instance->impl;
144 
145  /* Set mask. */
146  impl->mask = mask;
147 }
148 
149 
150 
151 /*
152  * Implementation of method write of interface afw_log.
153  */
154 void
156  const afw_log_t * instance,
157  afw_log_priority_t priority,
158  const afw_utf8_z_t * source_z,
159  const afw_utf8_t * message,
160  afw_xctx_t *xctx)
161 {
162  int syslog_priority;
163 
164  syslog_priority = afw_syslog_map[priority];
165 
166  syslog(syslog_priority, "%" AFW_UTF8_FMT, AFW_UTF8_FMT_ARG(message));
167 }
Adaptive Framework Core API.
AFW_DEFINE(const afw_object_t *)
Interface afw_interface implementation declares.
Helpers for log implementation development.
Interface afw_interface implementation declares.
#define AFW_IMPLEMENTATION_ID
#define AFW_UTF8_FMT_ARG(A_STRING)
Convenience Macro for use with AFW_UTF8_FMT to specify arg.
Definition: afw_common.h:605
#define AFW_UTF8_LITERAL(A_STRING)
String literal initializer.
Definition: afw_common.h:582
int afw_log_priority_mask_t
Definition: afw_common.h:659
#define AFW_UTF8_FMT
Format string specifier used for afw_utf8_t.
Definition: afw_common.h:588
afw_utf8_octet_t afw_utf8_z_t
NFC normalized UTF-8 null terminated string.
Definition: afw_common.h:523
enum afw_log_priority_e afw_log_priority_t
Log levels. See afw_log.h for more information.
@ afw_log_priority_alert
Definition: afw_common.h:983
@ afw_log_priority_trace3
Definition: afw_common.h:996
@ afw_log_priority_crit
Definition: afw_common.h:984
@ afw_log_priority_trace8
Definition: afw_common.h:1001
@ afw_log_priority_debug
Definition: afw_common.h:989
@ afw_log_priority_trace4
Definition: afw_common.h:997
@ afw_log_priority_notice
Definition: afw_common.h:987
@ afw_log_priority_emerg
Definition: afw_common.h:982
@ afw_log_priority_trace2
Definition: afw_common.h:995
@ afw_log_priority_trace5
Definition: afw_common.h:998
@ afw_log_priority_err
Definition: afw_common.h:985
@ afw_log_priority_trace7
Definition: afw_common.h:1000
@ afw_log_priority_trace6
Definition: afw_common.h:999
@ afw_log_priority_info
Definition: afw_common.h:988
@ afw_log_priority_warning
Definition: afw_common.h:986
@ afw_log_priority_trace1
Definition: afw_common.h:994
void impl_afw_log_write(const afw_log_t *instance, afw_log_priority_t priority, const afw_utf8_z_t *source_z, const afw_utf8_t *message, afw_xctx_t *xctx)
afw_log_impl_create_cede_p(const afw_log_inf_t *inf, afw_size_t instance_size, const afw_object_t *properties, const afw_pool_t *p, afw_xctx_t *xctx)
Developers should call this in all create functions for afw_log.
Definition: afw_log.c:769
afw_os_log_create(const afw_object_t *properties, const afw_pool_t *p, afw_xctx_t *xctx)
Create an instance of the OS log.
afw_os_log_factory_get()
Get the factory for OS log.
#define afw_pool_release(instance, xctx)
Call method release of interface afw_pool.
Interface afw_log_factory public struct.
Struct for afw_log_impl_t.
Definition: afw_log_impl.h:35
afw_log_priority_mask_t mask
Definition: afw_log_impl.h:53
Interface afw_log public struct.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_xctx public struct.