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