#include <json.h>
 
-// Enable legacy logging
-#define PAKFIRE_LEGACY_LOGGING
-
 #include <pakfire/archive.h>
 #include <pakfire/compress.h>
 #include <pakfire/constants.h>
 #include <pakfire/util.h>
 
 struct pakfire_packager {
+       struct pakfire_ctx* ctx;
        struct pakfire* pakfire;
        int nrefs;
+
        time_t time_created;
 
        struct pakfire_package* pkg;
 
        if (packager->filelist)
                pakfire_filelist_unref(packager->filelist);
-       pakfire_package_unref(packager->pkg);
-       pakfire_unref(packager->pakfire);
+       if (packager->pkg)
+               pakfire_package_unref(packager->pkg);
+       if (packager->pakfire)
+               pakfire_unref(packager->pakfire);
+       if (packager->ctx)
+               pakfire_ctx_unref(packager->ctx);
        free(packager);
 }
 
        // Initialize reference counting
        p->nrefs = 1;
 
+       // Store a reference to the context
+       p->ctx = pakfire_ctx(pakfire);
+
        // Store a reference to Pakfire
        p->pakfire = pakfire_ref(pakfire);
 
        // Set distribution
        const char* tag = pakfire_get_distro_tag(p->pakfire);
        if (!tag) {
-               ERROR(p->pakfire, "Distribution tag is not configured: %m\n");
+               CTX_ERROR(p->ctx, "Distribution tag is not configured: %m\n");
                goto ERROR;
        }
 
        // Fetch the hostname
        r = gethostname(hostname, sizeof(hostname));
        if (r) {
-               ERROR(p->pakfire, "Could not determine the hostname: %m\n");
+               CTX_ERROR(p->ctx, "Could not determine the hostname: %m\n");
                goto ERROR;
        }
 
        // Set build host
        r = pakfire_package_set_string(pkg, PAKFIRE_PKG_BUILD_HOST, hostname);
        if (r) {
-               ERROR(p->pakfire, "Could not set the hostname: %s\n", strerror(r));
+               CTX_ERROR(p->ctx, "Could not set the hostname: %s\n", strerror(r));
                goto ERROR;
        }
 
        // Create a new file
        struct archive_entry* entry = pakfire_packager_create_file(packager, filename, size, mode);
        if (!entry) {
-               ERROR(packager->pakfire, "Could not create file '%s'\n", filename);
+               CTX_ERROR(packager->ctx, "Could not create file '%s'\n", filename);
                return 1;
        }
 
        // This is the end of the header
        int r = archive_write_header(a, entry);
        if (r) {
-               ERROR(packager->pakfire, "Error writing header: %s\n", archive_error_string(a));
+               CTX_ERROR(packager->ctx, "Error writing header: %s\n", archive_error_string(a));
                goto ERROR;
        }
 
        // Write content
        r = archive_write_data(a, buffer, strlen(buffer));
        if (r < 0) {
-               ERROR(packager->pakfire, "Error writing data: %s\n", archive_error_string(a));
+               CTX_ERROR(packager->ctx, "Error writing data: %s\n", archive_error_string(a));
                goto ERROR;
        }
 
                struct archive* a) {
        const char buffer[] = TO_STRING(PACKAGE_FORMAT) "\n";
 
-       DEBUG(packager->pakfire, "Writing package format\n");
+       CTX_DEBUG(packager->ctx, "Writing package format\n");
 
        int r = pakfire_packager_write_file_from_buffer(packager, a,
                "pakfire-format", 0444, buffer);
        if (!buffer)
                return 1;
 
-       DEBUG(packager->pakfire, "Generated package metadata:\n%s\n", buffer);
+       CTX_DEBUG(packager->ctx, "Generated package metadata:\n%s\n", buffer);
 
        // Write buffer
        int r = pakfire_packager_write_file_from_buffer(packager, a,
        // Fetch type
        const char* type = pakfire_scriptlet_get_type(scriptlet);
 
-       DEBUG(packager->pakfire, "Writing scriptlet '%s' to package\n", type);
+       CTX_DEBUG(packager->ctx, "Writing scriptlet '%s' to package\n", type);
 
        // Make filename
        r = pakfire_string_format(filename, ".scriptlets/%s", type);
        // Start with the format file
        r = pakfire_packager_write_format(packager, a);
        if (r) {
-               ERROR(packager->pakfire, "Could not add format file to archive: %s\n",
+               CTX_ERROR(packager->ctx, "Could not add format file to archive: %s\n",
                        archive_error_string(a));
                goto ERROR;
        }
        // Write the metadata
        r = pakfire_packager_write_metadata(packager, a);
        if (r) {
-               ERROR(packager->pakfire, "Could not add metadata file to archive: %s\n",
+               CTX_ERROR(packager->ctx, "Could not add metadata file to archive: %s\n",
                        archive_error_string(a));
                goto ERROR;
        }
        for (unsigned int i = 0; i < packager->num_scriptlets; i++) {
                r = pakfire_packager_write_scriptlet(packager, a, packager->scriptlets[i]);
                if (r) {
-                       ERROR(packager->pakfire, "Could not add scriptlet to the archive: %m\n");
+                       CTX_ERROR(packager->ctx, "Could not add scriptlet to the archive: %m\n");
                        goto ERROR;
                }
        }
        // Get the filename of the package
        const char* filename = pakfire_packager_filename(packager);
        if (!filename) {
-               ERROR(packager->pakfire, "Could not generate filename for package: %m\n");
+               CTX_ERROR(packager->ctx, "Could not generate filename for package: %m\n");
                r = 1;
                goto ERROR;
        }
        // Write the finished package
        r = pakfire_packager_finish(packager, f);
        if (r) {
-               ERROR(packager->pakfire, "pakfire_packager_finish() failed: %m\n");
+               CTX_ERROR(packager->ctx, "pakfire_packager_finish() failed: %m\n");
                goto ERROR;
        }
 
        // Move the temporary file to destination
        r = rename(tmppath, path);
        if (r) {
-               ERROR(packager->pakfire, "Could not move %s to %s: %m\n", tmppath, path);
+               CTX_ERROR(packager->ctx, "Could not move %s to %s: %m\n", tmppath, path);
                goto ERROR;
        }
 
-       DEBUG(packager->pakfire, "Package written to %s\n", path);
+       CTX_DEBUG(packager->ctx, "Package written to %s\n", path);
 
        // Store result path if requested
        if (result) {
 
        // Files cannot have an empty path
        if (!path || !*path) {
-               ERROR(packager->pakfire, "Cannot add a file with an empty path\n");
+               CTX_ERROR(packager->ctx, "Cannot add a file with an empty path\n");
                errno = EPERM;
                return 1;
 
        // Hidden files cannot be added
        } else if (*path == '.') {
-               ERROR(packager->pakfire, "Hidden files cannot be added to a package: %s\n", path);
+               CTX_ERROR(packager->ctx, "Hidden files cannot be added to a package: %s\n", path);
                errno = EPERM;
                return 1;
        }
 
-       DEBUG(packager->pakfire, "Adding file to payload: %s\n", path);
+       CTX_DEBUG(packager->ctx, "Adding file to payload: %s\n", path);
 
        // Detect the MIME type
        r = pakfire_file_detect_mimetype(file);
        // Read everything
        r = archive_read_disk_entry_from_file(packager->reader, entry, -1, NULL);
        if (r) {
-               ERROR(packager->pakfire, "Could not read %s: %s\n",
+               CTX_ERROR(packager->ctx, "Could not read %s: %s\n",
                        sourcepath, archive_error_string(packager->reader));
                r = -errno;
                goto ERROR;
        // Create a new file object from the archive entry
        r = pakfire_file_create_from_archive_entry(&file, packager->pakfire, entry);
        if (r < 0) {
-               ERROR(packager->pakfire, "Could not create file object: %s\n", strerror(-r));
+               CTX_ERROR(packager->ctx, "Could not create file object: %s\n", strerror(-r));
                goto ERROR;
        }