2 * An example of using logging through libcryptsetup API
4 * Copyright (C) 2011, 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 function that can be registered using crypt_set_log_callback API.
30 * Its prototype is void (*log)(int level, const char *msg, void *usrptr) as defined
31 * in crypt_set_log_callback
33 static void simple_syslog_wrapper(int level, const char *msg, void *usrptr)
35 const char *prefix = (const char *)usrptr;
39 case CRYPT_LOG_NORMAL: priority = LOG_NOTICE; break;
40 case CRYPT_LOG_ERROR: priority = LOG_ERR; break;
41 case CRYPT_LOG_VERBOSE: priority = LOG_INFO; break;
42 case CRYPT_LOG_DEBUG: priority = LOG_DEBUG; break;
44 fprintf(stderr, "Unsupported log level requested!\n");
49 syslog(priority, "%s:%s", prefix, msg);
51 syslog(priority, "%s", msg);
56 struct crypt_device *cd;
57 char usrprefix[] = "cslog_example";
61 printf("Using of libcryptsetup requires super user privileges.\n");
65 openlog("cryptsetup", LOG_CONS | LOG_PID, LOG_USER);
67 /* Initialize empty crypt device context */
68 r = crypt_init(&cd, NULL);
70 printf("crypt_init() failed.\n");
74 /* crypt_set_log_callback() - register a log function for crypt context */
75 crypt_set_log_callback(cd, &simple_syslog_wrapper, (void *)usrprefix);
77 /* send messages ithrough the crypt_log() interface */
78 crypt_log(cd, CRYPT_LOG_NORMAL, "This is normal log message");
79 crypt_log(cd, CRYPT_LOG_ERROR, "This is error log message");
80 crypt_log(cd, CRYPT_LOG_VERBOSE, "This is verbose log message");
81 crypt_log(cd, CRYPT_LOG_DEBUG, "This is debug message");
83 /* release crypt context */
86 /* Initialize default (global) log function */
87 crypt_set_log_callback(NULL, &simple_syslog_wrapper, NULL);
89 crypt_log(NULL, CRYPT_LOG_NORMAL, "This is normal log message");
90 crypt_log(NULL, CRYPT_LOG_ERROR, "This is error log message");
91 crypt_log(NULL, CRYPT_LOG_VERBOSE, "This is verbose log message");
92 crypt_log(NULL, CRYPT_LOG_DEBUG, "This is debug message");