LCOV - code coverage report
Current view: top level - deps/debug - assert.c (source / functions) Hit Total Coverage
Test: mkernel.info Lines: 26 26 100.0 %
Date: 2024-07-29 18:25:04 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /**         @file  assert.c
       2             :  *         @brief  Compiled functions used by debugging macros to write on stderr
       3             :  *          @date  11/05/1997
       4             :  *        @author  François Cerbelle (Fanfan), francois@cerbelle.net
       5             :  *     @copyright  Copyright (c) 1997-2024, François Cerbelle
       6             :  *
       7             :  *  Originally inspired by "L'art du code", Steve Maguire, Microsoft Press
       8             :  *
       9             :  *   @internal
      10             :  *       Compiler  gcc
      11             :  *  Last modified  2024-07-27 20:44
      12             :  *   Organization  Cerbelle.net
      13             :  *        Company  Home
      14             :  *
      15             :  *   This source code is released for free distribution under the terms of the
      16             :  *   GNU General Public License as published by the Free Software Foundation.
      17             :  *
      18             :  */
      19             : 
      20             : #include "assert.h"
      21             : #include <stdarg.h>                             /* va_list, va_start, va_arg, va_end */
      22             : #include <string.h>                             /* strcpy */
      23             : #include <stdio.h>                              /* fprintf */
      24             : #include <time.h>                               /* time, localtime */
      25             : 
      26             : /** Build a timestamp string with filename and compilation date
      27             :  *
      28             :  * Returned value is a static string which can be modified later. It should not
      29             :  * be passed to free and should not be modified.
      30             :  *
      31             :  * NULL or empty string parameter values will abort the program.
      32             :  *
      33             :  *  @param[in] p_File Source file
      34             :  *  @param[in] p_Line Source line in the source file
      35             :  *  @param[in] p_CompilDate Compilation date
      36             :  *  @param[in] p_CompilTime Compilation time
      37             :  *  @param[in] p_Function Function name in the source file
      38             :  *
      39             :  *  @return Pointer to a static string
      40             :  */
      41       45786 : static const char*_timestamp(const char* p_File,
      42             :                              const unsigned int p_Line,
      43             :                              const char* p_CompilDate,
      44             :                              const char* p_CompilTime,
      45             :                              const char* p_Function)
      46             : {
      47             :     /* Get local time and format it */
      48             :     char l_Time[24];
      49             :     static char l_tmp[110];
      50       45786 :     time_t l_CurrentTime = time(NULL);
      51             : 
      52             :     /* Parameter validity check against invalid parameters such as NULL or
      53             :      * empty string values. */
      54       45786 :     if ((NULL==p_File)|| (0==p_File[0])||
      55       45768 :         (0==p_Line)||
      56       45762 :         (NULL==p_CompilDate)|| (0==p_CompilDate[0])||
      57       45750 :         (NULL==p_CompilTime)|| (0==p_CompilTime[0])||
      58       45734 :         (NULL==p_Function)|| (0==p_Function[0])) {
      59          58 :         fprintf(stderr,"%s:%d Unexpected and invalid parameters\n",__FILE__, __LINE__);
      60          58 :         abort();
      61             :     }
      62             : 
      63       45728 :     strftime(l_Time, sizeof(l_Time), "%Y-%m-%d %H:%M:%S", localtime(&l_CurrentTime));
      64             : 
      65             :     /* Timestamp build with the filename, file line, function name */
      66             :     /** @todo Replace with a portable snprintf function */
      67       45728 :     snprintf(l_tmp,sizeof(l_tmp),
      68             :              "%19s [%20s:%-4ud] (%11s @ %8s) %30s()",
      69             :              l_Time,p_File,p_Line,p_CompilDate,p_CompilTime,p_Function
      70             :             );
      71       45728 :     return &l_tmp[0];
      72             : }
      73             : 
      74             : /* Documentation in header file */
      75       10870 : void _trace(const char* p_File,
      76             :             const unsigned int p_Line,
      77             :             const char* p_CompilDate,
      78             :             const char* p_CompilTime, const char* p_Function)
      79             : {
      80             :     /* Parameter validity enforced by _timestamp */
      81       10870 :     fprintf (stderr,"%s\n",
      82             :              _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function)
      83             :             );
      84       10852 : }
      85             : 
      86             : /* Documentation in header file */
      87       22562 : void _trace_msg(const char* p_File,
      88             :                 const unsigned int p_Line,
      89             :                 const char* p_CompilDate,
      90             :                 const char* p_CompilTime,
      91             :                 const char* p_Function, const char* p_Message)
      92             : {
      93             :     /* Parameter validity enforced by _timestamp */
      94       22562 :     fprintf (stderr,"%s : %s\n",
      95             :              _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function),
      96             :              p_Message
      97             :             );
      98       22542 : }
      99             : 
     100             : /* Documentation in header file */
     101       12354 : void _trace_dynmsg(const char* p_File,
     102             :                    const unsigned int p_Line,
     103             :                    const char* p_CompilDate,
     104             :                    const char* p_CompilTime,
     105             :                    const char* p_Function, const char* p_Format, ...
     106             :                   )
     107             : {
     108             :     /* Formatted message string */
     109             :     char l_tmp[100];
     110             :     /* Formatted message length */
     111             :     unsigned int l_length;
     112             : 
     113             :     /* Limit variadic processing scope in a block */
     114             :     {
     115             :         /* Build the formatted message */
     116             :         va_list l_ap;
     117       12354 :         va_start(l_ap, p_Format);
     118             :         /** @todo Replace with a portable snprintf function */
     119       12354 :         l_length = vsnprintf(l_tmp,sizeof(l_tmp),p_Format,l_ap);
     120       12354 :         va_end(l_ap);
     121             :     }
     122             : 
     123       12354 :     if (l_length >= sizeof(l_tmp)-1) {
     124             :         /* Indicate that message was truncated */
     125        3526 :         strcpy(&(l_tmp[sizeof(l_tmp)-6]),"[...]");
     126             :     }
     127             : 
     128             :     /* Parameter validity enforced by _timestamp */
     129       12354 :     fprintf (stderr,"%s : %s\n",
     130             :              _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function),
     131             :              l_tmp
     132             :             );
     133       12334 : }
     134             : 

Generated by: LCOV version 1.16