summaryrefslogtreecommitdiff
path: root/tools/eos-profile-tool/eos-profile-utils.c
blob: 483f3752158d395c85c1a69953c7fd786630061d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "config.h"

#include "eos-profile-utils.h"

#include "endless/eosprofile-private.h"

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include <math.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

static const char *ansi_colors[] = {
  [EOS_PRINT_COLOR_GREEN] = "[1;32m",
  [EOS_PRINT_COLOR_BLUE] = "[1;34m",
  [EOS_PRINT_COLOR_YELLOW] = "[1;33m",
  [EOS_PRINT_COLOR_RED] = "[1;31m",
  [EOS_PRINT_COLOR_NONE] = "[0m",
};

static char *
gen_color_message (const char *prefix,
                   EosPrintColor color,
                   const char *fmt,
                   va_list args)
{
  g_autofree char *res = NULL;

  if (prefix != NULL)
    {
      g_autofree char *msg = g_strdup_vprintf (fmt, args);

      res = g_strdup_printf ("\033%s%s\033%s: %s",
                             ansi_colors[color],
                             prefix,
                             ansi_colors[EOS_PRINT_COLOR_NONE],
                             msg);

    }
  else
    res = g_strdup_vprintf (fmt, args);

  return g_steal_pointer (&res);
}

void
eos_profile_util_print_message (const char *prefix,
                                EosPrintColor color,
                                const char *fmt,
                                ...)
{
  g_autofree char *msg = NULL;
  va_list args;

  va_start (args, fmt);
  msg = gen_color_message (prefix, color, fmt, args);
  va_end (args);

  g_print ("%s\n", msg);
}

void
eos_profile_util_print_error (const char *fmt,
                              ...)
{
  g_autofree char *msg = NULL;
  va_list args;

  va_start (args, fmt);
  msg = gen_color_message ("ERROR", EOS_PRINT_COLOR_RED, fmt, args);
  va_end (args);

  g_printerr ("%s\n", msg);
}

void
eos_profile_util_print_warning (const char *fmt,
                                ...)
{
  g_autofree char *msg = NULL;
  va_list args;

  va_start (args, fmt);
  msg = gen_color_message ("WARNING", EOS_PRINT_COLOR_YELLOW, fmt, args);
  va_end (args);

  g_printerr ("%s\n", msg);
}

void
eos_profile_util_foreach_probe_v1 (GvdbTable               *db,
                                   EosProfileProbeCallback  callback,
                                   gpointer                 callback_data)
{
  int names_len = 0;
  g_auto(GStrv) names = gvdb_table_get_names (db, &names_len);

  const char * const meta_keys[] = {
    PROBE_DB_META_VERSION_KEY,
    PROBE_DB_META_APPID_KEY,
    PROBE_DB_META_PROFILE_KEY,
    PROBE_DB_META_START_KEY,
    NULL,
  };

  for (int i = 0; i < names_len; i++)
    {
      const char *key_name = names[i];

      if (g_strv_contains (meta_keys, key_name))
        continue;

      if (!gvdb_table_has_value (db, key_name))
        continue;

      g_autoptr(GVariant) value = gvdb_table_get_raw_value (db, key_name);
      if (value == NULL)
        continue;

      const char *file = NULL;
      const char *function = NULL;
      const char *probe_name = NULL;
      g_autoptr(GVariant) samples = NULL;
      gint32 line, n_samples;

      g_variant_get (value, "(&s&s&suu@a(xx))",
                     &probe_name,
                     &function,
                     &file,
                     &line,
                     &n_samples,
                     &samples);

      if (!callback (probe_name, function, file, line, n_samples, samples, callback_data))
        break;
   }
}