OpenASIP 2.2
Loading...
Searching...
No Matches
Application.hh
Go to the documentation of this file.
1/*
2 Copyright (c) 2002-2012 Tampere University.
3
4 This file is part of TTA-Based Codesign Environment (TCE).
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24/**
25 * @file Application.hh
26 *
27 * Declaration of Application class and services of standalone applications.
28 *
29 * Application is a class for generic services that are project-wide
30 * applicable to standalone applications or modules. These services include
31 * assertion, program exiting, debugging to a log file, catching unexpected
32 * exceptions and "control-c", SIGFPE and SIGSEGV signal handling.
33 *
34 * @author Atte Oksman 2003 (oksman-no.spam-cs.tut.fi)
35 * @author Pekka Jääskeläinen 2005-2012 (pjaaskel-no.spam-cs.tut.fi)
36 */
37
38#ifndef TTA_APPLICATION_HH
39#define TTA_APPLICATION_HH
40
41#include <cstdlib>
42#include <string>
43#include <iostream>
44#include <vector>
45#ifndef __MINGW32__
46# include <signal.h>
47#else
48// provide a dummy type to allow dummy signal functionality
49typedef int siginfo_t;
50
51#endif
52#include <map>
53
54#ifdef assert
55#undef assert
56#endif
57
58#define UNKNOWN_FUNCTION "[?]"
59
60// taken from
61// http://gcc.gnu.org/onlinedocs/gcc-3.4.1/gcc/Function-Names.html#
62// Function%20Names
63#if __STDC_VERSION__ < 199901L
64# if __GNUC__ >= 2
65# define __func__ __PRETTY_FUNCTION__
66# else
67# define __func__ UNKNOWN_FUNCTION
68# endif
69#endif
70
71// abort the program with an error message
72#define abortWithError(message) \
73 Application::writeToErrorLog(__FILE__, __LINE__, __func__, message); \
74 Application::abortProgram();
75
76#ifdef NDEBUG
77
78# define assert(condition)
79
80#else
81
82// provide a TCE version of assert macro for error logging
83#ifdef NDEBUG
84#define assert(condition)
85#else
86#define assert(condition) \
87 if (!(condition)) { \
88 abortWithError("Assertion failed: " #condition); \
89 }
90#endif
91
92#endif
93
94// provide an easy way to "debug print"
95#define debugLog(text) \
96 Application::writeToErrorLog(__FILE__, __LINE__, __func__, \
97 std::string("DEBUG: ") + std::string(text))
98
99// provide an easy way to print out exception data
100#define CATCH_ANY(XXX__) \
101 try { XXX__; } \
102 catch (const Exception& e) { \
103 debugLog(e.errorMessage() + " in " + \
104 e.fileName() + ":" + \
105 e.procedureName() + ":" + \
106 Conversion::toString(e.lineNum())); } \
107 catch ( ... ) { debugLog("Unknown exception"); }
108
109// easy way to do conditional verbose logging
110#define verboseLogC(text, neededVerbosity) \
111 if (Application::verboseLevel() >= neededVerbosity) { \
112 Application::logStream() << text << std::endl; }
113
114// provide an easy way to verbose log printing
115#define verboseLog(text) verboseLogC(text, 1)
116
117// provide an easy way to print out the contents of a variable
118#define PRINT_VAR(VARIABLE__) \
119 Application::logStream() << #VARIABLE__ << " == " \
120 << VARIABLE__ << std::endl
121
122/// default value of maximum amount of output lines saved from popen() output
123/// in runShellCommandAndGetOutput()
125
126/// maximum length of an output line saved from popen() output in
127/// runShellCommandAndGetOutput()
129
130/**
131 * A project-wide class for basic application services.
132 *
133 * Provides application services such as program exit and abort. It also
134 * provides generic debugging aids like error logging and handling of
135 * unexpected exceptions.
136 *
137 * Initialization of this toolkit happens when any function is called the
138 * first time.
139 *
140 * Should be used by every standalone application of the toolset.
141 */
142class CmdLineOptions;
143
145public:
146 static void initialize(int argc, char* argv[]);
147 static void initialize();
148 static void finalize();
149
150 static void writeToErrorLog(
151 const std::string fileName,
152 const int lineNumber,
153 const std::string functionName,
154 const std::string message,
155 const int neededVerbosity = 0);
156
157 static void exitProgram(const int status = EXIT_SUCCESS);
158 static void abortProgram() __attribute__((noreturn));
159
160 static void unexpectedExceptionHandler();
161
162 static bool shellCommandsExists(
163 const std::string& commands);
164 static int runShellCommandSilently(
165 const std::string& command);
167 const std::string& command,
168 std::vector<std::string>& outputLines,
169 std::size_t maxOutputLines = DEFAULT_MAX_OUTPUT_LINES,
170 bool includeStdErr = false);
171
172 static std::ostream& logStream();
173 static std::ostream& warningStream();
174 static std::ostream& errorStream();
175
176 static int verboseLevel() {
177 return verboseLevel_;
178 }
179 static bool increasedVerbose() {
181 }
182 static bool spamVerbose() {
184 }
185
186 static void setVerboseLevel(const int level = VERBOSE_LEVEL_DEFAULT) {
187 verboseLevel_ = level;
188 }
189
190 static void setCmdLineOptions(CmdLineOptions* options_);
192 static int argc() { return argc_; }
193 static char** argv() { return argv_; }
194 static bool isInstalled();
195 static std::string installationDir();
196
197 /// ---------------------
198
199 /**
200 * An interface for classes that can receive notification when a Unix
201 * signal occurs.
202 */
204 public:
205 /**
206 * Executed when the Unix signal occurs.
207 *
208 * @param data Data of the signal.
209 */
210 virtual void execute(int data, siginfo_t *info) = 0;
212 };
213
214 // Unix signal handler set/reset functions
215 static void setSignalHandler(int signalNum, UnixSignalHandler& handler);
216 static UnixSignalHandler* getSignalHandler(int signalNum);
217 static void restoreSignalHandler(int signalNum);
218
219 static std::string TCEVersionString();
220
221 /// Default verbose level - do not print anything unnecessary
222 static const int VERBOSE_LEVEL_DEFAULT = 0;
223 /// Increased verbose level - print information about modules etc
224 static const int VERBOSE_LEVEL_INCREASED = 1;
225 /// More Increased verbose level - spam about ddg heights of loops
226 static const int VERBOSE_LEVEL_SPAM = 2;
227
228 /// Use Loop Optimizations
229 static const bool LOOP_OPT_DEFAULT = false;
230
231private:
232 static void signalRedirector(int data, siginfo_t *info, void *context);
233
234 /// True when initialize() is called. Ensures that initialization is
235 /// done only once.
236 static bool initialized_;
237
238 /// The stream for debug logging.
239 static std::ostream* logStream_;
240
241 /// The stream for user error notifications.
242 static std::ostream* errorStream_;
243
244 /// The stream for user error notifications.
245 static std::ostream* warningStream_;
246
247 /// Signal handlers in a map associated by their signal numbers
248 static std::map<int, UnixSignalHandler*> signalHandlers_;
249
250 /// Verbose level directs how much output will be printed to console
251 static int verboseLevel_;
252
253 /// Holds command line options passed to program
255
256 /// The original argc and argv given to the main program, if applicable.
257 static int argc_;
258 static char** argv_;
259
260 /// Path to the TCE installation root
261 static std::string installationRoot_;
262};
263
264#endif
const int MAX_OUTPUT_LINE_LENGTH
maximum length of an output line saved from popen() output in runShellCommandAndGetOutput()
const int DEFAULT_MAX_OUTPUT_LINES
default value of maximum amount of output lines saved from popen() output in runShellCommandAndGetOut...
virtual void execute(int data, siginfo_t *info)=0
static void setCmdLineOptions(CmdLineOptions *options_)
static CmdLineOptions * cmdLineOptions()
static std::ostream & warningStream()
static std::map< int, UnixSignalHandler * > signalHandlers_
Signal handlers in a map associated by their signal numbers.
static char ** argv()
static bool shellCommandsExists(const std::string &commands)
static bool isInstalled()
static void exitProgram(const int status=EXIT_SUCCESS)
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
static std::string installationDir()
static void finalize()
static void unexpectedExceptionHandler()
static bool increasedVerbose()
static std::string installationRoot_
Path to the TCE installation root.
static const int VERBOSE_LEVEL_INCREASED
Increased verbose level - print information about modules etc.
static UnixSignalHandler * getSignalHandler(int signalNum)
static const int VERBOSE_LEVEL_SPAM
More Increased verbose level - spam about ddg heights of loops.
static void setSignalHandler(int signalNum, UnixSignalHandler &handler)
static std::ostream * logStream_
The stream for debug logging.
static void abortProgram() __attribute__((noreturn))
static CmdLineOptions * cmdLineOptions_
Holds command line options passed to program.
static bool spamVerbose()
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
static int runShellCommandSilently(const std::string &command)
static void setVerboseLevel(const int level=VERBOSE_LEVEL_DEFAULT)
static bool initialized_
True when initialize() is called. Ensures that initialization is done only once.
static const bool LOOP_OPT_DEFAULT
Use Loop Optimizations.
static std::ostream * errorStream_
The stream for user error notifications.
static const int VERBOSE_LEVEL_DEFAULT
Default verbose level - do not print anything unnecessary.
static std::string TCEVersionString()
static char ** argv_
static int argc()
static std::ostream & errorStream()
static int verboseLevel_
Verbose level directs how much output will be printed to console.
static int verboseLevel()
static int argc_
The original argc and argv given to the main program, if applicable.
static std::ostream * warningStream_
The stream for user error notifications.
static std::ostream & logStream()
static void restoreSignalHandler(int signalNum)
static void initialize()
static void signalRedirector(int data, siginfo_t *info, void *context)