diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 3 | ||||
-rw-r--r-- | src/dkp-csr.c | 42 | ||||
-rw-r--r-- | src/dkp-daemon.c | 48 | ||||
-rw-r--r-- | src/dkp-device-list.c | 6 | ||||
-rw-r--r-- | src/dkp-device.c | 12 | ||||
-rw-r--r-- | src/dkp-hid.c | 22 | ||||
-rw-r--r-- | src/dkp-history.c | 24 | ||||
-rw-r--r-- | src/dkp-supply.c | 20 | ||||
-rw-r--r-- | src/dkp-wup.c | 54 | ||||
-rw-r--r-- | src/egg-debug.c | 283 | ||||
-rw-r--r-- | src/egg-debug.h | 81 | ||||
-rw-r--r-- | src/egg-string.c | 228 | ||||
-rw-r--r-- | src/egg-string.h | 49 | ||||
-rw-r--r-- | src/main.c | 22 | ||||
-rw-r--r-- | src/sysfs-utils.c | 10 |
15 files changed, 779 insertions, 125 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 60c230c..6bfa13b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,6 +10,7 @@ INCLUDES = \ -DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \ -DPACKAGE_LIB_DIR=\""$(libdir)"\" \ -D_POSIX_PTHREAD_SEMANTICS -D_REENTRANT \ + -DEGG_LOG_FILE=\""$(PK_LOG_DIR)/DeviceKit-power"\" \ -I$(top_srcdir)/libdevkit-power \ $(GIO_CFLAGS) \ $(DBUS_GLIB_CFLAGS) \ @@ -42,6 +43,8 @@ dbusifdir = $(datadir)/dbus-1/interfaces dbusif_DATA = org.freedesktop.DeviceKit.Power.xml org.freedesktop.DeviceKit.Power.Device.xml devkit_power_daemon_SOURCES = \ + egg-debug.c \ + egg-debug.h \ dkp-daemon.h dkp-daemon.c \ dkp-device.h dkp-device.c \ dkp-device-list.h dkp-device-list.c \ diff --git a/src/dkp-csr.c b/src/dkp-csr.c index e78b35e..8315ad2 100644 --- a/src/dkp-csr.c +++ b/src/dkp-csr.c @@ -34,7 +34,9 @@ #include <usb.h> #include "sysfs-utils.h" -#include "dkp-debug.h" +#include "egg-debug.h" +#include "egg-string.h" + #include "dkp-enum.h" #include "dkp-object.h" #include "dkp-csr.h" @@ -78,7 +80,7 @@ dkp_csr_poll (DkpCsr *csr) DkpDevice *device = DKP_DEVICE (csr); DkpObject *obj = dkp_device_get_obj (device); - dkp_debug ("Polling: %s", obj->native_path); + egg_debug ("Polling: %s", obj->native_path); ret = dkp_csr_refresh (device); if (ret) dkp_device_emit_changed (device); @@ -98,19 +100,19 @@ dkp_csr_find_device (DkpCsr *csr) dir_name = g_strdup_printf ("%03d", csr->priv->bus_num); filename = g_strdup_printf ("%03d",csr->priv->dev_num); - dkp_debug ("Looking for: [%s][%s]", dir_name, filename); + egg_debug ("Looking for: [%s][%s]", dir_name, filename); for (curr_bus = usb_busses; curr_bus != NULL; curr_bus = curr_bus->next) { - /* dkp_debug ("Checking bus: [%s]", curr_bus->dirname); */ + /* egg_debug ("Checking bus: [%s]", curr_bus->dirname); */ if (g_strcasecmp (dir_name, curr_bus->dirname)) continue; for (curr_device = curr_bus->devices; curr_device != NULL; curr_device = curr_device->next) { - /* dkp_debug ("Checking port: [%s]", curr_device->filename); */ + /* egg_debug ("Checking port: [%s]", curr_device->filename); */ if (g_strcasecmp (filename, curr_device->filename)) continue; - dkp_debug ("Matched device: [%s][%s][%04X:%04X]", curr_bus->dirname, + egg_debug ("Matched device: [%s][%s][%04X:%04X]", curr_bus->dirname, curr_device->filename, curr_device->descriptor.idVendor, curr_device->descriptor.idProduct); @@ -140,24 +142,24 @@ dkp_csr_coldplug (DkpDevice *device) /* detect what kind of device we are */ d = dkp_device_get_d (device); if (d == NULL) - dkp_error ("could not get device"); + egg_error ("could not get device"); /* get the type */ type = devkit_device_get_property (d, "DKP_BATTERY_TYPE"); if (type == NULL) { - dkp_debug ("not a CSR device"); + egg_debug ("not a CSR device"); goto out; } /* which one? */ - if (strcmp (type, "mouse") == 0) + if (egg_strequal (type, "mouse")) obj->type = DKP_DEVICE_TYPE_MOUSE; - if (strcmp (type, "keyboard") == 0) + if (egg_strequal (type, "keyboard")) obj->type = DKP_DEVICE_TYPE_KEYBOARD; /* nothing known */ if (obj->type == DKP_DEVICE_TYPE_UNKNOWN) { - dkp_debug ("not a recognised csr device"); + egg_debug ("not a recognised csr device"); goto out; } @@ -167,14 +169,14 @@ dkp_csr_coldplug (DkpDevice *device) /* get correct bus numbers? */ if (csr->priv->bus_num == 0 || csr->priv->dev_num == 0) { - dkp_warning ("unable to get bus or device numbers"); + egg_warning ("unable to get bus or device numbers"); goto out; } /* try to get the usb device */ csr->priv->device = dkp_csr_find_device (csr); if (csr->priv->device == NULL) { - dkp_debug ("failed to get device %p", csr); + egg_debug ("failed to get device %p", csr); goto out; } @@ -182,7 +184,7 @@ dkp_csr_coldplug (DkpDevice *device) ret = devkit_device_has_property (d, "DKP_CSR_DUAL"); if (ret) csr->priv->is_dual = devkit_device_get_property_as_boolean (d, "DKP_CSR_DUAL"); - dkp_debug ("is_dual=%i", csr->priv->is_dual); + egg_debug ("is_dual=%i", csr->priv->is_dual); obj->vendor = g_strdup (devkit_device_get_property (d, "ID_VENDOR")); obj->model = g_strdup (devkit_device_get_property (d, "ID_PRODUCT")); @@ -228,14 +230,14 @@ dkp_csr_refresh (DkpDevice *device) addr = csr->priv->is_dual ? 1<<8 : 0; if (csr->priv->device == NULL) { - dkp_warning ("no device!"); + egg_warning ("no device!"); return FALSE; } /* open USB device */ handle = usb_open (csr->priv->device); if (handle == NULL) { - dkp_warning ("could not open device"); + egg_warning ("could not open device"); return FALSE; } @@ -243,22 +245,22 @@ dkp_csr_refresh (DkpDevice *device) written = usb_control_msg (handle, 0xc0, 0x09, 0x03|addr, 0x00|addr, buf, 8, DKP_CSR_REFRESH_TIMEOUT); ret = (written == 8); if (!ret) { - dkp_warning ("failed to write to device, wrote %i bytes", written); + egg_warning ("failed to write to device, wrote %i bytes", written); goto out; } /* is a C504 receiver busy? */ if (CSR_P0 == 0x3b && CSR_P4 == 0) { - dkp_debug ("receiver busy"); + egg_debug ("receiver busy"); goto out; } /* get battery status */ csr->priv->raw_value = CSR_P5 & 0x07; - dkp_debug ("charge level: %d", csr->priv->raw_value); + egg_debug ("charge level: %d", csr->priv->raw_value); if (csr->priv->raw_value != 0) { obj->percentage = (100.0 / 7.0) * csr->priv->raw_value; - dkp_debug ("percentage=%f", obj->percentage); + egg_debug ("percentage=%f", obj->percentage); } out: diff --git a/src/dkp-daemon.c b/src/dkp-daemon.c index a31adb9..d1f1baf 100644 --- a/src/dkp-daemon.c +++ b/src/dkp-daemon.c @@ -31,7 +31,9 @@ #include <dbus/dbus-glib-lowlevel.h> #include <devkit-gobject.h> -#include "dkp-debug.h" +#include "egg-debug.h" +#include "egg-string.h" + #include "dkp-daemon.h" #include "dkp-device.h" #include "dkp-supply.h" @@ -379,23 +381,23 @@ gpk_daemon_device_changed (DkpDaemon *daemon, DevkitDevice *d, gboolean synthesi ret = dkp_daemon_get_on_battery_local (daemon); if (ret != daemon->priv->on_battery) { daemon->priv->on_battery = ret; - dkp_debug ("now on_battery = %s", ret ? "yes" : "no"); + egg_debug ("now on_battery = %s", ret ? "yes" : "no"); g_signal_emit (daemon, signals[ON_BATTERY_CHANGED_SIGNAL], 0, ret); } ret = dkp_daemon_get_low_battery_local (daemon); if (ret != daemon->priv->low_battery) { daemon->priv->low_battery = ret; - dkp_debug ("now low_battery = %s", ret ? "yes" : "no"); + egg_debug ("now low_battery = %s", ret ? "yes" : "no"); g_signal_emit (daemon, signals[LOW_BATTERY_CHANGED_SIGNAL], 0, ret); } /* does the device exist in the db? */ device = dkp_device_list_lookup (daemon->priv->list, d); if (device != NULL) { - dkp_debug ("changed %s", dkp_device_get_object_path (device)); + egg_debug ("changed %s", dkp_device_get_object_path (device)); dkp_device_changed (device, d, synthesized); } else { - dkp_debug ("treating change event as add on %s", dkp_device_get_object_path (device)); + egg_debug ("treating change event as add on %s", dkp_device_get_object_path (device)); gpk_daemon_device_add (daemon, d, TRUE); } } @@ -423,7 +425,7 @@ gpk_daemon_device_get (DkpDaemon *daemon, DevkitDevice *d) gboolean ret; subsys = devkit_device_get_subsystem (d); - if (strcmp (subsys, "power_supply") == 0) { + if (egg_strequal (subsys, "power_supply")) { /* are we a valid power supply */ device = DKP_DEVICE (dkp_supply_new ()); @@ -435,7 +437,7 @@ gpk_daemon_device_get (DkpDaemon *daemon, DevkitDevice *d) /* no valid power supply object */ device = NULL; - } else if (strcmp (subsys, "usb") == 0) { + } else if (egg_strequal (subsys, "usb")) { /* see if this is a CSR mouse or keyboard */ device = DKP_DEVICE (dkp_csr_new ()); @@ -463,7 +465,7 @@ gpk_daemon_device_get (DkpDaemon *daemon, DevkitDevice *d) } else { native_path = devkit_device_get_native_path (d); - dkp_warning ("native path %s (%s) ignoring", native_path, subsys); + egg_warning ("native path %s (%s) ignoring", native_path, subsys); } out: return device; @@ -482,14 +484,14 @@ gpk_daemon_device_add (DkpDaemon *daemon, DevkitDevice *d, gboolean emit_event) device = dkp_device_list_lookup (daemon->priv->list, d); if (device != NULL) { /* we already have the device; treat as change event */ - dkp_debug ("treating add event as change event on %s", dkp_device_get_object_path (device)); + egg_debug ("treating add event as change event on %s", dkp_device_get_object_path (device)); gpk_daemon_device_changed (daemon, d, FALSE); } else { /* get the right sort of device */ device = gpk_daemon_device_get (daemon, d); if (device == NULL) { - dkp_debug ("ignoring add event on %s", devkit_device_get_native_path (d)); + egg_debug ("ignoring add event on %s", devkit_device_get_native_path (d)); ret = FALSE; goto out; } @@ -518,7 +520,7 @@ gpk_daemon_device_remove (DkpDaemon *daemon, DevkitDevice *d) /* does device exist in db? */ device = dkp_device_list_lookup (daemon->priv->list, d); if (device == NULL) { - dkp_debug ("ignoring remove event on %s", devkit_device_get_native_path (d)); + egg_debug ("ignoring remove event on %s", devkit_device_get_native_path (d)); } else { dkp_device_removed (device); g_signal_emit (daemon, signals[DEVICE_REMOVED_SIGNAL], 0, @@ -536,17 +538,17 @@ gpk_daemon_device_event_signal_handler (DevkitClient *client, const char *action { DkpDaemon *daemon = DKP_DAEMON (user_data); - if (strcmp (action, "add") == 0) { - dkp_debug ("add %s", devkit_device_get_native_path (device)); + if (egg_strequal (action, "add")) { + egg_debug ("add %s", devkit_device_get_native_path (device)); gpk_daemon_device_add (daemon, device, TRUE); - } else if (strcmp (action, "remove") == 0) { - dkp_debug ("remove %s", devkit_device_get_native_path (device)); + } else if (egg_strequal (action, "remove")) { + egg_debug ("remove %s", devkit_device_get_native_path (device)); gpk_daemon_device_remove (daemon, device); - } else if (strcmp (action, "change") == 0) { - dkp_debug ("change %s", devkit_device_get_native_path (device)); + } else if (egg_strequal (action, "change")) { + egg_debug ("change %s", devkit_device_get_native_path (device)); gpk_daemon_device_changed (daemon, device, FALSE); } else { - dkp_warning ("unhandled action '%s' on %s", action, devkit_device_get_native_path (device)); + egg_warning ("unhandled action '%s' on %s", action, devkit_device_get_native_path (device)); } } @@ -607,7 +609,7 @@ gpk_daemon_register_power_daemon (DkpDaemon *daemon) &dbus_error); if (dbus_error_is_set (&dbus_error)) { - dkp_warning ("Cannot add match rule: %s: %s", dbus_error.name, dbus_error.message); + egg_warning ("Cannot add match rule: %s: %s", dbus_error.name, dbus_error.message); dbus_error_free (&dbus_error); goto error; } @@ -618,7 +620,7 @@ gpk_daemon_register_power_daemon (DkpDaemon *daemon) &dbus_error); if (dbus_error_is_set (&dbus_error)) { - dkp_warning ("Cannot add match rule: %s: %s", dbus_error.name, dbus_error.message); + egg_warning ("Cannot add match rule: %s: %s", dbus_error.name, dbus_error.message); dbus_error_free (&dbus_error); goto error; } @@ -627,14 +629,14 @@ gpk_daemon_register_power_daemon (DkpDaemon *daemon) gpk_daemon_dbus_filter, daemon, NULL)) { - dkp_warning ("Cannot add D-Bus filter: %s: %s", dbus_error.name, dbus_error.message); + egg_warning ("Cannot add D-Bus filter: %s: %s", dbus_error.name, dbus_error.message); goto error; } /* connect to the DeviceKit daemon */ daemon->priv->devkit_client = devkit_client_new (subsystems); if (!devkit_client_connect (daemon->priv->devkit_client, &error)) { - dkp_warning ("Couldn't open connection to DeviceKit daemon: %s", error->message); + egg_warning ("Couldn't open connection to DeviceKit daemon: %s", error->message); g_error_free (error); goto error; } @@ -667,7 +669,7 @@ dkp_daemon_new (void) devices = devkit_client_enumerate_by_subsystem (daemon->priv->devkit_client, subsystems, &error); if (error != NULL) { - dkp_warning ("Cannot enumerate devices: %s", error->message); + egg_warning ("Cannot enumerate devices: %s", error->message); g_error_free (error); g_object_unref (daemon); return NULL; diff --git a/src/dkp-device-list.c b/src/dkp-device-list.c index daa0910..8d41b71 100644 --- a/src/dkp-device-list.c +++ b/src/dkp-device-list.c @@ -25,7 +25,7 @@ #include <stdio.h> #include <glib.h> -#include "dkp-debug.h" +#include "egg-debug.h" #include "dkp-device.h" #include "dkp-device-list.h" @@ -82,7 +82,7 @@ dkp_device_list_insert (DkpDeviceList *list, DevkitDevice *d, DkpDevice *device) g_hash_table_insert (list->priv->map_native_path_to_device, g_strdup (native_path), device); g_ptr_array_add (list->priv->array, device); - dkp_debug ("added %s", native_path); + egg_debug ("added %s", native_path); return TRUE; } @@ -93,7 +93,7 @@ static gboolean dkp_device_list_remove_cb (gpointer key, gpointer value, gpointer user_data) { if (value == user_data) { - dkp_debug ("removed %s", (char *) key); + egg_debug ("removed %s", (char *) key); return TRUE; } return FALSE; diff --git a/src/dkp-device.c b/src/dkp-device.c index 6008ba9..376371d 100644 --- a/src/dkp-device.c +++ b/src/dkp-device.c @@ -35,7 +35,7 @@ #include <polkit-dbus/polkit-dbus.h> #include "sysfs-utils.h" -#include "dkp-debug.h" +#include "egg-debug.h" #include "dkp-supply.h" #include "dkp-device.h" #include "dkp-device.h" @@ -228,7 +228,7 @@ dkp_device_removed (DkpDevice *device) //DkpDeviceClass *klass = DKP_DEVICE_GET_CLASS (device); //klass->removed (device); g_return_if_fail (DKP_IS_DEVICE (device)); - dkp_warning ("do something here?"); + egg_warning ("do something here?"); } /** @@ -522,7 +522,7 @@ dkp_device_emit_changed (DkpDevice *device) { g_return_if_fail (DKP_IS_DEVICE (device)); - dkp_debug ("emitting changed on %s", device->priv->obj->native_path); + egg_debug ("emitting changed on %s", device->priv->obj->native_path); g_signal_emit_by_name (device->priv->daemon, "device-changed", device->priv->object_path, NULL); g_signal_emit (device, signals[CHANGED_SIGNAL], 0); @@ -572,13 +572,13 @@ dkp_device_register_device (DkpDevice *device) gboolean ret = TRUE; device->priv->object_path = dkp_device_compute_object_path (device); - dkp_debug ("object path = %s", device->priv->object_path); + egg_debug ("object path = %s", device->priv->object_path); dbus_g_connection_register_g_object (device->priv->system_bus_connection, device->priv->object_path, G_OBJECT (device)); device->priv->system_bus_proxy = dbus_g_proxy_new_for_name (device->priv->system_bus_connection, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (device->priv->system_bus_proxy == NULL) { - dkp_warning ("proxy invalid"); + egg_warning ("proxy invalid"); ret = FALSE; } return ret; @@ -603,7 +603,7 @@ dkp_device_init (DkpDevice *device) device->priv->system_bus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); if (device->priv->system_bus_connection == NULL) { - dkp_error ("error getting system bus: %s", error->message); + egg_error ("error getting system bus: %s", error->message); g_error_free (error); } } diff --git a/src/dkp-hid.c b/src/dkp-hid.c index 39e43e7..69dfa95 100644 --- a/src/dkp-hid.c +++ b/src/dkp-hid.c @@ -47,7 +47,9 @@ #include <unistd.h> #include "sysfs-utils.h" -#include "dkp-debug.h" +#include "egg-debug.h" +#include "egg-string.h" + #include "dkp-enum.h" #include "dkp-object.h" #include "dkp-hid.h" @@ -111,7 +113,7 @@ dkp_hid_is_ups (DkpHid *hid) /* get device info */ retval = ioctl (hid->priv->fd, HIDIOCGDEVINFO, &device_info); if (retval < 0) { - dkp_debug ("HIDIOCGDEVINFO failed: %s", strerror (errno)); + egg_debug ("HIDIOCGDEVINFO failed: %s", strerror (errno)); goto out; } @@ -137,7 +139,7 @@ dkp_hid_poll (DkpHid *hid) DkpDevice *device = DKP_DEVICE (hid); DkpObject *obj = dkp_device_get_obj (device); - dkp_debug ("Polling: %s", obj->native_path); + egg_debug ("Polling: %s", obj->native_path); ret = dkp_hid_refresh (device); if (ret) dkp_device_emit_changed (device); @@ -162,7 +164,7 @@ dkp_hid_get_string (DkpHid *hid, int sindex) if (ioctl (hid->priv->fd, HIDIOCGSTRING, &sdesc) < 0) return ""; - dkp_debug ("value: '%s'", sdesc.value); + egg_debug ("value: '%s'", sdesc.value); return sdesc.value; } @@ -283,33 +285,33 @@ dkp_hid_coldplug (DkpDevice *device) /* detect what kind of device we are */ d = dkp_device_get_d (device); if (d == NULL) - dkp_error ("could not get device"); + egg_error ("could not get device"); /* get the type */ type = devkit_device_get_property (d, "DKP_BATTERY_TYPE"); - if (type == NULL || strcmp (type, "ups") != 0) { - dkp_debug ("not a UPS device"); + if (type == NULL || !egg_strequal (type, "ups")) { + egg_debug ("not a UPS device"); goto out; } /* get the device file */ device_file = devkit_device_get_device_file (d); if (device_file == NULL) { - dkp_debug ("could not get device file for HID device"); + egg_debug ("could not get device file for HID device"); goto out; } /* connect to the device */ hid->priv->fd = open (device_file, O_RDONLY | O_NONBLOCK); if (hid->priv->fd < 0) { - dkp_debug ("cannot open device file %s", device_file); + egg_debug ("cannot open device file %s", device_file); goto out; } /* first check that we are an UPS */ ret = dkp_hid_is_ups (hid); if (!ret) { - dkp_debug ("not a HID device: %s", device_file); + egg_debug ("not a HID device: %s", device_file); goto out; } diff --git a/src/dkp-history.c b/src/dkp-history.c index 5a16ac6..a5ea62d 100644 --- a/src/dkp-history.c +++ b/src/dkp-history.c @@ -26,7 +26,7 @@ #include <glib/gi18n.h> #include <gio/gio.h> -#include "dkp-debug.h" +#include "egg-debug.h" #include "dkp-history.h" #include "dkp-history-obj.h" @@ -202,7 +202,7 @@ dkp_history_save_data_array (const gchar *filename, GPtrArray *array) /* we failed to convert to string */ if (!ret) { - dkp_warning ("failed to convert"); + egg_warning ("failed to convert"); goto out; } @@ -211,11 +211,11 @@ dkp_history_save_data_array (const gchar *filename, GPtrArray *array) ret = g_file_set_contents (filename, part, -1, &error); // ret = g_file_replace_contents (file, part, -1, NULL, TRUE, G_FILE_CREATE_NONE, NULL, NULL, &error); if (!ret) { - dkp_warning ("failed to set data: %s", error->message); + egg_warning ("failed to set data: %s", error->message); g_error_free (error); goto out; } - dkp_debug ("saved %s", filename); + egg_debug ("saved %s", filename); out: if (file != NULL) @@ -234,7 +234,7 @@ dkp_history_save_data (DkpHistory *history) /* we have an ID? */ if (history->priv->id == NULL) { - dkp_warning ("no ID, cannot save"); + egg_warning ("no ID, cannot save"); return FALSE; } @@ -319,7 +319,7 @@ dkp_history_schedule_save (DkpHistory *history) /* we already have one saved, cancel and reschedule */ if (history->priv->save_id != 0) { - dkp_debug ("deferring as others queued"); + egg_debug ("deferring as others queued"); g_source_remove (history->priv->save_id); history->priv->save_id = g_timeout_add_seconds (DKP_HISTORY_SAVE_INTERVAL, (GSourceFunc) dkp_history_schedule_save_cb, history); @@ -327,7 +327,7 @@ dkp_history_schedule_save (DkpHistory *history) } /* nothing scheduled, do new */ - dkp_debug ("saving in %i seconds", DKP_HISTORY_SAVE_INTERVAL); + egg_debug ("saving in %i seconds", DKP_HISTORY_SAVE_INTERVAL); history->priv->save_id = g_timeout_add_seconds (DKP_HISTORY_SAVE_INTERVAL, (GSourceFunc) dkp_history_schedule_save_cb, history); @@ -352,7 +352,7 @@ dkp_history_load_data_array (const gchar *filename, GPtrArray *array) /* do we exist */ ret = g_file_test (filename, G_FILE_TEST_EXISTS); if (!ret) { - dkp_debug ("failed to get data from %s as file does not exist", filename); + egg_debug ("failed to get data from %s as file does not exist", filename); goto out; } @@ -360,7 +360,7 @@ dkp_history_load_data_array (const gchar *filename, GPtrArray *array) file = g_file_new_for_path (filename); ret = g_file_load_contents (file, NULL, &data, NULL, NULL, &error); if (!ret) { - dkp_warning ("failed to get data: %s", error->message); + egg_warning ("failed to get data: %s", error->message); g_error_free (error); goto out; } @@ -369,12 +369,12 @@ dkp_history_load_data_array (const gchar *filename, GPtrArray *array) parts = g_strsplit (data, "\n", 0); length = g_strv_length (parts); if (length == 0) { - dkp_debug ("no data in %s", filename); + egg_debug ("no data in %s", filename); goto out; } /* add valid entries */ - dkp_debug ("loading %i items of data from %s", length, filename); + egg_debug ("loading %i items of data from %s", length, filename); for (i=0; i<length-1; i++) { obj = dkp_history_obj_from_string (parts[i]); if (obj != NULL) @@ -436,7 +436,7 @@ dkp_history_set_id (DkpHistory *history, const gchar *id) if (id == NULL) return FALSE; - dkp_debug ("using id: %s", id); + egg_debug ("using id: %s", id); history->priv->id = g_strdup (id); /* load all previous data */ ret = dkp_history_load_data (history); diff --git a/src/dkp-supply.c b/src/dkp-supply.c index 9bc5c75..5d199ee 100644 --- a/src/dkp-supply.c +++ b/src/dkp-supply.c @@ -33,7 +33,9 @@ #include <devkit-gobject.h> #include "sysfs-utils.h" -#include "dkp-debug.h" +#include "egg-debug.h" +#include "egg-string.h" + #include "dkp-enum.h" #include "dkp-object.h" #include "dkp-supply.h" @@ -259,7 +261,7 @@ dkp_supply_refresh_battery (DkpSupply *supply) else if (strcasecmp (status, "empty") == 0) state = DKP_DEVICE_STATE_EMPTY; else { - dkp_warning ("unknown status string: %s", status); + egg_warning ("unknown status string: %s", status); state = DKP_DEVICE_STATE_UNKNOWN; } @@ -348,7 +350,7 @@ dkp_supply_poll_battery (DkpSupply *supply) DkpDevice *device = DKP_DEVICE (supply); DkpObject *obj = dkp_device_get_obj (device); - dkp_debug ("No updates on supply %s for 30 seconds; forcing update", obj->native_path); + egg_debug ("No updates on supply %s for 30 seconds; forcing update", obj->native_path); supply->priv->poll_timer_id = 0; ret = dkp_supply_refresh (device); if (ret) @@ -369,13 +371,13 @@ dkp_supply_get_history (DkpDevice *device, const gchar *type, guint timespan) g_return_val_if_fail (type != NULL, FALSE); /* get the correct data */ - if (strcmp (type, "rate") == 0) + if (egg_strequal (type, "rate")) array = dkp_history_get_rate_data (supply->priv->history, timespan); - else if (strcmp (type, "charge") == 0) + else if (egg_strequal (type, "charge")) array = dkp_history_get_charge_data (supply->priv->history, timespan); - else if (strcmp (type, "time-full") == 0) + else if (egg_strequal (type, "time-full")) array = dkp_history_get_time_full_data (supply->priv->history, timespan); - else if (strcmp (type, "time-empty") == 0) + else if (egg_strequal (type, "time-empty")) array = dkp_history_get_time_empty_data (supply->priv->history, timespan); return array; @@ -398,11 +400,11 @@ dkp_supply_coldplug (DkpDevice *device) /* detect what kind of device we are */ d = dkp_device_get_d (device); if (d == NULL) - dkp_error ("could not get device"); + egg_error ("could not get device"); native_path = devkit_device_get_native_path (d); if (native_path == NULL) - dkp_error ("could not get native path"); + egg_error ("could not get native path"); if (sysfs_file_exists (native_path, "online")) { obj->type = DKP_DEVICE_TYPE_LINE_POWER; diff --git a/src/dkp-wup.c b/src/dkp-wup.c index 51aa1e1..6274231 100644 --- a/src/dkp-wup.c +++ b/src/dkp-wup.c @@ -41,7 +41,9 @@ #include <getopt.h> #include "sysfs-utils.h" -#include "dkp-debug.h" +#include "egg-debug.h" +#include "egg-string.h" + #include "dkp-enum.h" #include "dkp-object.h" #include "dkp-wup.h" @@ -77,7 +79,7 @@ dkp_wup_poll (DkpWup *wup) DkpDevice *device = DKP_DEVICE (wup); DkpObject *obj = dkp_device_get_obj (device); - dkp_debug ("Polling: %s", obj->native_path); + egg_debug ("Polling: %s", obj->native_path); ret = dkp_wup_refresh (device); if (ret) dkp_device_emit_changed (device); @@ -95,7 +97,7 @@ dkp_wup_set_speed (DkpWup *wup) retval = tcgetattr (wup->priv->fd, &t); if (retval != 0) { - dkp_debug ("failed to get speed"); + egg_debug ("failed to get speed"); return FALSE; } @@ -108,7 +110,7 @@ dkp_wup_set_speed (DkpWup *wup) t.c_cflag &= ~CSTOPB; retval = tcsetattr (wup->priv->fd, TCSANOW, &t); if (retval != 0) { - dkp_debug ("failed to set speed"); + egg_debug ("failed to set speed"); return FALSE; } @@ -128,10 +130,10 @@ dkp_wup_write_command (DkpWup *wup, const gchar *data) gint length; length = strlen (data); - dkp_debug ("writing [%s]", data); + egg_debug ("writing [%s]", data); retval = write (wup->priv->fd, data, length); if (retval != length) { - dkp_debug ("Writing [%s] to device failed", data); + egg_debug ("Writing [%s] to device failed", data); ret = FALSE; } return ret; @@ -149,10 +151,10 @@ dkp_wup_read_command (DkpWup *wup) gchar buffer[DKP_WUP_COMMAND_LEN]; retval = read (wup->priv->fd, &buffer, DKP_WUP_COMMAND_LEN); if (retval < 0) { - dkp_debug ("failed to read from fd"); + egg_debug ("failed to read from fd"); return NULL; } - dkp_debug ("reading [%s]", buffer); + egg_debug ("reading [%s]", buffer); return g_strdup (buffer); } @@ -190,7 +192,7 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) /* does packet exist? */ if (packet == NULL) { - dkp_debug ("no start char in %s", data); + egg_debug ("no start char in %s", data); goto out; } @@ -207,7 +209,7 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) tokens = g_strsplit (data, ",", -1); length = g_strv_length (tokens); if (length < 3) { - dkp_debug ("not enough tokens '%s'", data); + egg_debug ("not enough tokens '%s'", data); goto out; } @@ -219,11 +221,11 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) /* check the first token */ length_tok = strlen (tokens[0]); if (length_tok != 2) { - dkp_debug ("expected command '#?' but got '%s'", tokens[0]); + egg_debug ("expected command '#?' but got '%s'", tokens[0]); goto out; } if (tokens[0][0] != '#') { - dkp_debug ("expected command '#?' but got '%s'", tokens[0]); + egg_debug ("expected command '#?' but got '%s'", tokens[0]); goto out; } command = tokens[0][1]; @@ -231,7 +233,7 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) /* check the second token */ length_tok = strlen (tokens[1]); if (length_tok != 1) { - dkp_debug ("expected command '?' but got '%s'", tokens[1]); + egg_debug ("expected command '?' but got '%s'", tokens[1]); goto out; } subcommand = tokens[1][0]; /* expect to be '-' */ @@ -239,14 +241,14 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) /* check the length */ size = atoi (tokens[1]); if (size != length - offset) { - dkp_debug ("size expected to be '%i' but got '%i'", length - offset, size); + egg_debug ("size expected to be '%i' but got '%i'", length - offset, size); goto out; } /* print the data */ - dkp_debug ("command=%c:%c", command, subcommand); + egg_debug ("command=%c:%c", command, subcommand); for (i=0; i<size; i++) { - dkp_debug ("%i\t'%s'", i, tokens[i+offset]); + egg_debug ("%i\t'%s'", i, tokens[i+offset]); } /* update the command fields */ @@ -254,7 +256,7 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data) obj->energy_rate = strtod (tokens[offset+DKP_WUP_RESPONSE_HEADER_WATTS], NULL); // obj->volts = strtod (tokens[offset+DKP_WUP_RESPONSE_HEADER_VOLTS], NULL); } else { - dkp_debug ("ignoring command '%c'", command); + egg_debug ("ignoring command '%c'", command); } out: @@ -280,33 +282,33 @@ dkp_wup_coldplug (DkpDevice *device) /* detect what kind of device we are */ d = dkp_device_get_d (device); if (d == NULL) - dkp_error ("could not get device"); + egg_error ("could not get device"); /* get the type */ type = devkit_device_get_property (d, "DKP_MONITOR_TYPE"); - if (type == NULL || strcmp (type, "wup") != 0) { - dkp_debug ("not a Watts Up? Pro device"); + if (type == NULL || !egg_strequal (type, "wup")) { + egg_debug ("not a Watts Up? Pro device"); goto out; } /* get the device file */ device_file = devkit_device_get_device_file (d); if (device_file == NULL) { - dkp_debug ("could not get device file for WUP device"); + egg_debug ("could not get device file for WUP device"); goto out; } /* connect to the device */ wup->priv->fd = open (device_file, O_RDONLY | O_NONBLOCK); if (wup->priv->fd < 0) { - dkp_debug ("cannot open device file %s", device_file); + egg_debug ("cannot open device file %s", device_file); goto out; } /* set speed */ ret = dkp_wup_set_speed (wup); if (!ret) { - dkp_debug ("not a WUP device (cannot set speed): %s", device_file); + egg_debug ("not a WUP device (cannot set speed): %s", device_file); goto out; } @@ -314,7 +316,7 @@ dkp_wup_coldplug (DkpDevice *device) ret = dkp_wup_write_command (wup, DKP_WUP_COMMAND_CLEAR); /* dummy read */ data = dkp_wup_read_command (wup); - dkp_debug ("data after clear %s", data); + egg_debug ("data after clear %s", data); /* shouldn't do anything */ dkp_wup_parse_command (wup, data); @@ -356,14 +358,14 @@ dkp_wup_refresh (DkpDevice *device) /* get data */ data = dkp_wup_read_command (wup); if (data == NULL) { - dkp_debug ("no data"); + egg_debug ("no data"); goto out; } /* parse */ ret = dkp_wup_parse_command (wup, data); if (!ret) { - dkp_debug ("failed to parse %s", data); + egg_debug ("failed to parse %s", data); goto out; } ret = TRUE; diff --git a/src/egg-debug.c b/src/egg-debug.c new file mode 100644 index 0000000..2a37397 --- /dev/null +++ b/src/egg-debug.c @@ -0,0 +1,283 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/** + * SECTION:egg-debug + * @short_description: Debugging functions + * + * This file contains functions that can be used for debugging. + */ + +#include <glib.h> +#include <glib/gi18n.h> +#include <glib/gprintf.h> +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#include <stdlib.h> +#include <signal.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <time.h> +#include <execinfo.h> + +#include "egg-debug.h" + +#define CONSOLE_RESET 0 +#define CONSOLE_BLACK 30 +#define CONSOLE_RED 31 +#define CONSOLE_GREEN 32 +#define CONSOLE_YELLOW 33 +#define CONSOLE_BLUE 34 +#define CONSOLE_MAGENTA 35 +#define CONSOLE_CYAN 36 +#define CONSOLE_WHITE 37 + +static gboolean do_verbose = FALSE; /* if we should print out debugging */ +static gboolean do_logging = FALSE; /* if we should write to a file */ +static gboolean is_console = FALSE; +static gint fd = -1; + +/** + * egg_debug_set_logging: + **/ +void +egg_debug_set_logging (gboolean enabled) +{ + do_logging = enabled; + if (enabled) { + egg_debug ("now logging to %s", EGG_LOG_FILE); + } +} + +/** + * pk_set_console_mode: + **/ +static void +pk_set_console_mode (guint console_code) +{ + gchar command[13]; + + /* don't put extra commands into logs */ + if (!is_console) { + return; + } + /* Command is the control command to the terminal */ + g_snprintf (command, 13, "%c[%dm", 0x1B, console_code); + printf ("%s", command); +} + +/** + * egg_debug_backtrace: + **/ +void +egg_debug_backtrace (void) +{ + void *call_stack[512]; + int call_stack_size; + char **symbols; + int i = 1; + + call_stack_size = backtrace (call_stack, G_N_ELEMENTS (call_stack)); + symbols = backtrace_symbols (call_stack, call_stack_size); + if (symbols != NULL) { + pk_set_console_mode (CONSOLE_RED); + g_print ("Traceback:\n"); + while (i < call_stack_size) { + g_print ("\t%s\n", symbols[i]); + i++; + } + pk_set_console_mode (CONSOLE_RESET); + free (symbols); + } +} + +/** + * pk_log_line: + **/ +static void +pk_log_line (const gchar *buffer) +{ + ssize_t count; + /* open a file */ + if (fd == -1) { + /* ITS4: ignore, /var/log/foo is owned by root, and this is just debug text */ + fd = open (EGG_LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0777); + if (fd == -1) { + g_error ("could not open log: '%s'", EGG_LOG_FILE); + } + } + + /* ITS4: ignore, debug text always NULL terminated */ + count = write (fd, buffer, strlen (buffer)); + if (count == -1) { + g_warning ("could not write %s", buffer); + } + /* newline */ + count = write (fd, "\n", 1); + if (count == -1) { + g_warning ("could not write newline"); + } +} + +/** + * pk_print_line: + **/ +static void +pk_print_line (const gchar *func, const gchar *file, const int line, const gchar *buffer, guint color) +{ + gchar *str_time; + gchar *header; + time_t the_time; + GThread *thread; + + time (&the_time); + str_time = g_new0 (gchar, 255); + strftime (str_time, 254, "%H:%M:%S", localtime (&the_time)); + thread = g_thread_self (); + + /* generate header text */ + header = g_strdup_printf ("TI:%s\tTH:%p\tFI:%s\tFN:%s,%d", str_time, thread, file, func, line); + g_free (str_time); + + /* always in light green */ + pk_set_console_mode (CONSOLE_GREEN); + printf ("%s\n", header); + + /* different colours according to the severity */ + pk_set_console_mode (color); + printf (" - %s\n", buffer); + pk_set_console_mode (CONSOLE_RESET); + + /* log to a file */ + if (do_logging) { + pk_log_line (header); + pk_log_line (buffer); + } + + /* flush this output, as we need to debug */ + fflush (stdout); + + g_free (header); +} + +/** + * egg_debug_real: + **/ +void +egg_debug_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...) +{ + va_list args; + gchar *buffer = NULL; + + if (do_verbose == FALSE) { + return; + } + + va_start (args, format); + g_vasprintf (&buffer, format, args); + va_end (args); + + pk_print_line (func, file, line, buffer, CONSOLE_BLUE); + + g_free(buffer); +} + +/** + * egg_warning_real: + **/ +void +egg_warning_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...) +{ + va_list args; + gchar *buffer = NULL; + + if (do_verbose == FALSE) { + return; + } + + va_start (args, format); + g_vasprintf (&buffer, format, args); + va_end (args); + + /* do extra stuff for a warning */ + if (!is_console) { + printf ("*** WARNING ***\n"); + } + pk_print_line (func, file, line, buffer, CONSOLE_RED); + + g_free(buffer); +} + +/** + * egg_error_real: + **/ +void +egg_error_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...) +{ + va_list args; + gchar *buffer = NULL; + + va_start (args, format); + g_vasprintf (&buffer, format, args); + va_end (args); + + /* do extra stuff for a warning */ + if (!is_console) { + printf ("*** ERROR ***\n"); + } + pk_print_line (func, file, line, buffer, CONSOLE_RED); + g_free(buffer); + + /* we want to fix this! */ + egg_debug_backtrace (); + + exit (1); +} + +/** + * egg_debug_enabled: + * + * Returns: TRUE if we have debugging enabled + **/ +gboolean +egg_debug_enabled (void) +{ + return do_verbose; +} + +/** + * egg_debug_init: + * @debug: If we should print out verbose logging + **/ +void +egg_debug_init (gboolean debug) +{ + do_verbose = debug; + /* check if we are on console */ + if (isatty (fileno (stdout)) == 1) { + is_console = TRUE; + } + egg_debug ("Verbose debugging %i (on console %i)", do_verbose, is_console); +} + diff --git a/src/egg-debug.h b/src/egg-debug.h new file mode 100644 index 0000000..9683846 --- /dev/null +++ b/src/egg-debug.h @@ -0,0 +1,81 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_DEBUG_H +#define __EGG_DEBUG_H + +#include <stdarg.h> +#include <glib.h> + +G_BEGIN_DECLS + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +/** + * egg_debug: + * + * Non critical debugging + */ +#define egg_debug(...) egg_debug_real (__func__, __FILE__, __LINE__, __VA_ARGS__) + +/** + * egg_warning: + * + * Important debugging + */ +#define egg_warning(...) egg_warning_real (__func__, __FILE__, __LINE__, __VA_ARGS__) + +/** + * egg_error: + * + * Critical debugging, with exit + */ +#define egg_error(...) egg_error_real (__func__, __FILE__, __LINE__, __VA_ARGS__) + +#elif defined(__GNUC__) && __GNUC__ >= 3 +#define egg_debug(...) egg_debug_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) +#define egg_warning(...) egg_warning_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) +#define egg_error(...) egg_error_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__) +#else +#define egg_debug(...) +#define egg_warning(...) +#define egg_error(...) +#endif + +void egg_debug_init (gboolean debug); +void egg_debug_set_logging (gboolean enabled); +gboolean egg_debug_enabled (void); +void egg_debug_backtrace (void); +void egg_debug_real (const gchar *func, + const gchar *file, + int line, + const gchar *format, ...) __attribute__((format (printf,4,5))); +void egg_warning_real (const gchar *func, + const gchar *file, + int line, + const gchar *format, ...) __attribute__((format (printf,4,5))); +void egg_error_real (const gchar *func, + const gchar *file, + int line, + const gchar *format, ...) __attribute__((format (printf,4,5))); + +G_END_DECLS + +#endif /* __EGG_DEBUG_H */ diff --git a/src/egg-string.c b/src/egg-string.c new file mode 100644 index 0000000..ef414a3 --- /dev/null +++ b/src/egg-string.c @@ -0,0 +1,228 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/** + * SECTION:pk-common + * @short_description: Common utility functions for PackageKit + * + * This file contains functions that may be useful. + */ + +#include "config.h" + +#include <stdlib.h> +#include <stdio.h> + +#include <string.h> +#include <sys/types.h> +#include <sys/utsname.h> +#include <sys/stat.h> + +#ifdef HAVE_UNISTD_H +#include <unistd.h> +#endif /* HAVE_UNISTD_H */ + +#include <glib.h> + +#include "egg-debug.h" +#include "egg-string.h" + +/** + * egg_strnumber: + * @text: The text the validate + * + * Tests a string to see if it is a number. Both positive and negative numbers + * are allowed. + * + * Return value: %TRUE if the string represents a numeric value + **/ +gboolean +egg_strnumber (const gchar *text) +{ + guint i; + guint length; + + /* check explicitly */ + if (egg_strzero (text)) + return FALSE; + + /* max length is 10 */ + length = egg_strlen (text, 10); + if (length == 10) { + egg_warning ("input too long: %s", text); + return FALSE; + } + + for (i=0; i<length; i++) { + if (i == 0 && text[i] == '-') { + /* negative sign */ + } else if (g_ascii_isdigit (text[i]) == FALSE) { + egg_warning ("not a number '%c' in text!", text[i]); + return FALSE; + } + } + return TRUE; +} + +/** + * egg_strtoint: + * @text: The text the convert + * @value: The return numeric return value, or 0 if invalid. + * + * Converts a string into a signed integer value in a safe way. + * + * Return value: %TRUE if the string was converted correctly + **/ +gboolean +egg_strtoint (const gchar *text, gint *value) +{ + gboolean ret; + ret = egg_strnumber (text); + if (!ret) { + *value = 0; + return FALSE; + } + /* ITS4: ignore, we've already checked for validity */ + *value = atoi (text); + return TRUE; +} + +/** + * egg_strtouint: + * @text: The text the convert + * @value: The return numeric return value, or 0 if invalid. + * + * Converts a string into a unsigned integer value in a safe way. + * + * Return value: %TRUE if the string was converted correctly + **/ +gboolean +egg_strtouint (const gchar *text, guint *value) +{ + gboolean ret; + gint temp; + ret = egg_strtoint (text, &temp); + if (ret == FALSE || temp < 0) { + *value = 0; + return FALSE; + } + *value = (guint) temp; + return TRUE; +} + +/** + * egg_strzero: + * @text: The text to check + * + * This function is a much safer way of doing "if (strlen (text) == 0))" + * as it does not rely on text being NULL terminated. It's also much + * quicker as it only checks the first byte rather than scanning the whole + * string just to verify it's not zero length. + * + * Return value: %TRUE if the string was converted correctly + **/ +gboolean +egg_strzero (const gchar *text) +{ + if (text == NULL) + return TRUE; + if (text[0] == '\0') + return TRUE; + return FALSE; +} + +/** + * egg_strlen: + * @text: The text to check + * @len: The maximum length of the string + * + * This function is a much safer way of doing strlen as it checks for NULL and + * a stupidly long string. + * + * Return value: the length of the string, or len if the string is too long. + **/ +guint +egg_strlen (const gchar *text, guint len) +{ + guint i; + + /* common case */ + if (text == NULL || text[0] == '\0') { + return 0; + } + + /* only count up to len */ + for (i=1; i<len; i++) { + if (text[i] == '\0') + break; + } + return i; +} + +/** + * egg_strequal: + * @id1: the first item of text to test + * @id2: the second item of text to test + * + * This function is a much safer way of doing strcmp as it checks for + * NULL first, and returns boolean TRUE, not zero for success. + * + * Return value: %TRUE if the string are both non-%NULL and the same. + **/ +gboolean +egg_strequal (const gchar *id1, const gchar *id2) +{ + if (id1 == NULL || id2 == NULL) { + egg_debug ("string compare invalid '%s' and '%s'", id1, id2); + return FALSE; + } + return (strcmp (id1, id2) == 0); +} + +/** + * egg_strreplace: + * @text: The input text to make safe + * @find: What to search for + * @replace: What to replace with + * + * Replaces chars in the text with a replacement. + * The %find and %replace variables to not have to be of the same length + * + * Return value: the new string (copied) + **/ +gchar * +egg_strreplace (const gchar *text, const gchar *find, const gchar *replace) +{ + gchar **array; + gchar *retval; + + /* common case, not found */ + if (strstr (text, find) == NULL) { + return g_strdup (text); + } + + /* split apart and rejoin with new delimiter */ + array = g_strsplit (text, find, 0); + retval = g_strjoinv (replace, array); + g_strfreev (array); + return retval; +} + diff --git a/src/egg-string.h b/src/egg-string.h new file mode 100644 index 0000000..5331b4c --- /dev/null +++ b/src/egg-string.h @@ -0,0 +1,49 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- + * + * Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com> + * + * Licensed under the GNU General Public License Version 2 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __EGG_STRING_H +#define __EGG_STRING_H + +#include <glib.h> + +G_BEGIN_DECLS + +guint egg_strlen (const gchar *text, + guint len) + G_GNUC_WARN_UNUSED_RESULT; +gboolean egg_strzero (const gchar *text) + G_GNUC_WARN_UNUSED_RESULT; +gboolean egg_strequal (const gchar *id1, + const gchar *id2) + G_GNUC_WARN_UNUSED_RESULT; +gboolean egg_strnumber (const gchar *text) + G_GNUC_WARN_UNUSED_RESULT; +gboolean egg_strtoint (const gchar *text, + gint *value); +gboolean egg_strtouint (const gchar *text, + guint *value); +gchar *egg_strreplace (const gchar *text, + const gchar *find, + const gchar *replace); + +G_END_DECLS + +#endif /* __EGG_STRING_H */ @@ -41,7 +41,7 @@ #include <dbus/dbus-glib-lowlevel.h> #include <devkit-gobject.h> -#include "dkp-debug.h" +#include "egg-debug.h" #include "dkp-daemon.h" #define NAME_TO_CLAIM "org.freedesktop.DeviceKit.Power" @@ -75,20 +75,20 @@ main_acquire_name_on_proxy (DBusGProxy *bus_proxy) G_TYPE_INVALID); if (!res) { if (error != NULL) { - dkp_warning ("Failed to acquire %s: %s", NAME_TO_CLAIM, error->message); + egg_warning ("Failed to acquire %s: %s", NAME_TO_CLAIM, error->message); g_error_free (error); } else { - dkp_warning ("Failed to acquire %s", NAME_TO_CLAIM); + egg_warning ("Failed to acquire %s", NAME_TO_CLAIM); } goto out; } if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { if (error != NULL) { - dkp_warning ("Failed to acquire %s: %s", NAME_TO_CLAIM, error->message); + egg_warning ("Failed to acquire %s: %s", NAME_TO_CLAIM, error->message); g_error_free (error); } else { - dkp_warning ("Failed to acquire %s", NAME_TO_CLAIM); + egg_warning ("Failed to acquire %s", NAME_TO_CLAIM); } goto out; } @@ -105,7 +105,7 @@ main_acquire_name_on_proxy (DBusGProxy *bus_proxy) static void dkp_main_sigint_handler (int sig) { - dkp_debug ("Handling SIGINT"); + egg_debug ("Handling SIGINT"); /* restore default */ signal (SIGINT, SIG_DFL); @@ -140,12 +140,12 @@ main (int argc, char **argv) g_option_context_add_main_entries (context, entries, NULL); g_option_context_parse (context, &argc, &argv, NULL); g_option_context_free (context); - dkp_debug_init (verbose); + egg_debug_init (verbose); error = NULL; bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); if (bus == NULL) { - dkp_warning ("Couldn't connect to system bus: %s", error->message); + egg_warning ("Couldn't connect to system bus: %s", error->message); g_error_free (error); goto out; } @@ -153,19 +153,19 @@ main (int argc, char **argv) bus_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (bus_proxy == NULL) { - dkp_warning ("Could not construct bus_proxy object; bailing out"); + egg_warning ("Could not construct bus_proxy object; bailing out"); goto out; } if (!main_acquire_name_on_proxy (bus_proxy) ) { - dkp_warning ("Could not acquire name; bailing out"); + egg_warning ("Could not acquire name; bailing out"); goto out; } /* do stuff on ctrl-c */ signal (SIGINT, dkp_main_sigint_handler); - dkp_debug ("Starting devkit-power-daemon version %s", VERSION); + egg_debug ("Starting devkit-power-daemon version %s", VERSION); power_daemon = dkp_daemon_new (); diff --git a/src/sysfs-utils.c b/src/sysfs-utils.c index f7f18eb..c56f422 100644 --- a/src/sysfs-utils.c +++ b/src/sysfs-utils.c @@ -213,19 +213,19 @@ sysfs_resolve_link (const char *dir, const char *attribute) full_path = g_build_filename (dir, attribute, NULL); - //dkp_warning ("attribute='%s'", attribute); - //dkp_warning ("full_path='%s'", full_path); + //egg_warning ("attribute='%s'", attribute); + //egg_warning ("full_path='%s'", full_path); num = readlink (full_path, link_path, sizeof (link_path) - 1); if (num != -1) { char *absolute_path; link_path[num] = '\0'; - //dkp_warning ("link_path='%s'", link_path); + //egg_warning ("link_path='%s'", link_path); absolute_path = g_build_filename (dir, link_path, NULL); - //dkp_warning ("absolute_path='%s'", absolute_path); + //egg_warning ("absolute_path='%s'", absolute_path); if (realpath (absolute_path, resolved_path) != NULL) { - //dkp_warning ("resolved_path='%s'", resolved_path); + //egg_warning ("resolved_path='%s'", resolved_path); found_it = TRUE; } g_free (absolute_path); |