Subversion Repositories Projects

Rev

Rev 131 | Rev 230 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
40 dhylands 1
/****************************************************************************
2
*
3
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
4
*
5
*   This program is free software; you can redistribute it and/or modify
6
*   it under the terms of the GNU General Public License version 2 as
7
*   published by the Free Software Foundation.
8
*
9
*   Alternatively, this software may be distributed under the terms of BSD
10
*   license.
11
*
12
*   See README and COPYING for more details.
13
*
14
****************************************************************************/
15
/**
16
*
17
*   @file   Log.h
18
*
19
*   @brief  Contains some logging macros.
20
*
21
****************************************************************************/
22
/**
23
*   @defgroup   Log   Logging
24
*
25
*   @brief      Provides a common interface for logging.
26
*
27
****************************************************************************/
28
 
29
#if !defined( LOG_H )
30
#define LOG_H                   ///< Include Guard
31
 
32
// ---- Include Files -------------------------------------------------------
33
 
34
#include <stdarg.h>
35
 
36
#if !defined( CONFIG_H )
37
#   include "Config.h"
38
#endif
39
#if !defined( CFG_LOG_USE_STDIO )
40
#   define  CFG_LOG_USE_STDIO   1
41
#endif
42
 
43
#if defined( AVR )
213 dhylands 44
#   include <avr/pgmspace-fix.h>
40 dhylands 45
#   include <avr/interrupt.h>
213 dhylands 46
 
47
/* It turns out that newer versions of gcc have troubles with the
48
 * default PSTR macro when compiled using C++. This change
49
 * provides the equivalent behaviour, but gets rid of the
50
 * warning: only initialized variables can be placed into program memory area
51
 */
52
 
53
#define LOG_PROGMEM     PROGMEM
54
#define LOG_PSTR(s)      PSTR(s)
55
 
56
//#define LOG_PROGMEM __attribute__(( section(".progmem.data") ))
57
//#define LOG_PSTR(s) (__extension__({static char __c[] LOG_PROGMEM = (s); &__c[0];}))    
58
 
40 dhylands 59
#endif
60
 
61
#if CFG_LOG_TO_BUFFER
62
#   if !defined( CBUF_H )
63
#       include "CBUF.h"
64
#   endif
65
#endif
66
 
70 dhylands 67
#if !CFG_LOG_USE_STDIO
82 dhylands 68
 
69
#   if !defined( CFG_LOG_PUT_CHAR_FUNC )
70
#       define  CFG_LOG_PUT_CHAR_FUNC   UART0_PutChar
71
#       define  CFG_LOG_PUT_CHAR_HDR    "UART.h"
72
#   endif
73
 
70 dhylands 74
#   include CFG_LOG_PUT_CHAR_HDR
75
#endif
40 dhylands 76
 
70 dhylands 77
 
40 dhylands 78
/**
79
 * @addtogroup Log
80
 * @{
81
 */
82
 
83
#if !defined( CFG_LOG_ENABLED )
84
#   define  CFG_LOG_ENABLED 1
85
#endif
86
 
87
#if !CFG_LOG_ENABLED
88
 
89
#define  Log( fmt, args... )
90
#define  LogError( fmt, args... )
131 dhylands 91
#define  LogAssertFailed( exprStr, file, lineNum, function )
40 dhylands 92
 
131 dhylands 93
#define  ASSERT(expr)   ((void)0)
94
 
40 dhylands 95
#else
96
 
131 dhylands 97
#define  ASSERT(expr)   ((expr) ? ((void)0) : LogAssertFailed( #expr, __FILE__, __LINE__, __FUNCTION__ ), ((void)0))
98
 
40 dhylands 99
#if defined( __cplusplus )
100
extern "C"
101
{
102
#endif
103
 
125 dhylands 104
#if CFG_LOG_ALLOW_DEFERRED_NL
105
extern  int gDeferredNewline;
106
#endif
107
 
40 dhylands 108
/***************************************************************************
109
*
110
*   Log Buffer support
111
*/
112
 
113
#if CFG_LOG_TO_BUFFER
114
 
115
#if defined( AVR )
116
 
117
typedef struct
118
{
119
    const prog_char    *fmt;
120
    uint8_t             param1;
121
    uint8_t             param2;
122
#if CFG_LOG_EXTRA_PARAMS
123
    uint8_t             param3;
124
    uint8_t             param4;
125
#endif
126
 
127
} LOG_Entry_t;
128
 
129
#if CFG_LOG_EXTRA_PARAMS
130
#   define LOG_EXTRA_PARAMS_DECL    , uint8_t arg3, uint8_t arg4
131
#   define LOG_EXTRA_PARAMS         , 0, 0
132
#else
133
#   define LOG_EXTRA_PARAMS_DECL
134
#   define LOG_EXTRA_PARAMS
135
#endif
136
 
137
void LogBuf_P( const prog_char *fmt, uint8_t arg1, uint8_t arg2 LOG_EXTRA_PARAMS_DECL );
138
 
213 dhylands 139
#define LogBuf0( fmt )              LogBuf_P( LOG_PSTR( fmt ), 0, 0       LOG_EXTRA_PARAMS )
140
#define LogBuf1( fmt, arg1 )        LogBuf_P( LOG_PSTR( fmt ), arg1, 0    LOG_EXTRA_PARAMS )
141
#define LogBuf2( fmt, arg1, arg2 )  LogBuf_P( LOG_PSTR( fmt ), arg1, arg2 LOG_EXTRA_PARAMS )
40 dhylands 142
 
143
#if CFG_LOG_EXTRA_PARAMS
213 dhylands 144
#define LogBuf3( fmt, arg1, arg2, arg3 )        LogBuf_P( LOG_PSTR( fmt ), arg1, arg2, arg3, 0 )
145
#define LogBuf4( fmt, arg1, arg2, arg3, arg4 )  LogBuf_P( LOG_PSTR( fmt ), arg1, arg2, arg3, arg4 )
40 dhylands 146
#endif
147
 
148
#else
149
 
150
typedef struct
151
{
152
    const char         *fmt;
153
    uint8_t             param1;
154
    uint8_t             param2;
155
 
156
} LOG_Entry_t;
157
 
158
void LogBuf( const char *fmt, uint8_t arg1, uint8_t arg2 );
159
 
160
#define LogBuf0( fmt, arg1 )        LogBuf( fmt, 0, 0 )
161
#define LogBuf1( fmt, arg1 )        LogBuf( fmt, arg1, 0 )
162
#define LogBuf2( fmt, arg1, arg2 )  LogBuf( fmt, arg1, arg2 )
163
 
164
#endif  // AVR
165
 
166
#if ( CFG_LOG_NUM_BUFFER_ENTRIES > 128 )
167
typedef uint16_t    LOG_BufferIdx_t;
168
#else
169
typedef uint8_t     LOG_BufferIdx_t;
170
#endif
171
 
172
typedef struct
173
{
174
    LOG_BufferIdx_t m_getIdx;
175
    LOG_BufferIdx_t m_putIdx;
176
    LOG_Entry_t     m_entry[ CFG_LOG_NUM_BUFFER_ENTRIES ];
177
 
178
} LOG_Buffer_t;
179
 
180
extern  volatile    LOG_Buffer_t    LOG_gBuffer;
181
 
182
#define LOG_gBuffer_SIZE ( sizeof( LOG_gBuffer.m_entry ) / sizeof( LOG_gBuffer.m_entry[ 0 ] ))
183
 
184
void LogBufDump( void );
185
 
186
#endif  // CFG_LOG_TO_BUFFER
187
 
188
/***************************************************************************
189
*
190
*   Regular logging support
191
*/
192
 
193
#if CFG_LOG_USE_STDIO
131 dhylands 194
#include <stdio.h>
195
 
40 dhylands 196
extern FILE *gLogFs;
197
 
198
void LogInit( FILE *logFs );
199
#endif
200
 
201
#if defined( AVR )
202
 
213 dhylands 203
void Log_P( const char *fmt, ... );
204
void LogError_P( const char *fmt, ... );
131 dhylands 205
void LogAssertFailed_P( const char *exprStr, const char *file, unsigned lineNum, const char *function );
213 dhylands 206
void vLog_P( const char *fmt, va_list args );
40 dhylands 207
 
213 dhylands 208
#define Log( fmt, args... )         Log_P( LOG_PSTR( fmt ), ## args )
209
#define LogError( fmt, args... )    LogError_P( LOG_PSTR( fmt ), ## args )
210
#define LogAssertFailed( exprStr, file, lineNum, function )    LogAssertFailed_P( LOG_PSTR( expr ), LOG_PSTR( file ), lineNum, LOG_PSTR( function ))
211
#define vLog( fmt, va_list, args )  vLog_P( LOG_PSTR( fmt ), args )
40 dhylands 212
 
213
#else   // AVR
214
 
215
#define LOG_LEVEL_NORMAL    0
216
#define LOG_LEVEL_ERROR     1
131 dhylands 217
#define LOG_LEVEL_ASSERT    2
40 dhylands 218
 
219
typedef void (*LogFunc_t)( int logLevel, const char *fmt, va_list args );
220
 
221
extern  int     gQuiet;
222
 
223
void Log( const char *fmt, ... );
224
void LogError( const char *fmt, ... );
131 dhylands 225
void LogAssertFailed( const char *expr, const char *file, unsigned lineNum, const char *function );
40 dhylands 226
void vLog( const char *fmt, va_list args );
227
void vLogError( const char *fmt, va_list args );
228
 
131 dhylands 229
void LogFunc( int logLevel, const char *fmt, ... );
230
void vLogFunc( int logLevel, const char *fmt, va_list args );
231
 
40 dhylands 232
#define Log_P( fmt, args... )       Log( fmt, ## args )
233
#define LogError_P( fmt, args... )  LogError( fmt, ## args )
131 dhylands 234
#define LogAssertFailed_P( expr, file, lineNum, function )  LogAssertFailed( expr, file, line, function )
40 dhylands 235
#define vLog_P( fmt, args )         vLog( fmt, args )
236
 
237
void SetLogFunc( LogFunc_t logFunc );
238
void DefaultLogFunc( int logLevel, const char *fmt, va_list args );
239
 
240
#endif  // AVR
241
 
213 dhylands 242
extern  int     gVerbose;
243
extern  int     gDebug;
244
 
245
#define LogDebug( fmt, args... )    do { if ( gDebug )   { Log( fmt, ## args ); }} while (0)
246
#define LogVerbose( fmt, args... )  do { if ( gVerbose ) { Log( fmt, ## args ); }} while (0)
247
 
40 dhylands 248
#if defined( __cplusplus )
249
}
250
#endif
251
 
252
#endif  // CFG_LOG_ENABLED
253
 
254
/** @} */
255
 
256
#endif // LOG_H
257