Add simple makefile for examples.
[platform/upstream/cryptsetup.git] / docs / examples / crypt_log_usage.c
1 #include <stdio.h>
2 #include <sys/types.h>
3 #include <syslog.h>
4 #include <unistd.h>
5 #include <libcryptsetup.h>
6
7 #include "crypt_examples.h"
8
9 #define LOG_PREFIX_CD   "cslog_example_prefix"
10
11 int log_ready = 0;
12
13 /*
14  * This is an example of function that can be registered using crypt_set_log_callback API.
15  *
16  * Its prototype is void (*log)(int level, const char *msg, void *usrptr) as defined
17  * in crypt_set_log_callback
18  *
19  * NOTE: that some syslog message levels may not be visible with respect to your
20  *       syslog setings
21  *
22  *       If your syslog daemon is turned off, messages should be printed to stderr
23  */
24 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
25 {
26         if(!log_ready) {
27                 openlog((char *)usrptr, LOG_CONS | LOG_PID, LOG_USER);
28                 log_ready = 1;
29         }
30         switch(level) {
31                 case CRYPT_LOG_NORMAL:
32                         syslog(LOG_NOTICE, msg);
33                         break;
34                 case CRYPT_LOG_ERROR:
35                         syslog(LOG_ERR, msg);
36                         break;
37                 case CRYPT_LOG_VERBOSE:
38                         syslog(LOG_INFO, msg);
39                         break;
40                 case CRYPT_LOG_DEBUG:
41                         syslog(LOG_DEBUG, msg);
42                         break;
43                 default:
44                         fprintf(stderr, "Unsupported log level requested!\n");
45         }
46 }
47
48 int main(void)
49 {
50         int step = 0, r = 0;
51         struct crypt_device *cd;
52
53         if (geteuid())
54                 fprintf(stderr, "WARN: Process doesn't have super user privileges. "
55                                 "Most of examples will fail because of that.\n");
56
57         EX_STEP(++step, "crypt_init() to get an empty device context");
58         if ((r = crypt_init(&cd, NULL))) {
59                 EX_FAIL("crypt_init() failed.");
60                 return r;
61         }
62         EX_SUCCESS("crypt_init() successfull");
63
64         EX_STEP(++step, "crypt_set_log_callback() to register a log function tied with context");
65         crypt_set_log_callback(cd, &simple_syslog_wrapper, LOG_PREFIX_CD);
66         EX_SUCCESS("");
67         EX_DELIM;
68
69         EX_STEP(++step, "multiple crypt_log() to send messages into the context set log function. "
70                         "The messages should be prefixed with '" LOG_PREFIX_CD "'");
71         crypt_log(cd, CRYPT_LOG_NORMAL, "This is normal log message");
72         crypt_log(cd, CRYPT_LOG_ERROR, "This is error log message");
73         crypt_log(cd, CRYPT_LOG_VERBOSE, "This is verbose log message");
74         crypt_log(cd, CRYPT_LOG_DEBUG, "This is debug message");
75         EX_SUCCESS("");
76         EX_DELIM;
77
78         crypt_free(cd);
79
80         if (log_ready)
81                 closelog();
82
83         log_ready = 0;
84
85         EX_STEP(++step, "crypt_set_log_callback() to register a default (global) log function");
86         crypt_set_log_callback(NULL, &simple_syslog_wrapper, NULL);
87         EX_SUCCESS("");
88         EX_DELIM;
89
90         EX_STEP(++step, "multiple crypt_log() to send messages into default log");
91         crypt_log(NULL, CRYPT_LOG_NORMAL, "This is normal log message");
92         crypt_log(NULL, CRYPT_LOG_ERROR, "This is error log message");
93         crypt_log(NULL, CRYPT_LOG_VERBOSE, "This is verbose log message");
94         crypt_log(NULL, CRYPT_LOG_DEBUG, "This is debug message");
95         EX_SUCCESS("");
96
97         return r;
98 }