xref: /freebsd/contrib/pkgconf/libpkgconf/audit.c (revision a259b98fa211ed87bfee58c575de4e2de94ee0fa)
1 /*
2  * audit.c
3  * package audit log functions
4  *
5  * SPDX-License-Identifier: pkgconf
6  *
7  * Copyright (c) 2016 pkgconf authors (see AUTHORS).
8  *
9  * Permission to use, copy, modify, and/or distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * This software is provided 'as is' and without any warranty, express or
14  * implied.  In no event shall the authors be liable for any damages arising
15  * from the use of this software.
16  */
17 
18 #include <libpkgconf/libpkgconf.h>
19 #include <errno.h>
20 
21 /*
22  * !doc
23  *
24  * libpkgconf `audit` module
25  * =========================
26  *
27  * The libpkgconf `audit` module contains the functions related to attaching an audit log file
28  * to a ``pkgconf_client_t`` object.
29  *
30  * The audit log format is the same as the output generated by the ``PKG_CONFIG_LOG`` environment
31  * variable.
32  */
33 
34 /*
35  * !doc
36  *
37  * .. c:function:: void pkgconf_audit_set_log(pkgconf_client_t *client, FILE *auditf)
38  *
39  *    Sets the audit log file pointer on `client` to `auditf`.
40  *    The callee is responsible for closing any previous log files.
41  *
42  *    :param pkgconf_client_t* client: The client object to modify.
43  *    :param FILE* auditf: The file pointer for the already open log file.
44  *    :return: nothing
45  */
46 void
47 pkgconf_audit_set_log(pkgconf_client_t *client, FILE *auditf)
48 {
49 	client->auditf = auditf;
50 }
51 
52 /*
53  * !doc
54  *
55  * .. c:function:: void pkgconf_audit_log(pkgconf_client_t *client, const char *format, ...)
56  *
57  *    Logs a message to the opened audit log (if any).
58  *
59  *    :param pkgconf_client_t* client: The client object the log message is for.
60  *    :param char* format: The format string to use for the log messages.
61  *    :return: nothing
62  */
63 void
64 pkgconf_audit_log(pkgconf_client_t *client, const char *format, ...)
65 {
66 	va_list va;
67 
68 	if (client->auditf == NULL)
69 		return;
70 
71 	va_start(va, format);
72 	if (!pkgconf_output_file_vfmt(client->auditf, format, va))
73 	{
74 		// TODO: should probably return here
75 		pkgconf_error(client, "Failed to output to file: %s", strerror(errno));
76 	}
77 	va_end(va);
78 }
79 
80 /*
81  * !doc
82  *
83  * .. c:function:: void pkgconf_audit_log_dependency(pkgconf_client_t *client, const pkgconf_pkg_t *dep, const pkgconf_dependency_t *depnode)
84  *
85  *    Convenience function which logs a dependency node to the opened audit log (if any).
86  *
87  *    :param pkgconf_client_t* client: The client object the log message is for.
88  *    :param pkgconf_pkg_t* dep: The dependency package object being logged.
89  *    :param pkgconf_dependency_t* depnode: The dependency object itself being logged.
90  *    :return: nothing
91  */
92 void
93 pkgconf_audit_log_dependency(pkgconf_client_t *client, const pkgconf_pkg_t *dep, const pkgconf_dependency_t *depnode)
94 {
95 	if (client->auditf == NULL)
96 		return;
97 
98 	if (!pkgconf_output_file_fmt(client->auditf, "%s ", dep->id))
99 	{
100 		// TODO: should probably return here
101 		pkgconf_error(client, "Failed to output to file: %s", strerror(errno));
102 	}
103 
104 	if (depnode->version != NULL && depnode->compare != PKGCONF_CMP_ANY)
105 	{
106 		if (!pkgconf_output_file_fmt(client->auditf, "%s %s ", pkgconf_pkg_get_comparator(depnode), depnode->version))
107 		{
108 			// TODO: should probably return here
109 			pkgconf_error(client, "Failed to output to file: %s", strerror(errno));
110 		}
111 	}
112 
113 	if (!pkgconf_output_file_fmt(client->auditf, "[%s]\n", dep->version))
114 	{
115 		// TODO: should probably return here
116 		pkgconf_error(client, "Failed to output to file: %s", strerror(errno));
117 	}
118 }
119