simulavr  1.1.0
avrerror.cpp
Go to the documentation of this file.
1 /*
2  *
3  ****************************************************************************
4  *
5  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
6  * Copyright (C) 2001, 2002 Theodore A. Roth
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  ****************************************************************************
23  *
24  * $Id$
25  */
26 
35 #include <fstream>
36 #include <sstream>
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <stdarg.h>
41 #include <string.h>
42 
43 #include "avrerror.h"
44 #include "helper.h"
45 
46 /* for preprocessor symbol HAVE_SYS_MINGW */
47 #include "config.h"
48 
49 #ifdef _MSC_VER
50 # define snprintf _snprintf
51 #endif
52 
54  useExitAndAbort = true;
55  nullStream = new std::ostream(0);
56  msgStream = &std::cout;
57  wrnStream = &std::cerr;
59  fileTraceStream = nullptr;
60  traceEnabled = false;
61  traceToFile = false;
62 }
63 
65  StopTrace();
66  if(traceToFile)
67  delete traceStream;
68  delete nullStream;
69 }
70 
72  useExitAndAbort = useExit;
73 }
74 
76  msgStream = s;
77 }
78 
80  wrnStream = s;
81 }
82 
83 void SystemConsoleHandler::SetTraceFile(const char *name, unsigned int maxlines) {
84  StopTrace();
85  fileTraceStream = new std::ofstream();
86  fileTraceStream->open(name);
87  traceFilename = name;
89  traceFileCount = 1;
90  traceLinesOnFile = maxlines;
91  traceLines = 0;
92  traceEnabled = true;
93  traceToFile = true;
94 }
95 
96 void SystemConsoleHandler::SetTraceStream(std::ostream *s) {
97  StopTrace();
98  traceStream = s;
99  if(fileTraceStream != nullptr) {
100  fileTraceStream->close();
101  delete fileTraceStream;
102  fileTraceStream = nullptr;
103  }
104  traceEnabled = true;
105  traceToFile = false;
106 }
107 
109  if(!traceEnabled)
110  return;
111  if(traceToFile) {
112  fileTraceStream->close();
113  delete fileTraceStream;
114  fileTraceStream = nullptr;
115  }
117  traceEnabled = false;
118  traceToFile = false;
119 }
120 
122  if(!traceEnabled || !traceToFile)
123  return;
124 
125  traceLines++;
127  traceFileCount++;
128  traceLines = 0;
129 
130  fileTraceStream->close();
131  delete fileTraceStream;
132 
133  std::ostringstream n;
134  std::size_t idx = (int)traceFilename.rfind('.');
135  if(idx == std::string::npos) {
136  n << traceFilename << "_" << traceFileCount;
137  } else {
138  n << traceFilename.substr(0, idx) << "_" << traceFileCount << traceFilename.substr(idx);
139  }
140  fileTraceStream = new std::ofstream();
141  fileTraceStream->open(n.str().c_str());
142 
144  }
145 }
146 
147 void SystemConsoleHandler::vfmessage(const char *fmt, ...) {
148  if ( ! global_verbose_on)
149  return;
150 
151  va_list ap;
152  snprintf(formatStringBuffer, sizeof(formatStringBuffer),
153  "MESSAGE %s", fmt);
154  va_start(ap, fmt);
155  vsnprintf(messageStringBuffer, sizeof(messageStringBuffer),
156  formatStringBuffer, ap);
157  va_end(ap);
159  if(fmt[strlen(fmt) - 1] != '\n')
160  *msgStream << std::endl;
161  msgStream->flush();
162 }
163 
164 void SystemConsoleHandler::vfwarning(const char *file, int line, const char *fmt, ...) {
165  va_list ap;
166  char *mfmt = getFormatString("WARNING", file, line, fmt);
167  va_start(ap, fmt);
168  vsnprintf(messageStringBuffer, sizeof(messageStringBuffer), mfmt, ap);
169  va_end(ap);
171  if(fmt[strlen(fmt) - 1] != '\n')
172  *wrnStream << std::endl;
173  wrnStream->flush();
174 }
175 
176 void SystemConsoleHandler::vferror(const char *file, int line, const char *fmt, ...) {
177  va_list ap;
178  char *mfmt = getFormatString("ERROR", file, line, fmt);
179  va_start(ap, fmt);
180  vsnprintf(messageStringBuffer, sizeof(messageStringBuffer), mfmt, ap);
181  va_end(ap);
183  if(fmt[strlen(fmt) - 1] != '\n')
184  *wrnStream << std::endl;
185  wrnStream->flush();
186 }
187 
188 void SystemConsoleHandler::vffatal(const char *file, int line, const char *fmt, ...) {
189  va_list ap;
190  char *mfmt = getFormatString("FATAL", file, line, fmt);
191  va_start(ap, fmt);
192  vsnprintf(messageStringBuffer, sizeof(messageStringBuffer), mfmt, ap);
193  va_end(ap);
194  if(useExitAndAbort) {
195  *wrnStream << "\n" << messageStringBuffer << "\n" << std::endl;
196  exit(1);
197  } else {
198  throw (char const*)messageStringBuffer;
199  }
200 }
201 
203  if(useExitAndAbort) {
204  abort();
205  } else {
206  throw -code;
207  }
208 }
209 
211  if(useExitAndAbort) {
212  exit(code);
213  } else {
214  throw code;
215  }
216 }
217 
218 char* SystemConsoleHandler::getFormatString(const char *prefix,
219  const char *file,
220  int line,
221  const char *fmtstr) {
222  snprintf(formatStringBuffer,
223  sizeof(formatStringBuffer),
224  "%s: file %s: line %d: %s",
225  prefix,
226  file,
227  line,
228  fmtstr);
229  formatStringBuffer[sizeof(formatStringBuffer) - 1] = '\0';
230  return formatStringBuffer;
231 }
232 
233 // create the handler instance
235 
238 
239 void trioaccess(const char *t, unsigned char val) {
240  sysConHandler.traceOutStream() << t << "=" << HexChar(val) << " ";
241 }
242 
243 // EOF
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
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
Definition: helper.h:31
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
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
void trioaccess(const char *t, unsigned char val)
Helper function for writing trace (trace IO access)
Definition: avrerror.cpp:239
bool traceToFile
flag, true if trace writes to filestream
Definition: avrerror.h:107
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
int global_verbose_on
Verbose enable flag.
Definition: avrerror.cpp:236
std::ostream * nullStream
/dev/null! ;-)
Definition: avrerror.h:104
bool global_suppress_memory_warnings
flag to suppress invalid memory usage warnings
Definition: avrerror.cpp:237
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 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