OpenASIP 2.2
Loading...
Searching...
No Matches
Classes | Macros | Variables
Application.hh File Reference
#include <cstdlib>
#include <string>
#include <iostream>
#include <vector>
#include <signal.h>
#include <map>
Include dependency graph for Application.hh:

Go to the source code of this file.

Classes

class  Application
 
class  Application::UnixSignalHandler
 

Macros

#define UNKNOWN_FUNCTION   "[?]"
 
#define __func__   UNKNOWN_FUNCTION
 
#define abortWithError(message)
 
#define assert(condition)
 
#define debugLog(text)
 
#define CATCH_ANY(XXX__)
 
#define verboseLogC(text, neededVerbosity)
 
#define verboseLog(text)   verboseLogC(text, 1)
 
#define PRINT_VAR(VARIABLE__)
 

Variables

const int DEFAULT_MAX_OUTPUT_LINES = 200
 default value of maximum amount of output lines saved from popen() output in runShellCommandAndGetOutput()
 
const int MAX_OUTPUT_LINE_LENGTH = 512
 maximum length of an output line saved from popen() output in runShellCommandAndGetOutput()
 

Detailed Description

Declaration of Application class and services of standalone applications.

Application is a class for generic services that are project-wide applicable to standalone applications or modules. These services include assertion, program exiting, debugging to a log file, catching unexpected exceptions and "control-c", SIGFPE and SIGSEGV signal handling.

Author
Atte Oksman 2003 (oksman-no.spam-cs.tut.fi)
Pekka Jääskeläinen 2005-2012 (pjaaskel-no.spam-cs.tut.fi)

Definition in file Application.hh.

Macro Definition Documentation

◆ __func__

#define __func__   UNKNOWN_FUNCTION

Definition at line 67 of file Application.hh.

◆ abortWithError

#define abortWithError (   message)
Value:
Application::writeToErrorLog(__FILE__, __LINE__, __func__, message); \
Application::abortProgram();
#define __func__
static void writeToErrorLog(const std::string fileName, const int lineNumber, const std::string functionName, const std::string message, const int neededVerbosity=0)

Definition at line 72 of file Application.hh.

86 { \
87 abortWithError("Assertion failed: " #condition); \
88 }
89#endif
90
91#endif
92
93// provide an easy way to "debug print"
94#define debugLog(text) \
95 Application::writeToErrorLog(__FILE__, __LINE__, __func__, \
96 std::string("DEBUG: ") + std::string(text))
97
98// provide an easy way to print out exception data
99#define CATCH_ANY(XXX__) \
100 try { XXX__; } \
101 catch (const Exception& e) { \
102 debugLog(e.errorMessage() + " in " + \
103 e.fileName() + ":" + \
104 e.procedureName() + ":" + \
105 Conversion::toString(e.lineNum())); } \
106 catch ( ... ) { debugLog("Unknown exception"); }
107
108// easy way to do conditional verbose logging
109#define verboseLogC(text, neededVerbosity) \
110 if (Application::verboseLevel() >= neededVerbosity) { \
111 Application::logStream() << text << std::endl; }
112
113// provide an easy way to verbose log printing
114#define verboseLog(text) verboseLogC(text, 1)
115
116// provide an easy way to print out the contents of a variable
117#define PRINT_VAR(VARIABLE__) \
118 Application::logStream() << #VARIABLE__ << " == " \
119 << VARIABLE__ << std::endl
120
121/// default value of maximum amount of output lines saved from popen() output
122/// in runShellCommandAndGetOutput()
123const int DEFAULT_MAX_OUTPUT_LINES = 200;
124
125/// maximum length of an output line saved from popen() output in
126/// runShellCommandAndGetOutput()
127const int MAX_OUTPUT_LINE_LENGTH = 512;
128
129/**
130 * A project-wide class for basic application services.
131 *
132 * Provides application services such as program exit and abort. It also
133 * provides generic debugging aids like error logging and handling of
134 * unexpected exceptions.
135 *
136 * Initialization of this toolkit happens when any function is called the
137 * first time.
138 *
139 * Should be used by every standalone application of the toolset.
140 */
141class CmdLineOptions;
142
143class Application {
144public:
145 static void initialize(int argc, char* argv[]);
146 static void initialize();
147 static void finalize();
148
149 static void writeToErrorLog(
150 const std::string fileName,
151 const int lineNumber,
152 const std::string functionName,
153 const std::string message,
154 const int neededVerbosity = 0);
155
156 static void exitProgram(const int status = EXIT_SUCCESS);
157 static void abortProgram() __attribute__((noreturn));
158
159 static void unexpectedExceptionHandler();
160
161 static bool shellCommandsExists(
162 const std::string& commands);
163 static int runShellCommandSilently(
164 const std::string& command);
166 const std::string& command,
167 std::vector<std::string>& outputLines,
168 std::size_t maxOutputLines = DEFAULT_MAX_OUTPUT_LINES,
169 bool includeStdErr = false);
170
171 static std::ostream& logStream();
172 static std::ostream& warningStream();
173 static std::ostream& errorStream();
174
175 static int verboseLevel() {
176 return verboseLevel_;
177 }
178 static bool increasedVerbose() {
180 }
181 static bool spamVerbose() {
183 }
184
185 static void setVerboseLevel(const int level = VERBOSE_LEVEL_DEFAULT) {
186 verboseLevel_ = level;
187 }
188
189 static void setCmdLineOptions(CmdLineOptions* options_);
191 static int argc() { return argc_; }
192 static char** argv() { return argv_; }
193 static bool isInstalled();
194 static std::string installationDir();
195
196 /// ---------------------
197
198 /**
199 * An interface for classes that can receive notification when a Unix
200 * signal occurs.
201 */
202 class UnixSignalHandler {
203 public:
204 /**
205 * Executed when the Unix signal occurs.
206 *
207 * @param data Data of the signal.
208 */
209 virtual void execute(int data, siginfo_t *info) = 0;
210 virtual ~UnixSignalHandler() {}
211 };
212
213 // Unix signal handler set/reset functions
214 static void setSignalHandler(int signalNum, UnixSignalHandler& handler);
215 static UnixSignalHandler* getSignalHandler(int signalNum);
216 static void restoreSignalHandler(int signalNum);
217
218 static std::string TCEVersionString();
219
220 /// Default verbose level - do not print anything unnecessary
221 static const int VERBOSE_LEVEL_DEFAULT = 0;
222 /// Increased verbose level - print information about modules etc
223 static const int VERBOSE_LEVEL_INCREASED = 1;
224 /// More Increased verbose level - spam about ddg heights of loops
225 static const int VERBOSE_LEVEL_SPAM = 2;
226
227 /// Use Loop Optimizations
228 static const bool LOOP_OPT_DEFAULT = false;
229
230private:
231 static void signalRedirector(int data, siginfo_t *info, void *context);
232
233 /// True when initialize() is called. Ensures that initialization is
234 /// done only once.
235 static bool initialized_;
236
237 /// The stream for debug logging.
238 static std::ostream* logStream_;
239
240 /// The stream for user error notifications.
241 static std::ostream* errorStream_;
242
243 /// The stream for user error notifications.
244 static std::ostream* warningStream_;
245
246 /// Signal handlers in a map associated by their signal numbers
247 static std::map<int, UnixSignalHandler*> signalHandlers_;
248
249 /// Verbose level directs how much output will be printed to console
250 static int verboseLevel_;
251
252 /// Holds command line options passed to program
254
255 /// The original argc and argv given to the main program, if applicable.
256 static int argc_;
257 static char** argv_;
258
259 /// Path to the TCE installation root
260 static std::string installationRoot_;
261};
262
263#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...
find Finds info of the inner loops in the false
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 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)

◆ assert

#define assert (   condition)
Value:
if (!(condition)) { \
abortWithError("Assertion failed: " #condition); \
}

Definition at line 86 of file Application.hh.

87 { \
88 abortWithError("Assertion failed: " #condition); \
89 }

◆ CATCH_ANY

#define CATCH_ANY (   XXX__)
Value:
try { XXX__; } \
catch (const Exception& e) { \
debugLog(e.errorMessage() + " in " + \
e.fileName() + ":" + \
e.procedureName() + ":" + \
catch ( ... ) { debugLog("Unknown exception"); }
#define debugLog(text)
static std::string toString(const T &source)
std::string fileName() const
std::string errorMessage() const
Definition Exception.cc:123
std::string procedureName() const
int lineNum() const

Definition at line 100 of file Application.hh.

101 { XXX__; } \
102 catch (const Exception& e) { \
103 debugLog(e.errorMessage() + " in " + \
104 e.fileName() + ":" + \
105 e.procedureName() + ":" + \
107 catch ( ... ) { debugLog("Unknown exception"); }

◆ debugLog

#define debugLog (   text)
Value:
Application::writeToErrorLog(__FILE__, __LINE__, __func__, \
std::string("DEBUG: ") + std::string(text))

Definition at line 95 of file Application.hh.

97 : ") + std::string(text))

◆ PRINT_VAR

#define PRINT_VAR (   VARIABLE__)
Value:
Application::logStream() << #VARIABLE__ << " == " \
<< VARIABLE__ << std::endl

Definition at line 118 of file Application.hh.

◆ UNKNOWN_FUNCTION

#define UNKNOWN_FUNCTION   "[?]"

Definition at line 58 of file Application.hh.

◆ verboseLog

#define verboseLog (   text)    verboseLogC(text, 1)

Definition at line 115 of file Application.hh.

◆ verboseLogC

#define verboseLogC (   text,
  neededVerbosity 
)
Value:
if (Application::verboseLevel() >= neededVerbosity) { \
Application::logStream() << text << std::endl; }

Definition at line 110 of file Application.hh.

111 { \
112 Application::logStream() << text << std::endl; }

Variable Documentation

◆ DEFAULT_MAX_OUTPUT_LINES

const int DEFAULT_MAX_OUTPUT_LINES = 200

default value of maximum amount of output lines saved from popen() output in runShellCommandAndGetOutput()

Definition at line 124 of file Application.hh.

◆ MAX_OUTPUT_LINE_LENGTH

const int MAX_OUTPUT_LINE_LENGTH = 512

maximum length of an output line saved from popen() output in runShellCommandAndGetOutput()

Definition at line 128 of file Application.hh.

Referenced by Application::runShellCommandAndGetOutput(), and Application::runShellCommandSilently().