libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
syslog.ipp
Go to the documentation of this file.
1/* Copyright 2025 NTESS. See the top-level LICENSE.txt file for details.
2 *
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#define SYSLOG_NAMES
6#include <syslog.h>
9#include <cstdlib>
10
11namespace adc {
12
13using std::cout;
14
15typedef std::string string;
16typedef std::string_view string_view;
17
18
19#define PRIORITY_UNSET_ADC_SYSLOG -1
20
22{
23 int i = 0;
24 while (prioritynames[i].c_name &&
25 strcmp(prioritynames[i].c_name,s.c_str()))
26 i++;
27 if (!prioritynames[i].c_name)
28 return LOG_INFO;
29 else
30 return prioritynames[i].c_val;
31}
32
33
34/*! \brief syslog publisher_api implementation.
35 This plugin sends messages to syslog synchronously.
36 Multiple independent instances of this plugin may be used simultaneously,
37 but the underlying syslog setting (priority) will be that
38 of the most recently created instance unless a map with "PRIORITY" is supplied
39 to config().
40
41 If not configured, the default priority is LOG_INFO.
42 env("ADC_SYSLOG_PLUGIN_PRIORITY") overrides the default
43 if defined with a priority string from
44 /usr/include/sys/syslog.h.
45 */
47
48private:
49 inline static const std::map< const string, const string > plugin_syslog_config_defaults =
50 {{ "PRIORITY", "info"} };
51 inline static const char *plugin_prefix = "ADC_SYSLOG_PLUGIN_";
52 const string vers;
53 const std::vector<string> tags;
54 int priority;
55 bool paused;
56
57 int config(string p) {
58 priority = get_priority_from_string(std::move(p));
59 return 0;
60 }
61
62 // find field in m, then with prefix in env, then the default.
63 const string get(const std::map< string, string >& m,
64 string field, string_view env_prefix) {
65 // fields not defined in config_defaults raise an exception.
66 auto it = m.find(field);
67 if (it != m.end()) {
68 return it->second;
69 }
70 string en = std::string(env_prefix) += field;
71 char *ec = getenv(en.c_str());
72 if (!ec) {
73 return plugin_syslog_config_defaults.at(field);
74 } else {
75 return string(ec);
76 }
77 }
78
79public:
80 syslog_plugin() : vers("1.0.0") , tags({"none"}), priority(PRIORITY_UNSET_ADC_SYSLOG), paused(false) {
81 openlog("ADC", LOG_PID, LOG_USER);
82
83 }
84
85 int publish(std::shared_ptr<builder_api> b) {
86 if (paused)
87 return 0;
88 if (priority == PRIORITY_UNSET_ADC_SYSLOG)
89 priority = LOG_INFO;
90 auto str = b->serialize();
91 syslog(priority, "%s", str.c_str());
92 return 0;
93 }
94
95 int config(const std::map< std::string, std::string >& m) {
96 return config(m, plugin_prefix);
97 }
98
99 int config(const std::map< std::string, std::string >& m, std::string_view env_prefix) {
100 string p = get(m, "PRIORITY", env_prefix);
101 return config(std::move(p));
102 }
103
104 const std::map< const std::string, const std::string> & get_option_defaults() {
105 return plugin_syslog_config_defaults;
106 }
107
109 std::map <string, string >m;
110 if (priority == PRIORITY_UNSET_ADC_SYSLOG)
111 config(m);
112 return 0;
113 }
114
115 void finalize() {
116 }
117
118 void pause() {
119 paused = true;
120 }
121
122 void resume() {
123 paused = false;
124 }
125
127 return "syslog";
128 }
129
131 return vers;
132 }
133
135 closelog();
136 }
137};
138
139} // adc
Publisher plugin interface.
Definition publisher.hpp:48
syslog publisher_api implementation. This plugin sends messages to syslog synchronously....
Definition syslog.ipp:46
int publish(std::shared_ptr< builder_api > b)
Publish the content of the builder.
Definition syslog.ipp:85
string_view name() const
Definition syslog.ipp:126
string_view version() const
Definition syslog.ipp:130
const std::map< const std::string, const std::string > & get_option_defaults()
Look up the settable options and their defaults.
Definition syslog.ipp:104
void finalize()
Stop publishing and release any resources held for managing publication.
Definition syslog.ipp:115
void pause()
Pause publishing until a call to resume. Duplicate calls are allowed.
Definition syslog.ipp:118
int initialize()
Ready the plugin to publish following the configuration options set or defaulted.
Definition syslog.ipp:108
int config(const std::map< std::string, std::string > &m, std::string_view env_prefix)
Configure the plugin with the options given and the corresponding environment variables.
Definition syslog.ipp:99
void resume()
Resume publishing Duplicate calls are allowed.
Definition syslog.ipp:122
int config(const std::map< std::string, std::string > &m)
Configure the plugin with the options given.
Definition syslog.ipp:95
Definition adc.hpp:82
std::string_view string_view
Definition curl.ipp:18
int get_priority_from_string(string s)
Definition syslog.ipp:21
std::string string
Definition curl.ipp:17
#define PRIORITY_UNSET_ADC_SYSLOG
Definition syslog.ipp:19