OpenASIP  2.0
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
49 typedef 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()
124 const int DEFAULT_MAX_OUTPUT_LINES = 200;
125 
126 /// maximum length of an output line saved from popen() output in
127 /// runShellCommandAndGetOutput()
128 const int MAX_OUTPUT_LINE_LENGTH = 512;
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  */
142 class CmdLineOptions;
143 
144 class Application {
145 public:
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);
166  static int runShellCommandAndGetOutput(
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_);
191  static CmdLineOptions* cmdLineOptions();
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;
211  virtual ~UnixSignalHandler() {}
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 
231 private:
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
Application::installationRoot_
static std::string installationRoot_
Path to the TCE installation root.
Definition: Application.hh:261
Application::finalize
static void finalize()
Definition: Application.cc:199
Application::errorStream_
static std::ostream * errorStream_
The stream for user error notifications.
Definition: Application.hh:242
Application::getSignalHandler
static UnixSignalHandler * getSignalHandler(int signalNum)
Definition: Application.cc:445
Application::restoreSignalHandler
static void restoreSignalHandler(int signalNum)
Definition: Application.cc:471
Application::runShellCommandAndGetOutput
static int runShellCommandAndGetOutput(const std::string &command, std::vector< std::string > &outputLines, std::size_t maxOutputLines=DEFAULT_MAX_OUTPUT_LINES, bool includeStdErr=false)
Definition: Application.cc:338
Application::writeToErrorLog
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)
Definition: Application.cc:224
Application::exitProgram
static void exitProgram(const int status=EXIT_SUCCESS)
Definition: Application.cc:258
Application::VERBOSE_LEVEL_DEFAULT
static const int VERBOSE_LEVEL_DEFAULT
Default verbose level - do not print anything unnecessary.
Definition: Application.hh:222
Application::setVerboseLevel
static void setVerboseLevel(const int level=VERBOSE_LEVEL_DEFAULT)
Definition: Application.hh:186
Application::setCmdLineOptions
static void setCmdLineOptions(CmdLineOptions *options_)
Definition: Application.cc:381
Application::verboseLevel
static int verboseLevel()
Definition: Application.hh:176
Application::logStream
static std::ostream & logStream()
Definition: Application.cc:155
Application::setSignalHandler
static void setSignalHandler(int signalNum, UnixSignalHandler &handler)
Definition: Application.cc:417
Application
Definition: Application.hh:144
Application::logStream_
static std::ostream * logStream_
The stream for debug logging.
Definition: Application.hh:239
Application::UnixSignalHandler::~UnixSignalHandler
virtual ~UnixSignalHandler()
Definition: Application.hh:211
Application::increasedVerbose
static bool increasedVerbose()
Definition: Application.hh:179
Application::UnixSignalHandler
Definition: Application.hh:203
Application::argc
static int argc()
Definition: Application.hh:192
Application::spamVerbose
static bool spamVerbose()
Definition: Application.hh:182
Application::LOOP_OPT_DEFAULT
static const bool LOOP_OPT_DEFAULT
Use Loop Optimizations.
Definition: Application.hh:229
CmdLineOptions
Definition: CmdLineOptions.hh:54
Application::initialized_
static bool initialized_
True when initialize() is called. Ensures that initialization is done only once.
Definition: Application.hh:236
Application::cmdLineOptions
static CmdLineOptions * cmdLineOptions()
Definition: Application.cc:397
MAX_OUTPUT_LINE_LENGTH
const int MAX_OUTPUT_LINE_LENGTH
maximum length of an output line saved from popen() output in runShellCommandAndGetOutput()
Definition: Application.hh:128
Application::warningStream_
static std::ostream * warningStream_
The stream for user error notifications.
Definition: Application.hh:245
Application::VERBOSE_LEVEL_INCREASED
static const int VERBOSE_LEVEL_INCREASED
Increased verbose level - print information about modules etc.
Definition: Application.hh:224
Application::runShellCommandSilently
static int runShellCommandSilently(const std::string &command)
Definition: Application.cc:301
Application::cmdLineOptions_
static CmdLineOptions * cmdLineOptions_
Holds command line options passed to program.
Definition: Application.hh:254
Application::signalHandlers_
static std::map< int, UnixSignalHandler * > signalHandlers_
Signal handlers in a map associated by their signal numbers.
Definition: Application.hh:248
Application::isInstalled
static bool isInstalled()
Definition: Application.cc:522
Application::argv
static char ** argv()
Definition: Application.hh:193
Application::errorStream
static std::ostream & errorStream()
Definition: Application.cc:171
Application::argv_
static char ** argv_
Definition: Application.hh:258
Application::installationDir
static std::string installationDir()
Definition: Application.cc:537
Application::initialize
static void initialize()
Definition: Application.cc:99
Application::argc_
static int argc_
The original argc and argv given to the main program, if applicable.
Definition: Application.hh:257
Application::VERBOSE_LEVEL_SPAM
static const int VERBOSE_LEVEL_SPAM
More Increased verbose level - spam about ddg heights of loops.
Definition: Application.hh:226
Application::warningStream
static std::ostream & warningStream()
Definition: Application.cc:188
DEFAULT_MAX_OUTPUT_LINES
const int DEFAULT_MAX_OUTPUT_LINES
default value of maximum amount of output lines saved from popen() output in runShellCommandAndGetOut...
Definition: Application.hh:124
Application::unexpectedExceptionHandler
static void unexpectedExceptionHandler()
Definition: Application.cc:274
Application::UnixSignalHandler::execute
virtual void execute(int data, siginfo_t *info)=0
Application::verboseLevel_
static int verboseLevel_
Verbose level directs how much output will be printed to console.
Definition: Application.hh:251
Application::abortProgram
static void abortProgram() __attribute__((noreturn))
Definition: Application.cc:266
Application::TCEVersionString
static std::string TCEVersionString()
Definition: Application.cc:510
Application::signalRedirector
static void signalRedirector(int data, siginfo_t *info, void *context)
Definition: Application.cc:494
Application::shellCommandsExists
static bool shellCommandsExists(const std::string &commands)
Definition: Application.cc:290