summaryrefslogtreecommitdiff
path: root/packages/i2c/files/Log.h
blob: d12d482fc616faae544c1cafecb75649da19c095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/****************************************************************************
*
*   Copyright (c) 2006 Dave Hylands     <dhylands@gmail.com>
*
*   This program is free software; you can redistribute it and/or modify
*   it under the terms of the GNU General Public License version 2 as
*   published by the Free Software Foundation.
*
*   Alternatively, this software may be distributed under the terms of BSD
*   license.
*
*   See README and COPYING for more details.
*
****************************************************************************/
/**
*
*   @file   Log.h
*
*   @brief  Contains some logging macros.
*
****************************************************************************/
/**
*   @defgroup   Log   Logging
*
*   @brief      Provides a common interface for logging.
*
****************************************************************************/

#if !defined( LOG_H )
#define LOG_H                   ///< Include Guard

// ---- Include Files -------------------------------------------------------

#include <stdarg.h>
#include <stdio.h>

#if !defined( CONFIG_H )
#   include "Config.h"
#endif

#if !defined( CFG_LOG_USE_STDIO )
#   define  CFG_LOG_USE_STDIO   1
#endif


#if defined( AVR )
#   include <avr/pgmspace.h>
#   include <avr/interrupt.h>
#endif


#if CFG_LOG_TO_BUFFER
#   if !defined( CBUF_H )
#       include "CBUF.h"
#   endif

#endif



/**
 * @addtogroup Log
 * @{
 */

#if !defined( CFG_LOG_ENABLED )
#   define  CFG_LOG_ENABLED 1
#endif


#if !CFG_LOG_ENABLED

#define  Log( fmt, args... )
#define  LogError( fmt, args... )

#else


#if defined( __cplusplus )
extern "C"
{
#endif


/***************************************************************************
*
*   Log Buffer support
*/

#if CFG_LOG_TO_BUFFER

#if defined( AVR )

typedef struct
{
    const prog_char    *fmt;
    uint8_t             param1;
    uint8_t             param2;
#if CFG_LOG_EXTRA_PARAMS
    uint8_t             param3;
    uint8_t             param4;
#endif


} LOG_Entry_t;

#if CFG_LOG_EXTRA_PARAMS
#   define LOG_EXTRA_PARAMS_DECL    , uint8_t arg3, uint8_t arg4
#   define LOG_EXTRA_PARAMS         , 0, 0
#else

#   define LOG_EXTRA_PARAMS_DECL
#   define LOG_EXTRA_PARAMS
#endif


void LogBuf_P( const prog_char *fmt, uint8_t arg1, uint8_t arg2 LOG_EXTRA_PARAMS_DECL );

#define LogBuf0( fmt )              LogBuf_P( PSTR( fmt ), 0, 0       LOG_EXTRA_PARAMS )
#define LogBuf1( fmt, arg1 )        LogBuf_P( PSTR( fmt ), arg1, 0    LOG_EXTRA_PARAMS )
#define LogBuf2( fmt, arg1, arg2 )  LogBuf_P( PSTR( fmt ), arg1, arg2 LOG_EXTRA_PARAMS )

#if CFG_LOG_EXTRA_PARAMS
#define LogBuf3( fmt, arg1, arg2, arg3 )        LogBuf_P( PSTR( fmt ), arg1, arg2, arg3, 0 )
#define LogBuf4( fmt, arg1, arg2, arg3, arg4 )  LogBuf_P( PSTR( fmt ), arg1, arg2, arg3, arg4 )
#endif


#else


typedef struct
{
    const char         *fmt;
    uint8_t             param1;
    uint8_t             param2;

} LOG_Entry_t;

void LogBuf( const char *fmt, uint8_t arg1, uint8_t arg2 );

#define LogBuf0( fmt, arg1 )        LogBuf( fmt, 0, 0 )
#define LogBuf1( fmt, arg1 )        LogBuf( fmt, arg1, 0 )
#define LogBuf2( fmt, arg1, arg2 )  LogBuf( fmt, arg1, arg2 )

#endif  // AVR

#if ( CFG_LOG_NUM_BUFFER_ENTRIES > 128 )
typedef uint16_t    LOG_BufferIdx_t;
#else

typedef uint8_t     LOG_BufferIdx_t;
#endif


typedef struct
{
    LOG_BufferIdx_t m_getIdx;
    LOG_BufferIdx_t m_putIdx;
    LOG_Entry_t     m_entry[ CFG_LOG_NUM_BUFFER_ENTRIES ];

} LOG_Buffer_t;

extern  volatile    LOG_Buffer_t    LOG_gBuffer;

#define LOG_gBuffer_SIZE ( sizeof( LOG_gBuffer.m_entry ) / sizeof( LOG_gBuffer.m_entry[ 0 ] ))

void LogBufDump( void );

#endif  // CFG_LOG_TO_BUFFER

/***************************************************************************
*
*   Regular logging support
*/

#if CFG_LOG_USE_STDIO
extern FILE *gLogFs;

void LogInit( FILE *logFs );
#endif


#if defined( AVR )

void Log_P( const prog_char *fmt, ... );
void LogError_P( const prog_char *fmt, ... );
void vLog_P( const prog_char *fmt, va_list args );

#define Log( fmt, args... )         Log_P( PSTR( fmt ), ## args )
#define LogError( fmt, args... )    LogError_P( PSTR( fmt ), ## args )
#define vLog( fmt, va_list, args )  vLog_P( PSTR( fmt ), args )

#else   // AVR

#define LOG_LEVEL_NORMAL    0
#define LOG_LEVEL_ERROR     1

typedef void (*LogFunc_t)( int logLevel, const char *fmt, va_list args );

extern  int     gVerbose;
extern  int     gDebug;
extern  int     gQuiet;

void Log( const char *fmt, ... );
void LogError( const char *fmt, ... );
void vLog( const char *fmt, va_list args );
void vLogError( const char *fmt, va_list args );

#define Log_P( fmt, args... )       Log( fmt, ## args )
#define LogError_P( fmt, args... )  LogError( fmt, ## args )
#define vLog_P( fmt, args )         vLog( fmt, args )

#define LogDebug( fmt, args... )    do { if ( gDebug )   { Log( fmt, ## args ); }} while (0)
#define LogVerbose( fmt, args... )  do { if ( gVerbose ) { Log( fmt, ## args ); }} while (0)

void SetLogFunc( LogFunc_t logFunc );
void DefaultLogFunc( int logLevel, const char *fmt, va_list args );

#endif  // AVR

#if defined( __cplusplus )
}
#endif


#endif  // CFG_LOG_ENABLED

/** @} */

#endif // LOG_H