~pitti/autopilot-gtk/gtktextbuffer

« back to all changes in this revision

Viewing changes to lib/main.cpp

  • Committer: Tarmac
  • Author(s): Thomi Richards
  • Date: 2013-02-21 03:15:13 UTC
  • mfrom: (27.1.4 fix-logging)
  • Revision ID: tarmac-20130221031513-r802e3hoydna05ry
Overhaul of the gtk plugin logging system. Fixes: https://bugs.launchpad.net/bugs/1130861.

Approved by PS Jenkins bot, Allan LeSage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include <glib.h>
21
21
#include <gdk/gdk.h>
 
22
#include <string.h>
 
23
#include <fstream>
 
24
#include <iostream>
22
25
 
23
26
#include "main.h"
24
27
#include "Introspection.h"
25
28
#include "IntrospectionService.h"
26
29
 
 
30
namespace
 
31
{
 
32
  std::string filename;
 
33
  GLogLevelFlags levels_to_log;
 
34
 
 
35
  std::ostream& get_log_stream()
 
36
  {
 
37
    if (!filename.empty())
 
38
    {
 
39
      static std::ofstream fstream(filename);
 
40
      return fstream;
 
41
    }
 
42
    else
 
43
    {
 
44
      return std::cout;
 
45
    }
 
46
  }
 
47
 
 
48
  std::string get_level_name(GLogLevelFlags lvl)
 
49
  {
 
50
    switch(lvl)
 
51
    {
 
52
      case G_LOG_LEVEL_DEBUG:
 
53
      return "DEBUG";
 
54
      case G_LOG_LEVEL_INFO:
 
55
      return "INFO";
 
56
      case G_LOG_LEVEL_MESSAGE:
 
57
      return "MESSAGE";
 
58
      case G_LOG_LEVEL_WARNING:
 
59
      return "WARNING";
 
60
      case G_LOG_LEVEL_CRITICAL:
 
61
      return "CRITICAL";
 
62
      case G_LOG_LEVEL_ERROR:
 
63
      return "ERROR";
 
64
      default:
 
65
      return "UNKNOWN";
 
66
    }
 
67
  }
 
68
}
27
69
AutopilotIntrospection* autopilot_introspection = NULL;
28
70
 
29
 
void LogToFile (const gchar* log_domain,
 
71
void LogHandler (const gchar* log_domain,
30
72
                GLogLevelFlags log_level,
31
73
                const gchar* message,
32
74
                gpointer user_data)
33
75
{
34
 
  FILE* logfile = fopen ("autopilot-gtk.log", "a");
35
 
  if (logfile == NULL) {
36
 
    //g_warning("rerouting logger to console");
37
 
    return;
38
 
  }
39
 
  fprintf (logfile, "%s\n", message);
40
 
  fclose (logfile);
 
76
  if (log_level & levels_to_log)
 
77
  {
 
78
    std::string domain = log_domain ? log_domain : "default";
 
79
    get_log_stream() << "[" << domain << "] " << get_level_name(log_level) << ": " << message << std::endl;
 
80
  }
 
81
}
 
82
 
 
83
void initialise_logging()
 
84
{
 
85
  if (getenv("AP_GTK_LOG_VERBOSE"))
 
86
  {
 
87
    levels_to_log = (GLogLevelFlags) (
 
88
      G_LOG_LEVEL_MASK |
 
89
      G_LOG_FLAG_FATAL |
 
90
      G_LOG_FLAG_RECURSION);
 
91
  }
 
92
  else
 
93
  {
 
94
    levels_to_log = (GLogLevelFlags)(
 
95
      G_LOG_LEVEL_WARNING |
 
96
      G_LOG_LEVEL_ERROR |
 
97
      G_LOG_LEVEL_CRITICAL |
 
98
      G_LOG_FLAG_FATAL |
 
99
      G_LOG_FLAG_RECURSION);
 
100
  }
 
101
  char* fname = getenv("AP_GTK_LOG_FILE");
 
102
  if (fname && strcmp(fname, "") != 0)
 
103
  {
 
104
    filename = fname;
 
105
  }
 
106
 
 
107
  g_log_set_default_handler(LogHandler, NULL);
41
108
}
42
109
 
43
110
int gtk_module_init(gint argc, char *argv[]) {
44
 
  //g_log_set_handler (NULL, G_LOG_LEVEL_MASK, LogToFile, NULL);
45
 
  //g_log_set_handler ("GLib", G_LOG_LEVEL_MASK, LogToFile, NULL);
 
111
  initialise_logging();
46
112
  autopilot_introspection = autopilot_introspection_skeleton_new ();
47
113
  g_bus_get (G_BUS_TYPE_SESSION, NULL, bus_acquired, NULL);
 
114
  // always log this:
 
115
  std::cout << "Autopilot GTK interface loaded." << std::endl;
48
116
  return 0;
49
117
}
50
118