Changeset d8de5d3 in mainline


Ignore:
Timestamp:
2011-05-23T13:31:54Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
540cb1b, 773f188
Parents:
207533f
Message:

do not reinvent the wheel in assert_abort(), use the services of printf_core() to implement a more elegant klog output
(printf_core should be safe w.r.t. nested assertions and memory allocation)

Location:
uspace/lib/c
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/assert.c

    r207533f rd8de5d3  
    3939#include <stdint.h>
    4040
    41 #define MSG_START       "Assertion failed ("
    42 #define MSG_FILE        ") in file \""
    43 #define MSG_LINE        "\", line "
    44 #define MSG_END         ".\n"
    45 
    46 static atomic_t failed_asserts;
     41static atomic_t failed_asserts = {0};
    4742
    4843void assert_abort(const char *cond, const char *file, unsigned int line)
    4944{
    50         char lstr[11];
    51         char *pd = &lstr[10];
    52         uint32_t ln = (uint32_t) line;
    53 
    54         /*
    55          * Convert ln to a zero-terminated string.
    56          */
    57         *pd-- = 0;
    58         for (; ln; ln /= 10)
    59                 *pd-- = '0' + ln % 10;
    60         pd++;
    61 
    6245        /*
    6346         * Send the message safely to klog. Nested asserts should not occur.
    6447         */
    65         klog_write(MSG_START, str_size(MSG_START));
    66         klog_write(cond, str_size(cond));
    67         klog_write(MSG_FILE, str_size(MSG_FILE));
    68         klog_write(file, str_size(file));
    69         klog_write(MSG_LINE, str_size(MSG_LINE));
    70         klog_write(pd, str_size(pd));
    71         klog_write(MSG_END, str_size(MSG_END));
    72 
     48        klog_printf("Assertion failed (%s) in file \"%s\", line %u.\n",
     49            cond, file, line);
     50       
    7351        /*
    7452         * Check if this is a nested or parallel assert.
     
    8260         * assertions.
    8361         */
    84         printf(MSG_START "%s" MSG_FILE "%s" MSG_LINE "%u" MSG_END,
     62        printf("Assertion failed (%s) in file \"%s\", line %u.\n",
    8563            cond, file, line);
    8664        stacktrace_print();
    87 
     65       
    8866        abort();
    8967}
  • uspace/lib/c/generic/io/klog.c

    r207533f rd8de5d3  
    3838#include <sys/types.h>
    3939#include <unistd.h>
     40#include <errno.h>
    4041#include <io/klog.h>
     42#include <io/printf_core.h>
    4143
    4244size_t klog_write(const void *buf, size_t size)
     
    5557}
    5658
     59/** Print formatted text to klog.
     60 *
     61 * @param fmt Format string
     62 *
     63 * \see For more details about format string see printf_core.
     64 *
     65 */
     66int klog_printf(const char *fmt, ...)
     67{
     68        va_list args;
     69        va_start(args, fmt);
     70       
     71        int ret = klog_vprintf(fmt, args);
     72       
     73        va_end(args);
     74       
     75        return ret;
     76}
     77
     78static int klog_vprintf_str_write(const char *str, size_t size, void *data)
     79{
     80        size_t wr = klog_write(str, size);
     81        return str_nlength(str, wr);
     82}
     83
     84static int klog_vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
     85{
     86        size_t offset = 0;
     87        size_t chars = 0;
     88       
     89        while (offset < size) {
     90                char buf[STR_BOUNDS(1)];
     91                size_t sz = 0;
     92               
     93                if (chr_encode(str[chars], buf, &sz, STR_BOUNDS(1)) == EOK)
     94                        klog_write(buf, sz);
     95               
     96                chars++;
     97                offset += sizeof(wchar_t);
     98        }
     99       
     100        return chars;
     101}
     102
     103/** Print formatted text to klog.
     104 *
     105 * @param fmt Format string
     106 * @param ap  Format parameters
     107 *
     108 * \see For more details about format string see printf_core.
     109 *
     110 */
     111int klog_vprintf(const char *fmt, va_list ap)
     112{
     113        printf_spec_t ps = {
     114                klog_vprintf_str_write,
     115                klog_vprintf_wstr_write,
     116                NULL
     117        };
     118       
     119        return printf_core(fmt, &ps, ap);
     120}
     121
    57122/** @}
    58123 */
  • uspace/lib/c/generic/io/vprintf.c

    r207533f rd8de5d3  
    9696/** Print formatted text to stdout.
    9797 *
    98  * @param file Output stream
    99  * @param fmt  Format string
    100  * @param ap   Format parameters
     98 * @param fmt Format string
     99 * @param ap  Format parameters
    101100 *
    102101 * \see For more details about format string see printf_core.
  • uspace/lib/c/include/assert.h

    r207533f rd8de5d3  
    6464    __attribute__((noreturn));
    6565
    66 
    6766#endif
    6867
  • uspace/lib/c/include/io/klog.h

    r207533f rd8de5d3  
    3737
    3838#include <sys/types.h>
     39#include <stdarg.h>
    3940
    4041extern size_t klog_write(const void *, size_t);
    4142extern void klog_update(void);
     43extern int klog_printf(const char *, ...);
     44extern int klog_vprintf(const char *, va_list);
    4245
    4346#endif
Note: See TracChangeset for help on using the changeset viewer.