2 * libcryptsetup API log example
4 * Copyright (C) 2011-2021 Red Hat, Inc. All rights reserved.
6 * This file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This file is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this file; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <sys/types.h>
25 #include <libcryptsetup.h>
28 * This is an example of crypt_set_log_callback API callback.
31 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
33 const char *prefix = (const char *)usrptr;
37 case CRYPT_LOG_NORMAL: priority = LOG_NOTICE; break;
38 case CRYPT_LOG_ERROR: priority = LOG_ERR; break;
39 case CRYPT_LOG_VERBOSE: priority = LOG_INFO; break;
40 case CRYPT_LOG_DEBUG: priority = LOG_DEBUG; break;
42 fprintf(stderr, "Unsupported log level requested!\n");
47 syslog(priority, "%s:%s", prefix, msg);
49 syslog(priority, "%s", msg);
54 struct crypt_device *cd;
55 char usrprefix[] = "cslog_example";
59 printf("Using of libcryptsetup requires super user privileges.\n");
63 openlog("cryptsetup", LOG_CONS | LOG_PID, LOG_USER);
65 /* Initialize empty crypt device context */
66 r = crypt_init(&cd, NULL);
68 printf("crypt_init() failed.\n");
72 /* crypt_set_log_callback() - register a log callback for crypt context */
73 crypt_set_log_callback(cd, &simple_syslog_wrapper, (void *)usrprefix);
75 /* send messages ithrough the crypt_log() interface */
76 crypt_log(cd, CRYPT_LOG_NORMAL, "This is normal log message");
77 crypt_log(cd, CRYPT_LOG_ERROR, "This is error log message");
78 crypt_log(cd, CRYPT_LOG_VERBOSE, "This is verbose log message");
79 crypt_log(cd, CRYPT_LOG_DEBUG, "This is debug message");
81 /* release crypt context */
84 /* Initialize default (global) log callback */
85 crypt_set_log_callback(NULL, &simple_syslog_wrapper, NULL);
87 crypt_log(NULL, CRYPT_LOG_NORMAL, "This is normal log message");
88 crypt_log(NULL, CRYPT_LOG_ERROR, "This is error log message");
89 crypt_log(NULL, CRYPT_LOG_VERBOSE, "This is verbose log message");
90 crypt_log(NULL, CRYPT_LOG_DEBUG, "This is debug message");