/* LogService.c generated by valac 0.56.19, the Vala compiler
 * generated from LogService.vala, do not modify */

#include "planify.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.h>
#include <glib-object.h>

#define SERVICES_LOG_SERVICE_MAX_LOG_SIZE ((5 * 1024) * 1024)
#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	SERVICES_LOG_SERVICE_0_PROPERTY,
	SERVICES_LOG_SERVICE_NUM_PROPERTIES
};
static GParamSpec* services_log_service_properties[SERVICES_LOG_SERVICE_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _ServicesLogServicePrivate {
	gchar* log_path;
	GFileOutputStream* stream;
};

static gint ServicesLogService_private_offset;
static gpointer services_log_service_parent_class = NULL;
static ServicesLogService* services_log_service__instance;
static ServicesLogService* services_log_service__instance = NULL;

static void services_log_service_rotate_if_needed (ServicesLogService* self);
static GObject * services_log_service_constructor (GType type,
                                            guint n_construct_properties,
                                            GObjectConstructParam * construct_properties);
static void services_log_service_finalize (GObject * obj);
static GType services_log_service_get_type_once (void);

static inline gpointer
services_log_service_get_instance_private (ServicesLogService* self)
{
	return G_STRUCT_MEMBER_P (self, ServicesLogService_private_offset);
}

gchar*
services_log_service_level_to_string (ServicesLogServiceLevel self)
{
	gchar* result;
	switch (self) {
		case SERVICES_LOG_SERVICE_LEVEL_DEBUG:
		{
			gchar* _tmp0_;
			_tmp0_ = g_strdup ("DEBUG");
			result = _tmp0_;
			return result;
		}
		case SERVICES_LOG_SERVICE_LEVEL_INFO:
		{
			gchar* _tmp1_;
			_tmp1_ = g_strdup ("INFO");
			result = _tmp1_;
			return result;
		}
		case SERVICES_LOG_SERVICE_LEVEL_WARNING:
		{
			gchar* _tmp2_;
			_tmp2_ = g_strdup ("WARNING");
			result = _tmp2_;
			return result;
		}
		case SERVICES_LOG_SERVICE_LEVEL_ERROR:
		{
			gchar* _tmp3_;
			_tmp3_ = g_strdup ("ERROR");
			result = _tmp3_;
			return result;
		}
		default:
		{
			gchar* _tmp4_;
			_tmp4_ = g_strdup ("UNKNOWN");
			result = _tmp4_;
			return result;
		}
	}
}

static GType
services_log_service_level_get_type_once (void)
{
	static const GEnumValue values[] = {{SERVICES_LOG_SERVICE_LEVEL_DEBUG, "SERVICES_LOG_SERVICE_LEVEL_DEBUG", "debug"}, {SERVICES_LOG_SERVICE_LEVEL_INFO, "SERVICES_LOG_SERVICE_LEVEL_INFO", "info"}, {SERVICES_LOG_SERVICE_LEVEL_WARNING, "SERVICES_LOG_SERVICE_LEVEL_WARNING", "warning"}, {SERVICES_LOG_SERVICE_LEVEL_ERROR, "SERVICES_LOG_SERVICE_LEVEL_ERROR", "error"}, {0, NULL, NULL}};
	GType services_log_service_level_type_id;
	services_log_service_level_type_id = g_enum_register_static ("ServicesLogServiceLevel", values);
	return services_log_service_level_type_id;
}

GType
services_log_service_level_get_type (void)
{
	static volatile gsize services_log_service_level_type_id__once = 0;
	if (g_once_init_enter (&services_log_service_level_type_id__once)) {
		GType services_log_service_level_type_id;
		services_log_service_level_type_id = services_log_service_level_get_type_once ();
		g_once_init_leave (&services_log_service_level_type_id__once, services_log_service_level_type_id);
	}
	return services_log_service_level_type_id__once;
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

ServicesLogService*
services_log_service_get_default (void)
{
	ServicesLogService* _tmp0_;
	ServicesLogService* _tmp2_;
	ServicesLogService* _tmp3_;
	ServicesLogService* result;
	_tmp0_ = services_log_service__instance;
	if (_tmp0_ == NULL) {
		ServicesLogService* _tmp1_;
		_tmp1_ = services_log_service_new ();
		_g_object_unref0 (services_log_service__instance);
		services_log_service__instance = _tmp1_;
	}
	_tmp2_ = services_log_service__instance;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	result = _tmp3_;
	return result;
}

static gchar*
string_chomp (const gchar* self)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strchomp (_result_);
	result = _result_;
	return result;
}

static guint8*
string_get_data (const gchar* self,
                 gint* result_length1)
{
	guint8* result;
	guint8* res = NULL;
	gint res_length1;
	gint _res_size_;
	gint _tmp0_;
	gint _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	g_return_val_if_fail (self != NULL, NULL);
	res = (guint8*) self;
	res_length1 = -1;
	_res_size_ = res_length1;
	_tmp0_ = strlen (self);
	_tmp1_ = _tmp0_;
	res_length1 = (gint) _tmp1_;
	_tmp2_ = res;
	_tmp2__length1 = res_length1;
	if (result_length1) {
		*result_length1 = _tmp2__length1;
	}
	result = _tmp2_;
	return result;
}

void
services_log_service_log (ServicesLogService* self,
                          ServicesLogServiceLevel level,
                          const gchar* context,
                          const gchar* message)
{
	gchar* timestamp = NULL;
	GDateTime* _tmp0_;
	GDateTime* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* line = NULL;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gboolean _tmp9_ = FALSE;
	GFileOutputStream* _tmp16_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (message != NULL);
	_tmp0_ = g_date_time_new_now_local ();
	_tmp1_ = _tmp0_;
	_tmp2_ = g_date_time_format (_tmp1_, "%Y-%m-%d %H:%M:%S");
	_tmp3_ = _tmp2_;
	_g_date_time_unref0 (_tmp1_);
	timestamp = _tmp3_;
	_tmp4_ = timestamp;
	_tmp5_ = services_log_service_level_to_string (level);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strdup_printf ("[%s] [%s] [%s] %s\n", _tmp4_, _tmp6_, context, message);
	_tmp8_ = _tmp7_;
	_g_free0 (_tmp6_);
	line = _tmp8_;
	if (CONSTANTS_IS_DEVELOPMENT) {
		_tmp9_ = TRUE;
	} else {
		Util* _tmp10_;
		Util* _tmp11_;
		_tmp10_ = util_get_default ();
		_tmp11_ = _tmp10_;
		_tmp9_ = !util_is_flatpak (_tmp11_);
		_g_object_unref0 (_tmp11_);
	}
	if (_tmp9_) {
		const gchar* _tmp12_;
		_tmp12_ = line;
		g_print ("%s", _tmp12_);
	} else {
		const gchar* _tmp13_;
		gchar* _tmp14_;
		gchar* _tmp15_;
		_tmp13_ = line;
		_tmp14_ = string_chomp (_tmp13_);
		_tmp15_ = _tmp14_;
		g_debug ("LogService.vala:57: %s", _tmp15_);
		_g_free0 (_tmp15_);
	}
	_tmp16_ = self->priv->stream;
	if (_tmp16_ != NULL) {
		{
			GFileOutputStream* _tmp17_;
			const gchar* _tmp18_;
			guint8* _tmp19_;
			gint _tmp19__length1;
			gint _tmp20_ = 0;
			guint8* _tmp21_;
			gint _tmp21__length1;
			GFileOutputStream* _tmp22_;
			_tmp17_ = self->priv->stream;
			_tmp18_ = line;
			_tmp19_ = string_get_data (_tmp18_, &_tmp20_);
			_tmp19__length1 = _tmp20_;
			_tmp21_ = _tmp19_;
			_tmp21__length1 = _tmp19__length1;
			g_output_stream_write ((GOutputStream*) _tmp17_, _tmp21_, (gsize) _tmp21__length1, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
			_tmp22_ = self->priv->stream;
			g_output_stream_flush ((GOutputStream*) _tmp22_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				goto __catch0_g_error;
			}
		}
		goto __finally0;
		__catch0_g_error:
		{
			GError* e = NULL;
			GError* _tmp23_;
			const gchar* _tmp24_;
			e = _inner_error0_;
			_inner_error0_ = NULL;
			_tmp23_ = e;
			_tmp24_ = _tmp23_->message;
			g_warning ("LogService.vala:66: Could not write to log: %s", _tmp24_);
			_g_error_free0 (e);
		}
		__finally0:
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (line);
			_g_free0 (timestamp);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return;
		}
	}
	_g_free0 (line);
	_g_free0 (timestamp);
}

void
services_log_service_debug (ServicesLogService* self,
                            const gchar* context,
                            const gchar* message)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (message != NULL);
	services_log_service_log (self, SERVICES_LOG_SERVICE_LEVEL_DEBUG, context, message);
}

void
services_log_service_info (ServicesLogService* self,
                           const gchar* context,
                           const gchar* message)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (message != NULL);
	services_log_service_log (self, SERVICES_LOG_SERVICE_LEVEL_INFO, context, message);
}

void
services_log_service_warn (ServicesLogService* self,
                           const gchar* context,
                           const gchar* message)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (message != NULL);
	services_log_service_log (self, SERVICES_LOG_SERVICE_LEVEL_WARNING, context, message);
}

void
services_log_service_error (ServicesLogService* self,
                            const gchar* context,
                            const gchar* message)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (message != NULL);
	services_log_service_log (self, SERVICES_LOG_SERVICE_LEVEL_ERROR, context, message);
}

static void
services_log_service_rotate_if_needed (ServicesLogService* self)
{
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	{
		GFile* file = NULL;
		const gchar* _tmp0_;
		GFile* _tmp1_;
		GFile* _tmp2_;
		GFileInfo* info = NULL;
		GFile* _tmp3_;
		GFileInfo* _tmp4_;
		GFileInfo* _tmp5_;
		_tmp0_ = self->priv->log_path;
		_tmp1_ = g_file_new_for_path (_tmp0_);
		file = _tmp1_;
		_tmp2_ = file;
		if (!g_file_query_exists (_tmp2_, NULL)) {
			_g_object_unref0 (file);
			return;
		}
		_tmp3_ = file;
		_tmp4_ = g_file_query_info (_tmp3_, G_FILE_ATTRIBUTE_STANDARD_SIZE, G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
		info = _tmp4_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file);
			goto __catch0_g_error;
		}
		_tmp5_ = info;
		if (g_file_info_get_size (_tmp5_) > ((gint64) SERVICES_LOG_SERVICE_MAX_LOG_SIZE)) {
			gchar* old_path = NULL;
			const gchar* _tmp6_;
			gchar* _tmp7_;
			GFile* old_file = NULL;
			const gchar* _tmp8_;
			GFile* _tmp9_;
			GFile* _tmp10_;
			GFile* _tmp12_;
			GFile* _tmp13_;
			_tmp6_ = self->priv->log_path;
			_tmp7_ = g_strconcat (_tmp6_, ".old", NULL);
			old_path = _tmp7_;
			_tmp8_ = old_path;
			_tmp9_ = g_file_new_for_path (_tmp8_);
			old_file = _tmp9_;
			_tmp10_ = old_file;
			if (g_file_query_exists (_tmp10_, NULL)) {
				GFile* _tmp11_;
				_tmp11_ = old_file;
				g_file_delete (_tmp11_, NULL, &_inner_error0_);
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					_g_object_unref0 (old_file);
					_g_free0 (old_path);
					_g_object_unref0 (info);
					_g_object_unref0 (file);
					goto __catch0_g_error;
				}
			}
			_tmp12_ = file;
			_tmp13_ = old_file;
			g_file_move (_tmp12_, _tmp13_, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (old_file);
				_g_free0 (old_path);
				_g_object_unref0 (info);
				_g_object_unref0 (file);
				goto __catch0_g_error;
			}
			_g_object_unref0 (old_file);
			_g_free0 (old_path);
		}
		_g_object_unref0 (info);
		_g_object_unref0 (file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp14_;
		const gchar* _tmp15_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp14_ = e;
		_tmp15_ = _tmp14_->message;
		g_warning ("LogService.vala:104: Could not rotate log: %s", _tmp15_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
}

gchar*
services_log_service_get_log_path (ServicesLogService* self)
{
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->log_path;
	_tmp1_ = g_strdup (_tmp0_);
	result = _tmp1_;
	return result;
}

ServicesLogService*
services_log_service_construct (GType object_type)
{
	ServicesLogService * self = NULL;
	self = (ServicesLogService*) g_object_new (object_type, NULL);
	return self;
}

ServicesLogService*
services_log_service_new (void)
{
	return services_log_service_construct (SERVICES_TYPE_LOG_SERVICE);
}

static GObject *
services_log_service_constructor (GType type,
                                  guint n_construct_properties,
                                  GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ServicesLogService * self;
	gchar* dir = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	GError* _inner_error0_ = NULL;
	parent_class = G_OBJECT_CLASS (services_log_service_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_LOG_SERVICE, ServicesLogService);
	_tmp0_ = g_get_user_data_dir ();
	_tmp1_ = g_build_filename (_tmp0_, "io.github.alainm23.planify", NULL);
	dir = _tmp1_;
	_tmp2_ = dir;
	_tmp3_ = g_build_filename (_tmp2_, "planify.log", NULL);
	_g_free0 (self->priv->log_path);
	self->priv->log_path = _tmp3_;
	{
		GFile* dir_file = NULL;
		const gchar* _tmp4_;
		GFile* _tmp5_;
		GFile* _tmp6_;
		GFile* file = NULL;
		const gchar* _tmp8_;
		GFile* _tmp9_;
		GFileOutputStream* _tmp10_ = NULL;
		GFile* _tmp11_;
		GFileOutputStream* _tmp12_;
		GFileOutputStream* _tmp13_;
		_tmp4_ = dir;
		_tmp5_ = g_file_new_for_path (_tmp4_);
		dir_file = _tmp5_;
		_tmp6_ = dir_file;
		if (!g_file_query_exists (_tmp6_, NULL)) {
			GFile* _tmp7_;
			_tmp7_ = dir_file;
			g_file_make_directory_with_parents (_tmp7_, NULL, &_inner_error0_);
			if (G_UNLIKELY (_inner_error0_ != NULL)) {
				_g_object_unref0 (dir_file);
				goto __catch0_g_error;
			}
		}
		services_log_service_rotate_if_needed (self);
		_tmp8_ = self->priv->log_path;
		_tmp9_ = g_file_new_for_path (_tmp8_);
		file = _tmp9_;
		_tmp11_ = file;
		_tmp12_ = g_file_append_to (_tmp11_, G_FILE_CREATE_NONE, NULL, &_inner_error0_);
		_tmp10_ = _tmp12_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (file);
			_g_object_unref0 (dir_file);
			goto __catch0_g_error;
		}
		_tmp13_ = _tmp10_;
		_tmp10_ = NULL;
		_g_object_unref0 (self->priv->stream);
		self->priv->stream = _tmp13_;
		_g_object_unref0 (_tmp10_);
		_g_object_unref0 (file);
		_g_object_unref0 (dir_file);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp14_;
		const gchar* _tmp15_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp14_ = e;
		_tmp15_ = _tmp14_->message;
		g_warning ("LogService.vala:46: Could not initialize log file: %s", _tmp15_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (dir);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
	}
	_g_free0 (dir);
	return obj;
}

static void
services_log_service_class_init (ServicesLogServiceClass * klass,
                                 gpointer klass_data)
{
	services_log_service_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ServicesLogService_private_offset);
	G_OBJECT_CLASS (klass)->constructor = services_log_service_constructor;
	G_OBJECT_CLASS (klass)->finalize = services_log_service_finalize;
}

static void
services_log_service_instance_init (ServicesLogService * self,
                                    gpointer klass)
{
	self->priv = services_log_service_get_instance_private (self);
	self->priv->stream = NULL;
}

static void
services_log_service_finalize (GObject * obj)
{
	ServicesLogService * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SERVICES_TYPE_LOG_SERVICE, ServicesLogService);
	_g_free0 (self->priv->log_path);
	_g_object_unref0 (self->priv->stream);
	G_OBJECT_CLASS (services_log_service_parent_class)->finalize (obj);
}

static GType
services_log_service_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ServicesLogServiceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) services_log_service_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ServicesLogService), 0, (GInstanceInitFunc) services_log_service_instance_init, NULL };
	GType services_log_service_type_id;
	services_log_service_type_id = g_type_register_static (G_TYPE_OBJECT, "ServicesLogService", &g_define_type_info, 0);
	ServicesLogService_private_offset = g_type_add_instance_private (services_log_service_type_id, sizeof (ServicesLogServicePrivate));
	return services_log_service_type_id;
}

GType
services_log_service_get_type (void)
{
	static volatile gsize services_log_service_type_id__once = 0;
	if (g_once_init_enter (&services_log_service_type_id__once)) {
		GType services_log_service_type_id;
		services_log_service_type_id = services_log_service_get_type_once ();
		g_once_init_leave (&services_log_service_type_id__once, services_log_service_type_id);
	}
	return services_log_service_type_id__once;
}

