• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aremmell / libsir / 1153

25 Jul 2023 04:30AM UTC coverage: 80.118% (-14.7%) from 94.801%
1153

push

travis-ci

aremmell
fix ternary

1 of 1 new or added line in 1 file covered. (100.0%)

2571 of 3209 relevant lines covered (80.12%)

24530.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.09
/example/example.c
1
/**
2
 * @file example.c
3
 *
4
 * A simple demonstration of initialization, configuration, and basic usage of
5
 * libsir.
6
 *
7
 * @author    Ryan M. Lederman \<lederman@gmail.com\>
8
 * @date      2018-2023
9
 * @version   2.2.1
10
 * @copyright The MIT License (MIT)
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
13
 * this software and associated documentation files (the "Software"), to deal in
14
 * the Software without restriction, including without limitation the rights to
15
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
16
 * the Software, and to permit persons to whom the Software is furnished to do so,
17
 * subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
24
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
25
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
26
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
27
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
 */
29
#include "sir.h"
30
#include "sir/helpers.h"
31

32
int report_error(void);
33

34
/**
35
 * @brief This is a basic example of initializing and configuring libsir for use.
36
 *
37
 * @note You can build this command-line app yourself using `make example` or the
38
 * Visual Studio [Code] project files.
39
 *
40
 * When the program is finished running, you can see the output in the console,
41
 * and examine the contents of 'libsir-example.log' in the current working
42
 * directory.
43
 *
44
 * @returns EXIT_SUCCESS if execution completes successfully, or EXIT_FAILURE
45
 * if an error occurs.
46
 */
47
int main(void) {
29✔
48
    /**
49
     * Instantiate the libsir initialization struct and customize the
50
     * configuration.
51
     *
52
     * NOTE: It is not necessary to retain this structure in memory;
53
     * libsir makes a copy of it before returning from ::sir_init.
54
     */
55
    sirinit si;
56
    if (!sir_makeinit(&si))
29✔
57
        return report_error();
×
58

59
    /* Levels for stdout: send debug, information, warning, and notice there. */
60
    si.d_stdout.levels = SIRL_DEBUG | SIRL_INFO | SIRL_WARN | SIRL_NOTICE;
29✔
61

62
    /* Options for stdout: don't show the timestamp, hostname, or thread ID. */
63
    si.d_stdout.opts = SIRO_NOTIME | SIRO_NOHOST | SIRO_NOTID;
29✔
64

65
    /* Levels for stderr: send error and above there. */
66
    si.d_stderr.levels = SIRL_ERROR | SIRL_CRIT | SIRL_ALERT | SIRL_EMERG;
29✔
67

68
    /* Options for stderr: don't show the timestamp, hostname, or thread ID. */
69
    si.d_stderr.opts = SIRO_NOTIME | SIRO_NOHOST | SIRO_NOTID;
29✔
70

71
    /* Levels for the system logger: don't send any output there. */
72
    si.d_syslog.levels = SIRL_NONE;
29✔
73

74
    /* Options for the system logger: use the default value. */
75
    si.d_syslog.opts = SIRO_DEFAULT;
29✔
76

77
    /* Configure a name to associate with our output. */
78
    static const char* appname = "MyFooServer";
79
    _sir_strncpy(si.name, SIR_MAXNAME, appname, strnlen(appname, SIR_MAXNAME));
29✔
80

81
    /* Initialize libsir. */
82
    if (!sir_init(&si))
29✔
83
        return report_error();
×
84

85
    /*
86
     * Configure and add a log file; don't log the process name or hostname,
87
     * and send all levels there.
88
     */
89
    sirfileid fileid = sir_addfile("libsir-example.log", SIRL_ALL, SIRO_NONAME | SIRO_NOHOST);
29✔
90
    if (0 == fileid)
29✔
91
        report_error();
×
92

93
    /*
94
     * Ready to start logging. The messages passed to sir_debug() will be sent
95
     * to all destinations registered for the ::SIRL_DEBUG level. In our example,
96
     * that is stdout and 'libsir-example.log'.
97
     */
98

99
    /* Notice that it is not necessary to include a newline */
100
    sir_debug("Reading config file %s...", "/usr/local/myapp/myapp.conf");
29✔
101

102
    /* Pretend to read a config file */
103
    sir_debug("Config file successfully parsed; connecting to database...");
29✔
104

105
    /* Pretend to connect to a database */
106
    sir_debug("Database connection established.");
29✔
107
    sir_debug("Binding a TCP socket to interface '%s'"
29✔
108
              " (IPv4: %s) on port %u and listening for connections...",
109
              "eth0", "120.22.140.8", 5500);
110

111
    /*
112
     * Log a message for each of the remaining severity levels. Only up to
113
     * 'notice' will the messages be emitted from stdout; the rest will come from
114
     * stderr.
115
     */
116
    sir_info("MyFooServer v%d.%d.%d (amd64) started successfully in %.2fsec.",
29✔
117
        2, 9, 4, (double)1.94f);
118

119
    sir_notice("Client at %s:%u (username: %s) failed 5 authentication attempts!",
29✔
120
        "210.10.54.3", 43113, "bob");
121

122
    sir_warn("Detected downgraded link speed on %s: last transfer rate: %.1f KiB/s",
29✔
123
        "eth0", (double)219.4f);
124

125
    /* Hmm, what else could go wrong... */
126
    sir_error("Failed to synchronize with node pool.846.myfooserver.io! Error:"
29✔
127
              " %s", "connection reset by peer. Retry in 30sec");
128

129
    /*
130
     * Let's decide we better set up logging to the system logger; things seem
131
     * to be going poorly.
132
     *
133
     * Set up an identity, some options, and register for error and higher.
134
     *
135
     * NOTE: If this platform does not have a supported system logger, or the
136
     * SIR_NO_SYSTEM_LOGGERS preprcessor define was set when libsir was
137
     * compiled, these calls will fail and have no effect.
138
     */
139

140
#if !defined(SIR_NO_SYSTEM_LOGGERS)
141
    if (!sir_syslogid(appname))
23✔
142
        report_error();
×
143

144
    if (!sir_syslogopts(SIRO_NOPID))
23✔
145
        report_error();
×
146

147
    if (!sir_sysloglevels(SIRL_ERROR | SIRL_CRIT | SIRL_EMERG))
23✔
148
        report_error();
×
149
#endif
150

151
    /* Okay, syslog should be configured now. Continue executing. */
152
    sir_crit("Database query failure! Ignoring incoming client requests while"
29✔
153
             " the database is analyzed and repaired...");
154

155
    /* Things just keep getting worse for this poor sysadmin. */
156
    sir_alert("Database repair attempt unsuccessful! Error: %s", "<unknown>");
29✔
157
    sir_emerg("Unable to process client requests for %s! Restarting...", "4m52s");
29✔
158

159
    sir_debug("Begin server shutdown.");
29✔
160
    sir_debug("Exiting with code %d.", 1);
29✔
161

162
    /* Deregister (and close) the log file. */
163
    if (fileid && !sir_remfile(fileid))
29✔
164
        report_error();
×
165

166
    /*
167
     * Now, you can examine the terminal output, libsir-example.log, and
168
     * syslog to ensure that the library operated as expected.
169
     *
170
     * The last thing we have to do is uninitialize libsir by calling sir_cleanup().
171
     */
172
    sir_cleanup();
29✔
173

174
    return EXIT_SUCCESS;
29✔
175
}
176

177
/**
178
 * Prints the last libsir error to stderr.
179
 *
180
 * @return EXIT_FAILURE
181
 */
182
int report_error(void) {
×
183
    char message[SIR_MAXERROR] = {0};
×
184
    uint16_t code              = sir_geterror(message);
×
185
    fprintf(stderr, "libsir error: (%"PRIu16", %s)\n", code, message);
×
186
    return EXIT_FAILURE;
×
187
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc