1 #include <libcryptsetup.h>
7 #include "crypt_examples.h"
9 #define LOG_PREFIX_CD "cslog_example_prefix"
14 * This is an example of function that can be registered using crypt_set_log_callback API.
16 * Its prototype is void (*log)(int level, const char *msg, void *usrptr) as defined
17 * in crypt_set_log_callback
19 * NOTE: that some syslog message levels may not be visible with respect to your
22 * If your syslog daemon is turned off, messages should be printed to stderr
24 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
27 openlog((char *)usrptr, LOG_CONS | LOG_PID, LOG_USER);
31 case CRYPT_LOG_NORMAL:
32 syslog(LOG_NOTICE, msg);
37 case CRYPT_LOG_VERBOSE:
38 syslog(LOG_INFO, msg);
41 syslog(LOG_DEBUG, msg);
44 fprintf(stderr, "Unsupported log level requested!\n");
51 struct crypt_device *cd;
54 fprintf(stderr, "WARN: Process doesn't have super user privileges. "
55 "Most of examples will fail because of that.\n");
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.");
62 EX_SUCCESS("crypt_init() successfull");
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);
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");
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);
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");