Custom assert function

9 views
Skip to first unread message

federic...@reghe.net

unread,
Dec 5, 2016, 4:58:53 AM12/5/16
to BOSP Development
I've added in reg-next (commit 625b32e) a custom assert function for Barbeque internals.

The function is wrapped in a macro called bbque_assert(x) declared in the same fashion of std::assert.
It provides the same functionality as std::assert logging the error to stdout, but it tries also to
write the error in the bbque.log before call std::abort(). This may speed up the debugging process
when Barbeque is run in daemon mode.

If Barbeque is not compiled in DEBUG mode, the asserts are disabled and removed.

In order to do not pollute the commit I haven't change the current asserts. As soon as we will merge
my branch into bosp-next please consider to use the new assert function and gradually replace the
current std::assert.


Cheers,
Federico Reghenzani

Giuseppe Massari

unread,
Dec 5, 2016, 10:04:14 AM12/5/16
to bosp-...@googlegroups.com
The patch has been just merged into bosp-next.

Regards
> --
> You received this message because you are subscribed to the Google Groups
> "BOSP Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to bosp-devel+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Giuseppe Massari

Twitter
Google+
Facebook
Skype: massanga999

derkling

unread,
Jan 6, 2017, 1:39:32 PM1/6/17
to BOSP Development

Few comments about this patch.

> diff --git a/bbque/utils/CMakeLists.txt b/bbque/utils/CMakeLists.txt
> index b99d7bb..038ab0a 100644
> --- a/bbque/utils/CMakeLists.txt
> +++ b/bbque/utils/CMakeLists.txt
> @@ -6,6 +6,7 @@ add_subdirectory(logging)
>  set (BBQUE_UTILS_SRC timer deferrable worker)
>  set (BBQUE_UTILS_SRC ${BBQUE_UTILS_SRC} metrics_collector)
>  set (BBQUE_UTILS_SRC ${BBQUE_UTILS_SRC} extra_data_container)
> +set (BBQUE_UTILS_SRC ${BBQUE_UTILS_SRC} assert)


AFAIU, you want to use assert functions only for BBQUE_DEBUG configured
builds. Thus, the compilation of "assert" should be better guarded by a:

if (CONFIG_BBQUE_DEBUG)
     set (BBQUE_UTILS_SRC ${BBQUE_UTILS_SRC} assert)
endif (CONFIG_BBQUE_DEBUG)

>  if (CONFIG_BBQUE_RTLIB_PERF_SUPPORT)
>       set (BBQUE_UTILS_SRC ${BBQUE_UTILS_SRC} perf)
>  endif (CONFIG_BBQUE_RTLIB_PERF_SUPPORT)
> diff --git a/bbque/utils/assert.cc b/bbque/utils/assert.cc
> new file mode 100644
> index 0000000..c0ba510
> --- /dev/null
> +++ b/bbque/utils/assert.cc
> @@ -0,0 +1,62 @@
> +/*
> + * Copyright (C) 2016  Politecnico di Milano
> + *
> + * This program is free software: you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program 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 General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#include "bbque/utils/logging/logger.h"
> +#include "bbque/utils/assert.h"
> +
> +#include <cassert>
> +#include <cstdlib>
> +#include <iostream>
> +
> +#define BBQUE_ASSERT_NAMESPACE "bq.crash"
> +
> +#ifdef BBQUE_DEBUG

If you don't want these functions defined that it's better to avoid the module
compilation on !BBQUE_DEBUG and these guards can be removed.

> +
> +namespace bu=bbque::utils;
> +
> +void _bbque_assert(const char *msg, const char *file, int line) {
> +
> +     // First of all print into the standard output. Maybe we will not able
> +     // to open a log, so we have to print the error into the stdout
> +     std::cout << "Barbeque ASSERTION FAILED." << std::endl;
> +     std::cout << "Assert: " << msg << std::endl;
> +     std::cout << "File: " << file << ":" << line << std::endl;
> +
> +     // Flush the stream just to be sure before continue (and maybe crash)
> +     std::cout << std::flush;
> +
> +     // Get a logger
> +     std::unique_ptr<bu::Logger> logger = 
                                            ^
                  Should remove white spaces at end of line

> +                             bu::Logger::GetLogger(BBQUE_ASSERT_NAMESPACE);
> +
> +     //If we do not have a logger, we cannot log the problem, so, stdout
> +     // and std assert
> +     if (logger) {
> +             logger->Fatal("Barbeque ASSERTION FAILED.");
> +             logger->Fatal("Assert: %s", msg);
> +             logger->Fatal("File: %s:%d", file, line);
> +     }
> +
> +     // Reset the logger to be sure it is flushed
> +     logger.reset(nullptr);
> +
> +     // And then, abort
> +     std::abort();
> +
> +     // Unreachable code
        ^
    Unnecessary comment, also because there is not code here in that function ;-)


> +}
> +#endif
> diff --git a/include/bbque/utils/assert.h b/include/bbque/utils/assert.h
> new file mode 100644
> index 0000000..d8ae432
> --- /dev/null
> +++ b/include/bbque/utils/assert.h
> @@ -0,0 +1,39 @@
> +/*
> + * Copyright (C) 2016  Politecnico di Milano
> + *
> + * This program is free software: you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation, either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program 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 General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> + */
> +
> +#ifndef BBQUE_ASSERT_H_
> +#define BBQUE_ASSERT_H_
> +
> +#include "bbque/config.h"
> +
> +#include <cassert>
> +
> +#ifdef BBQUE_DEBUG
> +     // The following macro respect the C++ specification for assert
> +     #define bbque_assert(EX) (void)((EX) || (_bbque_assert (#EX, __FILE__, __LINE__),0))
> +#else
> +     #define bbque_assert(EX)
> +#endif
> +
> +/**
> + * @brief This is the custom assert logging function. You should not use
> + *     directly this function, but instead use the bbque_assert macro,
> + *     that works exactly as the std::assert one.
> + */
> +void _bbque_assert(const char *msg, const char *file, int line);

But still you are exposing that function to all modules including this header.
Thus, if you should use "_bbque_assert" in other modules, you will get linking
errors in !BBQUE_DEBUG configured builds.

Perhaps it's better to move this forward declaration in the previous #ifdef and
add an empty definition in the #else case, e.g. something like:

#ifdef BBQUE_DEBUG
        void _bbque_assert(const char *msg, const char *file, int line);
        #define bbque_assert(EX) (void)((EX) || (_bbque_assert (#EX, __FILE__, __LINE__),0))
#else
        void _bbque_assert(const char *msg, const char *file, int line) {};
        #define bbque_assert(EX)
#endif


> +
> +#endif // BBQUE_ASSERT_H_


-- 
#include <best/regards.h>

Patrick Bellasi

Federico Reghenzani

unread,
Feb 14, 2017, 4:11:54 AM2/14/17
to derkling, bosp-...@googlegroups.com
> --
> You received this message because you are subscribed to the Google Groups
> "BOSP Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to bosp-devel+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Hi Patrick,

thank you for your comments, the fixes are on my branch in ce57832 commit.

I've not added the definition of _bbque_assert in !BBQUE_DEBUG build,
since the function _bbque_assert should not be directly used, as
specified in the documentation.


Cheers,

Federico Reghenzani
Reply all
Reply to author
Forward
0 new messages