libdebug 1.0.0
Debugging helper library with Memory leak detection
assert.c
Go to the documentation of this file.
1
20#define _XOPEN_SOURCE 500 /* vasprintf */
21#include "libdebug/assert.h"
22#include <stdarg.h> /* va_list, va_start, va_arg, va_end */
23#include <string.h> /* strcpy */
24#include <stdio.h> /* fprintf */
25#include <time.h> /* time, localtime */
26#include <stdlib.h> /* abort */
27#include <stdio.h> /* snprintf */
28
44static const char*_timestamp(const char* p_File,
45 const unsigned int p_Line,
46 const char* p_CompilDate,
47 const char* p_CompilTime,
48 const char* p_Function) {
49 /* Get local time and format it */
50 char l_Time[24];
51 static char l_tmp[120];
52 time_t l_CurrentTime = time(NULL);
53
54 /* Parameter validity check against invalid parameters such as NULL or
55 * empty string values. */
56 if ((NULL==p_File)|| (0==p_File[0])||
57 (0==p_Line)||
58 (NULL==p_CompilDate)|| (0==p_CompilDate[0])||
59 (NULL==p_CompilTime)|| (0==p_CompilTime[0])||
60 (NULL==p_Function)|| (0==p_Function[0])) {
61 fprintf(stderr,"%s:%d Unexpected and invalid parameters\n",__FILE__, __LINE__);
62 abort();
63 }
64
65 strftime(l_Time, sizeof(l_Time), "%Y-%m-%d %H:%M:%S", localtime(&l_CurrentTime));
66
67 /* Timestamp build with the filename, file line, function name */
69 snprintf(l_tmp,sizeof(l_tmp),
70 "%19s [%20s:%-4ud] (%11s @ %8s) %30s()",
71 l_Time,p_File,p_Line,p_CompilDate,p_CompilTime,p_Function /* cppcheck-suppress ctunullpointer */
72 );
73 return &l_tmp[0];
74}
75
76/* Documentation in header file */
77void _trace(const char* p_File,
78 const unsigned int p_Line,
79 const char* p_CompilDate,
80 const char* p_CompilTime, const char* p_Function) {
81 /* Parameter validity enforced by _timestamp */
82 fprintf (stderr,"%s\n",
83 _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function)
84 );
85}
86
87/* Documentation in header file */
88void _trace_msg(const char* p_File,
89 const unsigned int p_Line,
90 const char* p_CompilDate,
91 const char* p_CompilTime,
92 const char* p_Function, const char* p_Message) {
93 /* Parameter validity enforced by _timestamp */
94 fprintf (stderr,"%s : %s\n",
95 _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function),
96 p_Message /* cppcheck-suppress ctunullpointer */
97 );
98}
99
100/* Documentation in header file */
101void _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 /* Formatted message string */
108 char l_tmp[100];
109 /* Formatted message length */
110 unsigned int l_length;
111
112 /* Limit variadic processing scope in a block */
113 {
114 /* Build the formatted message */
115 va_list l_ap;
116 va_start(l_ap, p_Format);
118 l_length = vsnprintf(l_tmp,sizeof(l_tmp),p_Format,l_ap);
119 va_end(l_ap);
120 }
121
122 if (l_length >= sizeof(l_tmp)-1) {
123 /* Indicate that message was truncated */
124 strcpy(&(l_tmp[sizeof(l_tmp)-6]),"[...]");
125 }
126
127 /* Parameter validity enforced by _timestamp */
128 fprintf (stderr,"%s : %s\n",
129 _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function),
130 l_tmp
131 );
132}
133/* vim: set tw=80: */
void _trace_dynmsg(const char *p_File, const unsigned int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function, const char *p_Format,...)
Print a debug trace (checkpoint) with a formatted message.
Definition: assert.c:101
void _trace_msg(const char *p_File, const unsigned int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function, const char *p_Message)
Print a debug trace (checkpoint) with a static message.
Definition: assert.c:88
void _trace(const char *p_File, const unsigned int p_Line, const char *p_CompilDate, const char *p_CompilTime, const char *p_Function)
Print a debug trace (checkpoint)
Definition: assert.c:77
Debugging macros.