PHP_METHOD(midgard_connection, __destruct)
{
	if (MGDG(midgard_memory_debug)) {
		php_printf("[%p] midgard_connection::__destruct()\n", getThis());
	}

	MidgardConnection *mgd = __midgard_connection_get_ptr(getThis());
	int loghandler = midgard_connection_get_loghandler(mgd);

	if (loghandler) {
		if (MGDG(midgard_memory_debug)) {
			php_printf("[%p] ---> g_log_remove_handler(..., %d)\n", getThis(), loghandler);
		}

		g_log_remove_handler(G_LOG_DOMAIN, loghandler);

		if (global_loghandler != loghandler && MGDG(midgard_memory_debug)) {
			php_printf("[%p] ---> (?) global_loghandler != connection's handler\n", getThis());
		}

		// still just null it, as it is not valid anyway
		global_loghandler = 0;
		midgard_connection_set_loghandler (mgd, 0);
	}

	MGDG(connection_established) = FALSE;

	if (MGDG(midgard_memory_debug)) {
		php_printf("[%p] <= midgard_connection::__destruct()\n", getThis());
	}
}
static PHP_METHOD(midgard_connection, open)
{
	RETVAL_FALSE;
	char *cnf_name;
	int cnf_name_length;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &cnf_name, &cnf_name_length) == FAILURE)
		return;

	MidgardConnection *mgd = __midgard_connection_get_ptr(getThis());
	gboolean rv = midgard_connection_open(mgd, (const gchar *)cnf_name, NULL);

	if (rv) {
		guint loghandler = midgard_connection_get_loghandler(mgd);

		if (loghandler)
			g_log_remove_handler(G_LOG_DOMAIN, loghandler);

		global_loghandler = g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, php_midgard_log_errors, (gpointer)mgd);
		midgard_connection_set_loghandler(mgd, global_loghandler);

		if (MGDG(midgard_memory_debug)) {
			php_printf("---> global_loghandler = %d\n", global_loghandler);
		}
	}

	RETURN_BOOL(rv);
}
/* Object constructor */
PHP_METHOD(midgard_connection, __construct)
{
	MidgardConnection *mgd = NULL;

	if (MGDG(midgard_memory_debug)) {
		php_printf("[%p] midgard_connection::__construct()\n", getThis());
	}

	if (zend_parse_parameters_none() == FAILURE)
		return;

	if (MGDG(midgard_http)) {
		/* trying to reuse saved connection */
		mgd = php_midgard_handle_lookup(&MGDG(midgard_global_holder), MGDG(all_configs) TSRMLS_CC);

		if (mgd == NULL) {
			/* @todo throw exception, instead */
			php_error(E_ERROR, "Midgard handle-lookup failed (could not connect to database)");
			return;
		}
	} else {
		mgd = midgard_connection_new();
		if (mgd == NULL) {
			/* @todo throw exception, instead */
			php_error(E_ERROR, "Failed to create underlying GObject instance");
			return;
		}
	}

	if (global_loghandler) {
		if (MGDG(midgard_memory_debug)) {
			php_printf("---> g_log_remove_handler(..., %d)\n", global_loghandler);
		}
		g_log_remove_handler(G_LOG_DOMAIN, global_loghandler);
	}

	// midgard_connection_set_loglevel(mgd, "warning", NULL);
	global_loghandler = midgard_connection_get_loghandler(mgd);
	if (MGDG(midgard_memory_debug)) {
		php_printf("---> global_loghandler = %d\n", global_loghandler);
	}

	/* storing midgard_connection in object's store-structure */
	MGD_PHP_SET_GOBJECT(getThis(), mgd);

	// explicitly enable replication (to stay compatible with mjölnir)
	midgard_connection_enable_replication(mgd, TRUE);

	if (MGDG(midgard_memory_debug)) {
		php_printf("[%p] <= midgard_connection::__construct()\n", getThis());
	}
}
static PHP_METHOD(midgard_connection, set_loglevel)
{
	RETVAL_NULL();
	MidgardConnection *mgd =__midgard_connection_get_ptr(getThis());
	CHECK_MGD(mgd);

	char *level;
	int level_length;
	zval *callback;

	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &level, &level_length, &callback) == FAILURE)
		return;

	/* no support for callback atm */
	gboolean rv = midgard_connection_set_loglevel(mgd, (gchar *)level, php_midgard_log_errors);
	global_loghandler = midgard_connection_get_loghandler(mgd);

	if (MGDG(midgard_memory_debug)) {
		php_printf("---> global_loghandler = %d\n", global_loghandler);
	}

	RETURN_BOOL(rv);
}
/**
 * midgard_connection_set_loglevel:
 * @self: #MidgardConnection instance
 * @level: Loglevel string
 * @log_func: log handler function
 *
 * Sets log level of the given MidgardConnection.
 * Overwrites internal #MidgardConnection's log level defined in configuration file. 
 * By default MidgardConnection holds loglevel which is associated with ( and duplicated 
 * from ) #MidgardConfig. 
 * #MidgardConfig object's log level isn't changed by this function
 *
 * This method is a shortcut which sets correctly loghandler,loglevel
   and GLib's log function. Default log function will be used if %NULL 
 * is defined. Core's default function is #midgard_error_default_log.
 * 
 * Available levels: error, warn, warning, info, message, debug.
 * warn is default loglevel, SQL queries are logged with debug level.
 * With info level, function names ( and classes' names ) are ( at least should be) logged in language bindings
 *
 * Returns: %TRUE if debug level is set, %FALSE otherwise
 */
gboolean midgard_connection_set_loglevel(
		MidgardConnection *self, const gchar *level, GLogFunc log_func)
{
	g_assert(self != NULL);

	GLogFunc _func = log_func;
	if(_func == NULL)
		_func = midgard_error_default_log;

	gint loglevel = 
		midgard_error_parse_loglevel(level);

	if(loglevel > -1)
		self->priv->loglevel = loglevel;
	else
		return FALSE;

	MGD_CNC_DEBUG (self) = FALSE;

	if (loglevel > G_LOG_LEVEL_DEBUG)
		MGD_CNC_DEBUG (self) = TRUE;

	guint loghandler = midgard_connection_get_loghandler(self);
	if(loghandler)
		g_log_remove_handler(G_LOG_DOMAIN, loghandler);

	loghandler = g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK,
			_func, (gpointer)self);
	
	if(loghandler == 0)
		return FALSE;

	midgard_connection_set_loghandler(self, loghandler);

	return TRUE;
}