1 /*
2 This file is part of UFFS, the Ultra-low-cost Flash File System.
3
4 Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
5
6 UFFS is free software; you can redistribute it and/or modify it under
7 the GNU Library General Public License as published by the Free Software
8 Foundation; either version 2 of the License, or (at your option) any
9 later version.
10
11 UFFS is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 or GNU Library General Public License, as applicable, for more details.
15
16 You should have received a copy of the GNU General Public License
17 and GNU Library General Public License along with UFFS; if not, write
18 to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20
21 As a special exception, if other files instantiate templates or use
22 macros or inline functions from this file, or you compile this file
23 and link it with other works to produce a work based on this file,
24 this file does not by itself cause the resulting work to be covered
25 by the GNU General Public License. However the source code for this
26 file must still be made available in accordance with section (3) of
27 the GNU General Public License v2.
28
29 This exception does not invalidate any other reasons why a work based
30 on this file might be covered by the GNU General Public License.
31 */
32
33 /**
34 * \file uffs_debug.c
35 * \brief output debug messages
36 * \author Ricky Zheng, created 10th May, 2005
37 */
38 #include "uffs_config.h"
39 #include "uffs/uffs_os.h"
40 #include "uffs/uffs_public.h"
41
42 #if !defined(RT_THREAD)
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <string.h>
46
47 #define MAX_DEBUG_MSG_LINE_LENGTH 128
48
49 static struct uffs_DebugMsgOutputSt * m_ops = NULL;
50 static int m_msg_level = UFFS_MSG_NORMAL;
51
uffs_vprintf(const char * fmt,va_list args)52 static void uffs_vprintf(const char *fmt, va_list args)
53 {
54 char line_buf[MAX_DEBUG_MSG_LINE_LENGTH];
55
56 if (m_ops && m_ops->output) {
57 #ifdef _COMPILER_CLIB_DO_NOT_HAVE_VSNPRINTF_
58 vsprintf(line_buf, fmt, args);
59 #else
60 vsnprintf(line_buf, MAX_DEBUG_MSG_LINE_LENGTH, fmt, args);
61 #endif
62 m_ops->output(line_buf);
63 }
64 }
65
66 /**
67 * \brief Initialize debug message output functions
68 */
uffs_InitDebugMessageOutput(struct uffs_DebugMsgOutputSt * ops,int msg_level)69 URET uffs_InitDebugMessageOutput(struct uffs_DebugMsgOutputSt *ops, int msg_level)
70 {
71 m_ops = ops;
72
73 if (m_ops == NULL || m_ops->output == NULL) {
74 m_ops = NULL;
75 return U_FAIL;
76 }
77 else if (m_ops->vprintf == NULL)
78 m_ops->vprintf = uffs_vprintf;
79
80 m_msg_level = msg_level;
81
82 return U_SUCC;
83 }
84
uffs_DebugSetMessageLevel(int msg_level)85 void uffs_DebugSetMessageLevel(int msg_level)
86 {
87 m_msg_level = msg_level;
88 }
89 #endif
90
91 #ifdef CONFIG_ENABLE_UFFS_DEBUG_MSG
92
93 /**
94 * \brief The main debug message output function
95 */
96 #if !defined(RT_THREAD)
uffs_DebugMessage(int level,const char * prefix,const char * suffix,const char * errFmt,...)97 void uffs_DebugMessage(int level, const char *prefix,
98 const char *suffix, const char *errFmt, ...)
99 {
100 va_list arg;
101
102 if (m_ops && level >= m_msg_level) {
103 if (prefix)
104 m_ops->output(prefix);
105
106 va_start(arg, errFmt);
107 m_ops->vprintf(errFmt, arg);
108 va_end(arg);
109
110 if (suffix)
111 m_ops->output(suffix);
112 }
113 }
114
115 /**
116 * \brief Called when an assert occurred.
117 * This method is called when an assert occurred and should stop the
118 * application from running, as this there is a severe error condition.
119 * \param[in] file Source filename
120 * \param[in] line Source line of code
121 * \param[in] msg Assert message
122 */
uffs_AssertCall(const char * file,int line,const char * msg,...)123 void uffs_AssertCall(const char *file, int line, const char *msg, ...)
124 {
125 va_list args;
126 char buf[32];
127
128 if (m_ops && m_msg_level < UFFS_MSG_NOMSG) {
129 m_ops->output("ASSERT ");
130 m_ops->output(file);
131 sprintf(buf, ":%d - :", line);
132 m_ops->output(buf);
133 va_start(args, msg);
134 m_ops->vprintf(msg, args);
135 va_end(args);
136 m_ops->output(TENDSTR);
137 }
138 }
139 #endif
140
141 #ifdef _COMPILER_DO_NOT_SUPPORT_MACRO_VALIST_REPLACE_
uffs_Perror(int level,const char * fmt,...)142 void uffs_Perror(int level, const char *fmt, ...)
143 {
144 va_list args;
145 if (m_ops && level >= m_msg_level) {
146 va_start(args, fmt);
147 m_ops->vprintf(fmt, args);
148 va_end(args);
149 m_ops->output(TENDSTR);
150 }
151 }
152
uffs_PerrorRaw(int level,const char * fmt,...)153 void uffs_PerrorRaw(int level, const char *fmt, ...)
154 {
155 va_list args;
156 if (m_ops && level >= m_msg_level) {
157 va_start(args, fmt);
158 m_ops->vprintf(fmt, args);
159 va_end(args);
160 }
161 }
162
uffs_Assert(UBOOL expr,const char * fmt,...)163 UBOOL uffs_Assert(UBOOL expr, const char *fmt, ...)
164 {
165 va_list args;
166 if (m_ops && !expr && m_msg_level < UFFS_MSG_NOMSG) {
167 m_ops->output("ASSERT FAILED: ");
168 va_start(args, fmt);
169 m_ops->vprintf(fmt, args);
170 va_end(args);
171 m_ops->output(TENDSTR);
172 }
173 return expr ? U_TRUE : U_FALSE;
174 }
175 #endif
176
177 #else
178
uffs_DebugMessage(int level,const char * prefix,const char * suffix,const char * errFmt,...)179 void uffs_DebugMessage(int level, const char *prefix, const char *suffix, const char *errFmt, ...) {};
uffs_AssertCall(const char * file,int line,const char * msg,...)180 void uffs_AssertCall(const char *file, int line, const char *msg, ...) {};
181
182 #ifdef _COMPILER_DO_NOT_SUPPORT_MACRO_VALIST_REPLACE_
uffs_Perror(int level,const char * fmt,...)183 void uffs_Perror(int level, const char *fmt, ...) {}
uffs_PerrorRaw(int level,const char * fmt,...)184 void uffs_PerrorRaw(int level, const char *fmt, ...) {}
uffs_Assert(UBOOL expr,const char * fmt,...)185 UBOOL uffs_Assert(UBOOL expr, const char *fmt, ...) { return expr ? U_TRUE : U_FALSE; }
186 #endif
187
188 #endif
189