spice-common/common/log.h
Christophe Fergeau 4951c71553 log: Let gcc know about the logging macros which abort
This commit adds a SPICE_UNREACHABLE macro (courtesy of Frediano)
so that gcc does not think that code control can go past
spice_return_{val_,}if_fail(), spice_critical() and spice_error()

This avoids this kind of warnings:

fallthrough.c:

 #include "log.h"

int main(int argc, char **argv)
{
    switch(argc) {
        case 1:
            spice_critical("foo");
       default:
            return 0;
    }
}

$ gcc  -c    $(pkg-config --cflags --libs glib-2.0 spice-protocol)
       -I common   -Wimplicit-fallthrough=5 ./fallthrough.c
In file included from ./fallthrough.c:1:
./fallthrough.c: In function 'main':
common/log.h:73:5: warning: this statement may fall through [-Wimplicit-fallthrough=]
   73 |     spice_log(G_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "" format, ## __VA_ARGS__); \
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./fallthrough.c:8:25: note: in expansion of macro 'spice_critical'
    8 |                         spice_critical("foo");
      |                         ^~~~~~~~~~~~~~
./fallthrough.c:9:17: note: here
    9 |                 default:
      |                 ^~~~~~~

Signed-off-by: Christophe Fergeau <cfergeau@redhat.com>
Acked-by: Frediano Ziglio <fziglio@redhat.com>
2019-04-01 17:33:17 +01:00

105 lines
3.9 KiB
C

/*
Copyright (C) 2012 Red Hat, Inc.
This library 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/>.
*/
#ifndef H_SPICE_COMMON_LOG
#define H_SPICE_COMMON_LOG
#include <stdarg.h>
#include <stdio.h>
#include <glib.h>
#include <spice/macros.h>
#include "macros.h"
SPICE_BEGIN_DECLS
#ifdef SPICE_LOG_DOMAIN
#error Do not use obsolete SPICE_LOG_DOMAIN macro, is currently unused
#endif
#define SPICE_STRLOC __FILE__ ":" G_STRINGIFY (__LINE__)
void spice_log(GLogLevelFlags log_level,
const char *strloc,
const char *function,
const char *format,
...) G_GNUC_PRINTF(4, 5);
/* FIXME: name is misleading, this aborts.. */
#define spice_return_if_fail(x) G_STMT_START { \
if G_LIKELY(x) { } else { \
spice_critical("condition `%s' failed", #x); \
return; \
} \
} G_STMT_END
/* FIXME: name is misleading, this aborts.. */
#define spice_return_val_if_fail(x, val) G_STMT_START { \
if G_LIKELY(x) { } else { \
spice_critical("condition `%s' failed", #x); \
return (val); \
} \
} G_STMT_END
#define spice_warn_if_reached() G_STMT_START { \
spice_log(G_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "should not be reached"); \
} G_STMT_END
#define spice_info(format, ...) G_STMT_START { \
spice_log(G_LOG_LEVEL_INFO, SPICE_STRLOC, __FUNCTION__, "" format, ## __VA_ARGS__); \
} G_STMT_END
#define spice_debug(format, ...) G_STMT_START { \
spice_log(G_LOG_LEVEL_DEBUG, SPICE_STRLOC, __FUNCTION__, "" format, ## __VA_ARGS__); \
} G_STMT_END
#define spice_warning(format, ...) G_STMT_START { \
spice_log(G_LOG_LEVEL_WARNING, SPICE_STRLOC, __FUNCTION__, "" format, ## __VA_ARGS__); \
} G_STMT_END
#define spice_critical(format, ...) G_STMT_START { \
spice_log(G_LOG_LEVEL_CRITICAL, SPICE_STRLOC, __FUNCTION__, "" format, ## __VA_ARGS__); \
SPICE_UNREACHABLE; \
} G_STMT_END
#define spice_error(format, ...) G_STMT_START { \
spice_log(G_LOG_LEVEL_ERROR, SPICE_STRLOC, __FUNCTION__, "" format, ## __VA_ARGS__); \
SPICE_UNREACHABLE; \
} G_STMT_END
#define spice_warn_if_fail(x) G_STMT_START { \
if G_LIKELY(x) { } else { \
spice_warning("condition `%s' failed", #x); \
} \
} G_STMT_END
#define spice_assert(x) G_STMT_START { \
if G_LIKELY(x) { } else { \
spice_error("assertion `%s' failed", #x); \
} \
} G_STMT_END
#if ENABLE_EXTRA_CHECKS
enum { spice_extra_checks = 1 };
#else
enum { spice_extra_checks = 0 };
#endif
SPICE_END_DECLS
#endif // H_SPICE_COMMON_LOG