blob: f92eadbdb4cc9a0d1bd740867b884c499cad2b1b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/******************************************************************************
2*(C) Copyright 2008 Marvell International Ltd.
3* All Rights Reserved
4******************************************************************************/
5/*****************************************************************************
6* Utility Library
7*
8* DESCRIPTION
9* Error reporting functions.
10*
11* EXAMPLE USAGE
12*
13* --- assert ---
14*
15* utlAssert(num == 42);
16*
17* --- reporting errors ---
18*
19* utlError("my error message");
20*
21* utlError("my error message with parameters: %d %s", 99, "my string");
22*
23*****************************************************************************/
24
25#include <stdio.h>
26#include <stdarg.h>
27#include <string.h>
28#include <unistd.h>
29
30#include "utlTypes.h"
31#include "utlError.h"
32
33
34/*---------------------------------------------------------------------------*
35* FUNCTION
36* utlDoAssert(utlHERE_DECLARATION, result, assertion_p)
37* INPUT
38* utlHERE_DECLARATION == source of assertion
39* result == assertion test result
40* assertion_p == assertion as a character string
41* OUTPUT
42* none
43* RETURNS
44* "true" if the assertion passed, else "false"
45* DESCRIPTION
46* Handles assertions, reporting an error if the assert fails.
47*---------------------------------------------------------------------------*/
48bool utlDoAssert(utlHERE_DECLARATION,
49 const int result,
50 const char *assertion_p)
51{
52 if (!result)
53 {
54
55/* HACK: for now just print the failed assertion... */
56
57 (void)fprintf(stderr, "ASSERT:\n");
58 (void)fprintf(stderr, " file path: %s\n", utlHERE_FILE);
59 (void)fprintf(stderr, " func name: %s\n", utlHERE_FUNC);
60 (void)fprintf(stderr, " line number: %u\n", (unsigned int)utlHERE_LINE);
61 (void)fprintf(stderr, " assertion: %s\n", assertion_p);
62
63 (void)fflush(stderr);
64
65 return false;
66 }
67
68 return true;
69}
70
71/*---------------------------------------------------------------------------*
72* FUNCTION
73* utlDoError(utlHERE_DECLARATION, msg_p, ...)
74* INPUT
75* utlHERE_DECLARATION == source of error
76* msg_p == pointer to an error message
77* OUTPUT
78* none
79* RETURNS
80* utlSUCCESS for success, utlFAILED for failure
81* DESCRIPTION
82* Handles error reporting.
83*---------------------------------------------------------------------------*/
84void utlDoError(utlHERE_DECLARATION,
85 const char *msg_p,
86 ...)
87{
88 utlAssert(msg_p != NULL);
89
90/* HACK: for now just print the error... */
91
92 (void)fprintf(stderr, "ERROR:\n");
93 (void)fprintf(stderr, " file path: %s\n", utlHERE_FILE);
94 (void)fprintf(stderr, " func name: %s\n", utlHERE_FUNC);
95 (void)fprintf(stderr, " line number: %u\n", (unsigned int)utlHERE_LINE);
96 (void)fprintf(stderr, " message: ");
97
98 {
99 va_list va_arg_p;
100
101 va_start(va_arg_p, msg_p);
102 (void)vfprintf(stderr, msg_p, va_arg_p);
103 va_end(va_arg_p);
104 }
105
106 (void)fprintf(stderr, "\n");
107
108 (void)fflush(stderr);
109}
110
111/*---------------------------------------------------------------------------*
112* FUNCTION
113* utlDoSigError(utlHERE_DECLARATION, msg_p, ...)
114* INPUT
115* utlHERE_DECLARATION == source of error
116* msg_p == pointer to an error message
117* OUTPUT
118* none
119* RETURNS
120* utlSUCCESS for success, utlFAILED for failure
121* DESCRIPTION
122* Handles error reporting from with-in a signal handler.
123*---------------------------------------------------------------------------*/
124void utlDoSigError(utlHERE_DECLARATION,
125 const char *msg_p,
126 ...)
127{
128 char buf[200];
129 size_t n;
130
131 utlAssert(msg_p != NULL);
132
133/* HACK: for now just print the error... */
134
135 /*--- accessing buffered I/O in signal handlers is not safe, so we'll do
136 it the hard way using only local variables and unbuffered I/O... */
137
138 n = 0;
139 (void)strncpy(buf + n, "SIG-ERROR:", sizeof(buf) - n); n = strlen(buf);
140 (void)strncpy(buf + n, "\n file path: ", sizeof(buf) - n); n = strlen(buf);
141 (void)strncpy(buf + n, utlHERE_FILE, sizeof(buf) - n); n = strlen(buf);
142 (void)strncpy(buf + n, "\n func name: ", sizeof(buf) - n); n = strlen(buf);
143 (void)strncpy(buf + n, utlHERE_FUNC, sizeof(buf) - n); n = strlen(buf);
144 (void)strncpy(buf + n, "\n line number: ", sizeof(buf) - n); n = strlen(buf);
145 (void)snprintf(buf + n, sizeof(buf) - n, "%u", (unsigned int)utlHERE_LINE); n = strlen(buf);
146 (void)strncpy(buf + n, "\n message: ", sizeof(buf) - n); n = strlen(buf);
147
148 /*--- append a formated message string ---*/
149 {
150 va_list va_arg_p;
151
152 va_start(va_arg_p, msg_p);
153 (void)vsnprintf(buf + n, sizeof(buf) - n, msg_p, va_arg_p); n = strlen(buf);
154 va_end(va_arg_p);
155 }
156
157 /*--- append a trailing <new-line> character ---*/
158 (void)strncpy(buf + n, "\n", sizeof(buf) - n); n = strlen(buf);
159
160 /*--- write output ---*/
161 {
162 char *buf_p;
163 ssize_t rv;
164
165 buf_p = buf;
166 for (; n > (size_t)0; buf_p += rv, n -= rv)
167 if ((rv = write(STDERR_FILENO, buf_p, n)) < (ssize_t)0)
168 break;
169 }
170}
171
172#ifdef utlTEST
173/*---------------------------------------------------------------------------*
174* FUNCTION
175* errorTest()
176* INPUT
177* none
178* OUTPUT
179* none
180* RETURNS
181* "true" for pass, "false" for failure
182*---------------------------------------------------------------------------*/
183bool errorTest(void)
184{
185 if (utlDoAssert(utlHERE, 1 == 1, "1 == 1") != true)
186 {
187 (void)fprintf(stderr, "errorTest: utlDoAssert(1) failed\n");
188 return false;
189 }
190
191 utlAssert(1);
192
193 if (utlDoAssert(utlHERE, 1 == 0, "1 == 0") != false)
194 {
195 (void)fprintf(stderr, "errorTest: utlDoAssert(2) failed\n");
196 return false;
197 }
198
199 utlError(errorTest, "Test message %d", 1);
200
201 utlSigError(errorTest1, "Test message %d", 2);
202
203 return true;
204}
205#endif /* utlTEST */
206