simulavr  1.1.0
avrerror.h
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
5  * Copyright (C) 2001, 2002 Theodore A. Roth
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  ****************************************************************************
22  *
23  * $Id$
24  */
25 
26 #ifndef SIM_AVRERROR_H
27 #define SIM_AVRERROR_H
28 
29 #include <iostream>
30 
31 #if defined(_MSC_VER) && !defined(SWIG)
32 #define ATTRIBUTE_NORETURN __declspec(noreturn)
33 #define ATTRIBUTE_PRINTF(string_arg, first_arg)
34 #elif defined(__GNUC__)
35 #ifndef ATTRIBUTE_NORETURN
36 #define ATTRIBUTE_NORETURN __attribute__((noreturn))
37 #endif
38 #ifndef ATTRIBUTE_PRINTF
39 #define ATTRIBUTE_PRINTF(string_arg, first_arg) __attribute__ ((format (printf, string_arg, first_arg)))
40 #endif
41 #else
42 #define ATTRIBUTE_NORETURN
43 #define ATTRIBUTE_PRINTF(string_arg, first_arg)
44 #endif
45 
48 
49  public:
51 
55 
57  void SetUseExit(bool useExit = true);
59  void SetMessageStream(std::ostream *s);
61  void SetWarningStream(std::ostream *s);
62 
64  void SetTraceFile(const char *name, unsigned int maxlines = 0);
66  void SetTraceStream(std::ostream *s);
68  void StopTrace(void);
70  bool GetTraceState(void) { return traceEnabled; }
72  std::ostream &traceOutStream(void) { return *traceStream; }
74  void TraceNextLine(void);
75 
77  void vfmessage(const char *fmt, ...)
78  ATTRIBUTE_PRINTF(2, 3);
80  void vfwarning(const char *file, int line, const char *fmt, ...)
81  ATTRIBUTE_PRINTF(4, 5);
83  void vferror(const char *file, int line, const char *fmt, ...)
84  ATTRIBUTE_PRINTF(4, 5);
87  void vffatal(const char *file, int line, const char *fmt, ...)
88  ATTRIBUTE_PRINTF(4, 5);
89 
92  void AbortApplication(int code);
95  void ExitApplication(int code);
96 
97  protected:
99  char formatStringBuffer[192];
101  std::ostream *msgStream;
102  std::ostream *wrnStream;
103  std::ostream *traceStream;
104  std::ostream *nullStream;
105  std::ofstream *fileTraceStream;
107  bool traceToFile;
108  std::string traceFilename;
109  unsigned int traceLinesOnFile;
110  unsigned int traceLines;
112 
114  char *getFormatString(const char *prefix, const char *file, int line, const char *fmtstr);
115 };
116 
119 
120 // redirect old definition ostream traceOut to SystemConsoleHandler.traceStream
121 #define traceOut sysConHandler.traceOutStream()
122 
124 extern int global_verbose_on;
127 
128 // moved from trace.h
130 void trioaccess(const char *t, unsigned char val);
131 
132 #define avr_message(...) sysConHandler.vfmessage(__VA_ARGS__)
133 #define avr_warning(...) sysConHandler.vfwarning(__FILE__, __LINE__, ## __VA_ARGS__)
134 #define avr_failure(...) sysConHandler.vferror(__FILE__, __LINE__, ## __VA_ARGS__)
135 #define avr_error(...) sysConHandler.vffatal(__FILE__, __LINE__, ## __VA_ARGS__)
136 
137 #endif /* SIM_AVRERROR_H */
#define ATTRIBUTE_PRINTF(string_arg, first_arg)
Definition: avrerror.h:43
char * getFormatString(const char *prefix, const char *file, int line, const char *fmtstr)
Creates the format string for formatting a message.
Definition: avrerror.cpp:218
SystemConsoleHandler()
creates a SystemConsoleHandler instance
Definition: avrerror.cpp:53
SystemConsoleHandler sysConHandler
The SystemConsoleHandler instance for common usage.
Definition: avrerror.cpp:234
#define ATTRIBUTE_NORETURN
Definition: avrerror.h:42
void SetUseExit(bool useExit=true)
Tells the handler, that exit/abort is to use instead of exceptions.
Definition: avrerror.cpp:71
std::string traceFilename
file name for trace file (will be appended with file count!)
Definition: avrerror.h:108
char formatStringBuffer[192]
Buffer for format strings to format a message.
Definition: avrerror.h:99
bool traceEnabled
flag, true if trace is enabled
Definition: avrerror.h:106
int traceFileCount
Counter for trace files.
Definition: avrerror.h:111
void void void ATTRIBUTE_NORETURN void vffatal(const char *file, int line, const char *fmt,...) ATTRIBUTE_PRINTF(4
Format and send a error message to stderr and call exit or raise a exception.
Definition: avrerror.cpp:188
void SetTraceFile(const char *name, unsigned int maxlines=0)
Sets the trace to file stream and enables tracing global.
Definition: avrerror.cpp:83
void void void ATTRIBUTE_NORETURN void ATTRIBUTE_NORETURN void AbortApplication(int code)
Aborts application: uses abort or exception depending on useExitAndAbort.
Definition: avrerror.cpp:202
Class, that handle messages to console and also exit/abort calls.
Definition: avrerror.h:47
ATTRIBUTE_NORETURN void ExitApplication(int code)
Exits application: uses exit or exception depending on useExitAndAbort.
Definition: avrerror.cpp:210
char messageStringBuffer[768]
Buffer for built message string itself, 4 times bigger than formatStringBuffer.
Definition: avrerror.h:100
std::ostream * wrnStream
Stream, where warning and error messages are sent to.
Definition: avrerror.h:102
void StopTrace(void)
Stops tracing global, close file, if set, redirect trace to nullStream.
Definition: avrerror.cpp:108
std::ostream & traceOutStream(void)
Gives Access to trace stream.
Definition: avrerror.h:72
unsigned int traceLines
how much lines are written on current trace file
Definition: avrerror.h:110
int global_verbose_on
Verbose enable flag.
Definition: avrerror.cpp:236
std::ostream * msgStream
Stream, where normal messages are sent to.
Definition: avrerror.h:101
void SetWarningStream(std::ostream *s)
Sets the output stream, where warnings and errors are sent to.
Definition: avrerror.cpp:79
bool GetTraceState(void)
Returns true, if tracing is global enabled.
Definition: avrerror.h:70
bool traceToFile
flag, true if trace writes to filestream
Definition: avrerror.h:107
bool global_suppress_memory_warnings
flag to suppress invalid memory usage warnings
Definition: avrerror.cpp:237
void TraceNextLine(void)
Ends a trace line, performs reopen new filestream, if necessary.
Definition: avrerror.cpp:121
void SetTraceStream(std::ostream *s)
Sets the trace to given stream and enables tracing global.
Definition: avrerror.cpp:96
std::ostream * traceStream
Stream for trace output.
Definition: avrerror.h:103
void void void vferror(const char *file, int line, const char *fmt,...) ATTRIBUTE_PRINTF(4
Format and send a error message to warning stream (default stderr)
Definition: avrerror.cpp:176
std::ostream * nullStream
/dev/null! ;-)
Definition: avrerror.h:104
void vfmessage(const char *fmt,...) ATTRIBUTE_PRINTF(2
Format and send a message to message stream (default stdout)
Definition: avrerror.cpp:147
void SetMessageStream(std::ostream *s)
Sets the output stream, where messages are sent to.
Definition: avrerror.cpp:75
bool useExitAndAbort
Flag, if exit/abort have to be used instead of exceptions.
Definition: avrerror.h:98
std::ofstream * fileTraceStream
open file stream for trace
Definition: avrerror.h:105
unsigned int traceLinesOnFile
how much lines will be written on one trace file 0->means endless
Definition: avrerror.h:109
void trioaccess(const char *t, unsigned char val)
Helper function for writing trace (trace IO access)
Definition: avrerror.cpp:239
void void vfwarning(const char *file, int line, const char *fmt,...) ATTRIBUTE_PRINTF(4
Format and send a warning message to warning stream (default stderr)
Definition: avrerror.cpp:164