mkernel 0.0.2
Micro-kernel framework, everything as a module
assert.c
Go to the documentation of this file.
1
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
41static 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 time_t l_CurrentTime = time(NULL);
51
52 /* Parameter validity check against invalid parameters such as NULL or
53 * empty string values. */
54 if ((NULL==p_File)|| (0==p_File[0])||
55 (0==p_Line)||
56 (NULL==p_CompilDate)|| (0==p_CompilDate[0])||
57 (NULL==p_CompilTime)|| (0==p_CompilTime[0])||
58 (NULL==p_Function)|| (0==p_Function[0])) {
59 fprintf(stderr,"%s:%d Unexpected and invalid parameters\n",__FILE__, __LINE__);
60 abort();
61 }
62
63 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 */
67 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 return &l_tmp[0];
72}
73
74/* Documentation in header file */
75void _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 fprintf (stderr,"%s\n",
82 _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function)
83 );
84}
85
86/* Documentation in header file */
87void _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 fprintf (stderr,"%s : %s\n",
95 _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function),
96 p_Message
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{
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 va_start(l_ap, p_Format);
119 l_length = vsnprintf(l_tmp,sizeof(l_tmp),p_Format,l_ap);
120 va_end(l_ap);
121 }
122
123 if (l_length >= sizeof(l_tmp)-1) {
124 /* Indicate that message was truncated */
125 strcpy(&(l_tmp[sizeof(l_tmp)-6]),"[...]");
126 }
127
128 /* Parameter validity enforced by _timestamp */
129 fprintf (stderr,"%s : %s\n",
130 _timestamp( p_File, p_Line, p_CompilDate, p_CompilTime, p_Function),
131 l_tmp
132 );
133}
134
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:87
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:75
Debugging macros.
#define NULL
Definition: mkernel-opt.c:64