"Initial commit to Gerrit"
[profile/ivi/cogl.git] / cogl / cogl-profile.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #ifdef COGL_ENABLE_PROFILE
6
7 #include "cogl-profile.h"
8 #include "cogl-debug.h"
9
10 #include <glib/gi18n-lib.h>
11 #include <stdlib.h>
12
13 UProfContext *_cogl_uprof_context;
14
15 static gboolean
16 debug_option_getter (void *user_data)
17 {
18   unsigned int shift = GPOINTER_TO_UINT (user_data);
19   return COGL_DEBUG_ENABLED (shift);
20 }
21
22 static void
23 debug_option_setter (gboolean value, void *user_data)
24 {
25   unsigned int shift = GPOINTER_TO_UINT (user_data);
26
27   if (value)
28     COGL_DEBUG_SET_FLAG (shift);
29   else
30     COGL_DEBUG_CLEAR_FLAG (shift);
31 }
32
33 static void
34 print_exit_report (void)
35 {
36   if (getenv ("COGL_PROFILE_OUTPUT_REPORT"))
37     {
38       UProfReport *report = uprof_report_new ("Cogl report");
39       uprof_report_add_context (report, _cogl_uprof_context);
40       uprof_report_print (report);
41       uprof_report_unref (report);
42     }
43   uprof_context_unref (_cogl_uprof_context);
44 }
45
46 void
47 _cogl_uprof_init (void)
48 {
49   _cogl_uprof_context = uprof_context_new ("Cogl");
50 #define OPT(MASK_NAME, GROUP, NAME, NAME_FORMATTED, DESCRIPTION) \
51   G_STMT_START { \
52     int shift = COGL_DEBUG_ ## MASK_NAME; \
53     uprof_context_add_boolean_option (_cogl_uprof_context, \
54                                       g_dgettext (GETTEXT_PACKAGE, GROUP), \
55                                       NAME, \
56                                       g_dgettext (GETTEXT_PACKAGE, \
57                                                   NAME_FORMATTED), \
58                                       g_dgettext (GETTEXT_PACKAGE, \
59                                                   DESCRIPTION),    \
60                                       debug_option_getter, \
61                                       debug_option_setter, \
62                                       GUINT_TO_POINTER (shift)); \
63   } G_STMT_END;
64
65 #include "cogl-debug-options.h"
66 #undef OPT
67
68   atexit (print_exit_report);
69 }
70
71 void
72 _cogl_profile_trace_message (const char *format, ...)
73 {
74   va_list ap;
75
76   va_start (ap, format);
77   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, ap);
78   va_end (ap);
79
80   if (_cogl_uprof_context)
81     uprof_context_vtrace_message (_cogl_uprof_context, format, ap);
82 }
83
84 #endif