~indicator-applet-developers/indicator-applet/applet-0-3

« back to all changes in this revision

Viewing changes to src/applet-main.c

  • Committer: Ted Gould
  • Date: 2010-01-20 04:43:44 UTC
  • mfrom: (338.2.10 log-to-file)
  • Revision ID: ted@gould.cx-20100120044344-1ti7tnvwjkhvrdb9
Make the log output go to a file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
with this program.  If not, see <http://www.gnu.org/licenses/>.
21
21
*/
22
22
 
 
23
#include <stdlib.h>
23
24
#include <config.h>
24
25
#include <panel-applet.h>
25
26
 
73
74
#endif
74
75
 
75
76
/*************
 
77
 * log files
 
78
 * ***********/
 
79
#ifdef INDICATOR_APPLET
 
80
#define LOG_FILE_NAME  "indicator-applet.log"
 
81
#endif
 
82
#ifdef INDICATOR_APPLET_SESSION
 
83
#define LOG_FILE_NAME  "indicator-applet-session.log"
 
84
#endif
 
85
#ifdef INDICATOR_APPLET_COMPLETE
 
86
#define LOG_FILE_NAME  "indicator-applet-complete.log"
 
87
#endif
 
88
GOutputStream * log_file = NULL;
 
89
 
 
90
/*************
76
91
 * init function
77
92
 * ***********/
78
93
 
378
393
#endif
379
394
#define N_(x) x
380
395
 
 
396
static void
 
397
log_to_file_cb (GObject * source_obj, GAsyncResult * result, gpointer user_data)
 
398
{
 
399
        g_free(user_data);
 
400
        return;
 
401
}
 
402
 
 
403
static void
 
404
log_to_file (const gchar * domain, GLogLevelFlags level, const gchar * message, gpointer data)
 
405
{
 
406
        if (log_file == NULL) {
 
407
                GError * error = NULL;
 
408
                gchar * filename = g_build_path(g_get_user_cache_dir(), LOG_FILE_NAME, NULL);
 
409
                GFile * file = g_file_new_for_path(filename);
 
410
                g_free(filename);
 
411
 
 
412
                if (!g_file_test(g_get_user_cache_dir(), G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)) {
 
413
                        GFile * cachedir = g_file_new_for_path(g_get_user_cache_dir());
 
414
                        g_file_make_directory_with_parents(cachedir, NULL, &error);
 
415
 
 
416
                        if (error != NULL) {
 
417
                                g_error("Unable to make directory '%s' for log file: %s", g_get_user_cache_dir(), error->message);
 
418
                                return;
 
419
                        }
 
420
                }
 
421
 
 
422
                g_file_delete(file, NULL, NULL);
 
423
 
 
424
                GFileIOStream * io = g_file_create_readwrite(file,
 
425
                                          G_FILE_CREATE_REPLACE_DESTINATION, /* flags */
 
426
                                          NULL, /* cancelable */
 
427
                                          &error); /* error */
 
428
                if (error != NULL) {
 
429
                        g_error("Unable to replace file: %s", error->message);
 
430
                        return;
 
431
                }
 
432
 
 
433
                log_file = g_io_stream_get_output_stream(G_IO_STREAM(io));
 
434
        }
 
435
        
 
436
        gchar * outputstring = g_strdup_printf("%s\n", message);
 
437
        g_output_stream_write_async(log_file,
 
438
                                    outputstring, /* data */
 
439
                                    strlen(outputstring), /* length */
 
440
                                    G_PRIORITY_LOW, /* priority */
 
441
                                    NULL, /* cancelable */
 
442
                                    log_to_file_cb, /* callback */
 
443
                                    outputstring); /* data */
 
444
 
 
445
        return;
 
446
}
 
447
 
381
448
static gboolean
382
449
applet_fill_cb (PanelApplet * applet, const gchar * iid, gpointer data)
383
450
{
415
482
#ifdef INDICATOR_APPLET_COMPLETE
416
483
                g_set_application_name(_("Indicator Applet Complete"));
417
484
#endif
 
485
 
 
486
                g_log_set_default_handler(log_to_file, NULL);
418
487
        }
419
488
 
420
489
        /* Set panel options */