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