test-logging.c 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-device.h"
24
#include "bolt-domain.h"
25 26 27 28 29 30
#include "bolt-error.h"
#include "bolt-str.h"
#include "bolt-term.h"

#include "bolt-log.h"

31 32
#include "bolt-daemon-resource.h"

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include <glib.h>
#include <gio/gio.h>
#include <glib/gprintf.h>

#include <locale.h>
#include <stdarg.h>
#include <stdio.h>

typedef struct _LogData
{
  GLogLevelFlags level;
  GHashTable    *fields;
} LogData;

typedef struct _TestLog
{
  LogData data;
} TestLog;

static void
test_log_setup (TestLog *tt, gconstpointer user_data)
{
  tt->data.fields = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
56
  g_log_set_writer_func (g_log_writer_standard_streams, NULL, NULL);
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
}

static void
test_log_tear_down (TestLog *tt, gconstpointer user_data)
{
  g_hash_table_destroy (tt->data.fields);
}

static void
log_expectv (TestLog       *tt,
             GLogLevelFlags level,
             const char    *domain,
             const char    *message,
             va_list        args)
{
  const char *key;

  tt->data.level = level;

  g_hash_table_remove_all (tt->data.fields);

  if (domain)
    g_hash_table_insert (tt->data.fields,
                         g_strdup ("GLIB_DOMAIN"),
                         g_strdup (domain));

  if (message)
    g_hash_table_insert (tt->data.fields,
                         g_strdup ("MESSAGE"),
                         g_strdup (message));

  while ((key = va_arg (args, const char *)) != NULL)
    {
      const char *val = va_arg (args, const char *);
      g_hash_table_insert (tt->data.fields, g_strdup (key), g_strdup (val));
    }
}

static void
log_expect (TestLog       *tt,
            GLogLevelFlags level,
            const char    *domain,
            const char    *message,
            ...)
{
  va_list args;

  va_start (args, message);
  log_expectv (tt, level, domain, message, args);
  va_end (args);
}

static GLogWriterOutput
test_writer (GLogLevelFlags   log_level,
             const GLogField *fields,
             gsize            n_fields,
             gpointer         user_data)
{
  g_autoptr(GHashTable) index = NULL;
  LogData *data = user_data;
  GHashTableIter iter;
  gpointer k, v;

  index = g_hash_table_new (g_str_hash, g_str_equal);

  for (gsize i = 0; i < n_fields; i++)
    {
      const char *key = fields[i].key;
      const char *val = (const char *) fields[i].value;

      if (!g_hash_table_contains (data->fields, key))
        continue;

130
      g_hash_table_insert (index, (gpointer) key, (gpointer) val);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    }

  g_hash_table_iter_init (&iter, data->fields);
  while (g_hash_table_iter_next (&iter, &k, &v))
    {
      const char *key = k;
      const char *value = v;
      const char *have;

      g_assert_true (g_hash_table_contains (index, key));
      have = g_hash_table_lookup (index, key);
      g_assert_cmpstr (value, ==, have);
      //g_fprintf (stderr, "%s: %s vs %s\n", key, value, have);
    }

  return G_LOG_WRITER_HANDLED;
}

static void
test_log_basic (TestLog *tt, gconstpointer user_data)
{
  log_expect (tt, G_LOG_LEVEL_MESSAGE, "bolt-test", "test", NULL);
153 154

  g_log_set_writer_func (test_writer, &tt->data, NULL);
155
  bolt_log ("bolt-test", G_LOG_LEVEL_MESSAGE, "test");
156 157 158 159 160 161 162 163 164 165 166 167 168 169

  g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_ERROR));
  g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_CRITICAL));
  g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_WARNING));
  g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_MESSAGE));
  g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_INFO));
  g_assert_nonnull (bolt_log_level_to_string (G_LOG_LEVEL_DEBUG));

  g_assert_nonnull (bolt_log_level_to_priority (G_LOG_LEVEL_ERROR));
  g_assert_nonnull (bolt_log_level_to_priority (G_LOG_LEVEL_CRITICAL));
  g_assert_nonnull (bolt_log_level_to_priority (G_LOG_LEVEL_WARNING));
  g_assert_nonnull (bolt_log_level_to_priority (G_LOG_LEVEL_MESSAGE));
  g_assert_nonnull (bolt_log_level_to_priority (G_LOG_LEVEL_INFO));
  g_assert_nonnull (bolt_log_level_to_priority (G_LOG_LEVEL_DEBUG));
170 171 172 173 174 175 176 177 178 179 180 181 182
}

static void
test_log_gerror (TestLog *tt, gconstpointer user_data)
{
  g_autoptr(GError) error = NULL;
  const char *domain = "bolt-gerror";
  GLogLevelFlags lvl = G_LOG_LEVEL_INFO;
  const char *msg;

  msg = "no udev";
  g_set_error_literal (&error, BOLT_ERROR, BOLT_ERROR_UDEV, msg);
  log_expect (tt, lvl, domain, NULL, "ERROR_MESSAGE", msg, NULL);
183 184

  g_log_set_writer_func (test_writer, &tt->data, NULL);
185
  bolt_log (domain, lvl, LOG_ERR (error), NULL);
186 187

  /* check we handle NULL GErrors without crashing */
188 189 190
  log_expect (tt, lvl, domain, NULL, "ERROR_MESSAGE", "unknown cause",
              BOLT_LOG_BUG_MARK, "*",
              NULL);
191
  bolt_log (domain, lvl, LOG_ERR (NULL), NULL);
192 193 194 195 196 197 198 199 200
}

static void
test_log_device (TestLog *tt, gconstpointer user_data)
{
  g_autoptr(BoltDevice) a = NULL;
  const char *domain = "bolt-device";
  const char *msg;
  GLogLevelFlags lvl;
201
  const char *uid_a = "fbc83890-e9bf-45e5-a777-b3728490989c";
202 203 204 205 206 207 208 209 210 211 212 213 214

  a = g_object_new (BOLT_TYPE_DEVICE,
                    "uid", uid_a,
                    "name", "Laptop",
                    "vendor", "GNOME.org",
                    "status", BOLT_STATUS_DISCONNECTED,
                    NULL);

  lvl = G_LOG_LEVEL_INFO;
  msg = "test device a";
  log_expect (tt, lvl, domain, msg,
              BOLT_LOG_DEVICE_UID, uid_a,
              NULL);
215 216

  g_log_set_writer_func (test_writer, &tt->data, NULL);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  bolt_log (domain, lvl, LOG_DEV (a), msg);
}

static void
test_log_macros (TestLog *tt, gconstpointer user_data)
{
  g_autoptr(GError) error = NULL;
  GLogLevelFlags lvl = G_LOG_LEVEL_INFO;

  const char *msg = "da steht ich nun ich armer test";

  g_log_set_writer_func (test_writer, &tt->data, NULL);

  log_expect (tt, G_LOG_LEVEL_MESSAGE, G_LOG_DOMAIN, msg, NULL);
  bolt_msg (msg);

  g_set_error_literal (&error, BOLT_ERROR, BOLT_ERROR_UDEV, msg);
  log_expect (tt, lvl, G_LOG_DOMAIN, NULL, "ERROR_MESSAGE", msg, NULL);
  bolt_warn_err (error, NULL);

  log_expect (tt, lvl, G_LOG_DOMAIN, NULL,
              BOLT_LOG_ERROR_MESSAGE, msg,
              NULL);

  bolt_log (G_LOG_DOMAIN, lvl,
            LOG_DIRECT (BOLT_LOG_ERROR_MESSAGE, msg),
            NULL);

  log_expect (tt, G_LOG_LEVEL_DEBUG, G_LOG_DOMAIN, msg,
              "CODE_FILE", __FILE__,
              "CODE_FUNC", G_STRFUNC,
              NULL);
  bolt_debug (msg);
250 251 252 253 254 255 256

  msg = "nasty bug";
  log_expect (tt, G_LOG_LEVEL_DEBUG, G_LOG_DOMAIN, msg,
              BOLT_LOG_TOPIC, "code",
              BOLT_LOG_BUG_MARK, "*",
              NULL);
  bolt_bug (msg);
257 258
}

259
static GLogWriterOutput
260 261 262 263
test_log_logger_stdstream (GLogLevelFlags   level,
                           const GLogField *fields,
                           gsize            n_fields,
                           gpointer         user_data)
264 265 266 267 268 269 270 271 272 273 274 275 276 277
{
  g_autoptr(BoltLogCtx) ctx = NULL;

  g_return_val_if_fail (fields != NULL, G_LOG_WRITER_UNHANDLED);
  g_return_val_if_fail (n_fields > 0, G_LOG_WRITER_UNHANDLED);

  ctx = bolt_log_ctx_acquire (fields, n_fields);

  if (ctx == NULL)
    return G_LOG_WRITER_UNHANDLED;

  return bolt_log_stdstream (ctx, level, 0);
}

278 279 280 281 282 283 284 285
static GLogWriterOutput
test_log_logger_journal (GLogLevelFlags   level,
                         const GLogField *fields,
                         gsize            n_fields,
                         gpointer         user_data)
{
  g_autoptr(BoltLogCtx) ctx = NULL;
  char message[2048] = {0, };
286
  const char *dom;
287 288 289 290 291 292 293 294 295

  g_return_val_if_fail (fields != NULL, G_LOG_WRITER_UNHANDLED);
  g_return_val_if_fail (n_fields > 0, G_LOG_WRITER_UNHANDLED);

  ctx = bolt_log_ctx_acquire (fields, n_fields);

  if (ctx == NULL)
    return G_LOG_WRITER_UNHANDLED;

296 297 298 299
  dom = blot_log_ctx_get_domain (ctx);
  if (dom != NULL)
    fprintf (stderr, "DOMAIN: %s", dom);

300
  bolt_log_fmt_journal (ctx, level, message, sizeof (message));
301
  fprintf (stderr, "%s\n", message);
302 303 304 305

  return G_LOG_WRITER_HANDLED;
}

306
static void
307
test_log_logger (TestLog *tt, gconstpointer user_data)
308 309 310
{
  g_autoptr(GError) err = NULL;
  const char *msg = NULL;
311 312 313
  const char *uid1 = "884c6edd-7118-4b21-b186-b02d396ecca0";
  const char *uid2 = "884c6ede-7118-4b21-b186-b02d396ecca0";
  const char *uid3 = "884c6edf-7118-4b21-b186-b02d396ecca0";
314 315 316

  if (g_test_subprocess ())
    {
317 318 319
      g_autoptr(BoltDomain) dom = NULL;
      g_autoptr(BoltDevice) dev = NULL;

320 321 322 323
      if (GPOINTER_TO_INT (user_data) == 1)
        g_log_set_writer_func (test_log_logger_journal, tt, NULL);
      else
        g_log_set_writer_func (test_log_logger_stdstream, tt, NULL);
324

325 326 327 328 329 330 331 332 333 334 335 336 337
      dom = g_object_new (BOLT_TYPE_DOMAIN,
                          "id", "domain0",
                          "uid", uid1,
                          "bootacl", NULL,
                          NULL);

      dev = g_object_new (BOLT_TYPE_DEVICE,
                          "uid", uid2,
                          "name", "Laptop",
                          "vendor", "GNOME.org",
                          "status", BOLT_STATUS_DISCONNECTED,
                          NULL);

338 339 340 341
      msg = "no udev";
      g_set_error_literal (&err, BOLT_ERROR, BOLT_ERROR_UDEV, msg);
      bolt_warn_err (err, LOG_TOPIC ("the_topic"), "WARNUNG-1");

342 343 344 345 346 347 348 349 350
      bolt_log ("ck01", G_LOG_LEVEL_INFO, LOG_DEV (dev),
                "MESSAGE-%d", 1);

      bolt_log ("ck01", G_LOG_LEVEL_INFO, LOG_DOM (dom),
                "MESSAGE-%d", 2);

      bolt_log ("ck01", G_LOG_LEVEL_INFO, LOG_DEV_UID (uid3),
                "MESSAGE-%d", 3);

351 352 353
      g_warning ("WARNUNG-2");
      g_critical ("WARNUNG-3");

354 355
      g_log ("ck02", G_LOG_LEVEL_INFO, "MESSAGE-%d", 4);

356 357 358 359 360 361 362 363 364
      exit (0);
    }

  g_test_trap_subprocess (NULL, 0, 0);
  g_test_trap_assert_passed ();
  g_test_trap_assert_stderr ("*WARNUNG-1*");
  g_test_trap_assert_stderr ("*the_topic*");
  g_test_trap_assert_stderr ("*WARNUNG-2*");
  g_test_trap_assert_stderr ("*WARNUNG-3*");
365 366 367
  g_test_trap_assert_stderr ("*MESSAGE-1*");
  g_test_trap_assert_stderr ("*MESSAGE-2*");
  g_test_trap_assert_stderr ("*MESSAGE-3*");
368
  g_test_trap_assert_stderr ("*MESSAGE-3*");
369 370 371 372 373
  g_test_trap_assert_stderr ("*domain0*");
  g_test_trap_assert_stderr ("*884c6edd*");
  g_test_trap_assert_stderr ("*884c6ede*");
  g_test_trap_assert_stderr ("*884c6edf*");
  g_test_trap_assert_stderr ("*Laptop*");
374 375 376 377 378 379

  if (GPOINTER_TO_INT (user_data) == 1)
    {
      g_test_trap_assert_stderr ("*DOMAIN: ck01*");
      g_test_trap_assert_stderr ("*DOMAIN: ck02*");
    }
380 381
}

382

383 384 385 386 387 388 389 390
int
main (int argc, char **argv)
{

  setlocale (LC_ALL, "");

  g_test_init (&argc, &argv, NULL);

391 392
  g_resources_register (bolt_daemon_get_resource ());

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  g_test_add ("/logging/basic",
              TestLog,
              NULL,
              test_log_setup,
              test_log_basic,
              test_log_tear_down);

  g_test_add ("/logging/gerror",
              TestLog,
              NULL,
              test_log_setup,
              test_log_gerror,
              test_log_tear_down);

  g_test_add ("/logging/device",
              TestLog,
              NULL,
              test_log_setup,
              test_log_device,
              test_log_tear_down);

  g_test_add ("/logging/macros",
              TestLog,
              NULL,
              test_log_setup,
              test_log_macros,
              test_log_tear_down);

421
  g_test_add ("/logging/logger/stdstream",
422
              TestLog,
423
              GINT_TO_POINTER (0),
424
              test_log_setup,
425
              test_log_logger,
426 427
              test_log_tear_down);

428 429 430 431 432 433
  g_test_add ("/logging/logger/journal",
              TestLog,
              GINT_TO_POINTER (1),
              test_log_setup,
              test_log_logger,
              test_log_tear_down);
434

435 436
  return g_test_run ();
}