# #
#############################################################################*/
+#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
#include <pakfire/ctx.h>
#include <pakfire/private.h>
struct pakfire_ctx {
// Reference counter
int nrefs;
+
+ // Logging
+ struct pakfire_ctx_log {
+ int level;
+ } log;
};
+static int parse_log_level(const char* level) {
+ char* end = NULL;
+
+ int l = strtol(level, &end, 10);
+
+ if (*end == '\0' || isspace(*end))
+ return l;
+
+ if (strcmp(level, "error") == 0)
+ return LOG_ERR;
+
+ if (strcmp(level, "info") == 0)
+ return LOG_INFO;
+
+ if (strcmp(level, "debug") == 0)
+ return LOG_DEBUG;
+
+ return 0;
+}
+
+static int pakfire_ctx_setup_logging(struct pakfire_ctx* ctx) {
+ const char* env = NULL;
+
+ // Set the default level to INFO
+ int level = LOG_INFO;
+
+ // Try setting the log level from the environment
+ env = secure_getenv("PAKFIRE_LOG");
+ if (env)
+ level = parse_log_level(env);
+
+ // Store the log level
+ pakfire_ctx_set_log_level(ctx, level);
+
+ return 0;
+}
+
static void pakfire_ctx_free(struct pakfire_ctx* ctx) {
free(ctx);
}
PAKFIRE_EXPORT int pakfire_ctx_create(struct pakfire_ctx** ctx) {
struct pakfire_ctx* c = NULL;
+ int r;
// Allocate the context
c = calloc(1, sizeof(*c));
// Initialize the reference counter
c->nrefs = 1;
+ // Setup logging
+ r = pakfire_ctx_setup_logging(c);
+ if (r)
+ goto ERROR;
+
// Return the pointer
*ctx = c;
return 0;
+
+ERROR:
+ if (c)
+ pakfire_ctx_free(c);
+
+ return r;
}
PAKFIRE_EXPORT struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx) {
pakfire_ctx_free(ctx);
return NULL;
}
+
+// Logging
+
+PAKFIRE_EXPORT int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx) {
+ return ctx->log.level;
+}
+
+PAKFIRE_EXPORT void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level) {
+ ctx->log.level = level;
+}
struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx);
struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx);
+// Logging
+
+int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx);
+void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level);
+
#endif /* PAKFIRE_CTX_H */
if (r)
goto ERROR;
+ // Set the log level to DEBUG
+ pakfire_ctx_set_log_level(ctx, LOG_DEBUG);
+
// Create a pakfire instance
r = pakfire_create(&pakfire, ctx, root, NULL, NULL,
PAKFIRE_FLAGS_DEBUG, pakfire_log_stderr, NULL);