configobject.h \
delegate.h \
event.h \
+ exception.cpp \
+ exception.h \
fifo.cpp \
fifo.h \
i2-base.h \
#endif /* _WIN32 */
if (hModule == NULL)
- throw exception(/*"Could not load module"*/);
+ throw ComponentLoadException("Could not load module");
#ifdef _WIN32
pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
#endif /* _WIN32 */
if (pCreateComponent == NULL)
- throw exception(/*"Module does not contain CreateComponent function"*/);
+ throw ComponentLoadException("Loadable module does not contain CreateComponent function");
component = Component::Ptr(pCreateComponent());
component->SetApplication(static_pointer_cast<Application>(shared_from_this()));
PathEnv = getenv("PATH");
if (PathEnv != NULL) {
- PathEnv = strdup(PathEnv);
-
- if (PathEnv == NULL)
- throw exception(/*"strdup() failed"*/);
+ PathEnv = Memory::StrDup(PathEnv);
FoundPath = false;
class Component;
+DEFINE_EXCEPTION_CLASS(ComponentLoadException);
+
class Application : public Object {
private:
bool m_ShuttingDown;
Application::Instance->SetArguments(args);
- result = Application::Instance->Main(args);
+#ifndef _DEBUG
+ try {
+#endif /* !_DEBUG */
+ result = Application::Instance->Main(args);
+#ifndef _DEBUG
+ } catch (const Exception& ex) {
+ cout << "---" << endl;
+ cout << "Exception: " << typeid(ex).name() << endl;
+ cout << "Message: " << ex.GetMessage() << endl;
+
+ return EXIT_FAILURE;
+ }
+#endif /* !_DEBUG */
Application::Instance.reset();
<ClCompile Include="condvar.cpp" />
<ClCompile Include="confighive.cpp" />
<ClCompile Include="configobject.cpp" />
+ <ClCompile Include="exception.cpp" />
<ClCompile Include="fifo.cpp" />
<ClCompile Include="memory.cpp" />
<ClCompile Include="mutex.cpp" />
<ClInclude Include="configobject.h" />
<ClInclude Include="delegate.h" />
<ClInclude Include="event.h" />
+ <ClInclude Include="exception.h" />
<ClInclude Include="fifo.h" />
<ClInclude Include="i2-base.h" />
<ClInclude Include="memory.h" />
--- /dev/null
+#include "i2-base.h"
+
+using namespace icinga;
+
+Exception::Exception(void)
+{
+}
+
+Exception::Exception(const string& message)
+{
+ m_Message = message;
+}
+
+Exception::~Exception(void)
+{
+}
+
+string Exception::GetMessage(void) const
+{
+ return m_Message;
+}
--- /dev/null
+#ifndef EXCEPTION_H
+#define EXCEPTION_H
+
+namespace icinga
+{
+
+class Exception
+{
+private:
+ string m_Message;
+
+public:
+ typedef shared_ptr<Exception> Ptr;
+ typedef weak_ptr<Exception> WeakPtr;
+
+ Exception(void);
+ Exception(const string& message);
+
+ virtual ~Exception(void);
+
+ string GetMessage(void) const;
+};
+
+}
+
+#define DEFINE_EXCEPTION_CLASS(klass) \
+ class klass : public Exception \
+ { \
+ public: \
+ typedef shared_ptr<klass> Ptr; \
+ typedef weak_ptr<klass> WeakPtr; \
+ \
+ inline klass(void) : Exception() \
+ { \
+ } \
+ \
+ inline klass(const string& message) : Exception(message) \
+ { \
+ } \
+ };
+
+#endif /* EXCEPTION_H */
#include "condvar.h"
#include "thread.h"
#include "object.h"
+#include "exception.h"
#include "memory.h"
#include "delegate.h"
#include "event.h"
using namespace icinga;
+Memory::Memory(void)
+{
+}
+
void *Memory::Allocate(size_t size)
{
void *ptr = malloc(size);
return new_ptr;
}
+char *Memory::StrDup(const char *str)
+{
+ char *new_str = strdup(str);
+
+ if (str == NULL)
+ throw OutOfMemoryException();
+
+ return new_str;
+}
+
void Memory::Free(void *ptr)
{
if (ptr != NULL)
namespace icinga
{
-class OutOfMemoryException : public exception { };
+DEFINE_EXCEPTION_CLASS(OutOfMemoryException);
class Memory
{
private:
- Memory(void) { }
+ Memory(void);
public:
static void *Allocate(size_t size);
static void *Reallocate(void *ptr, size_t size);
+ static char *StrDup(const char *str);
static void Free(void *ptr);
};
return shared_ptr<T>(instance);
}
+template<class T, class TArg1>
+shared_ptr<T> new_object(const TArg1& arg1)
+{
+ T *instance = new T(arg1);
+
+ return shared_ptr<T>(instance);
+}
+
+template<class T, class TArg1, class TArg2>
+shared_ptr<T> new_object(const TArg1& arg1, const TArg2& arg2)
+{
+ T *instance = new T(arg1, arg2);
+
+ return shared_ptr<T>(instance);
+}
+
typedef function<Object::Ptr ()> factory_function;
template<class T>
string filename;
if (!GetConfig()->GetProperty("configFilename", &filename))
- throw exception(/*"Missing configFilename property"*/);
+ throw ConfigParserException("Missing configFilename property");
fp.open(filename.c_str(), ifstream::in);
if (fp.fail())
- throw exception(/*"Could not open config file"*/);
+ throw ConfigParserException("Could not open config file");
+ GetApplication()->Log("Reading config file: %s", filename.c_str());
+
while (!fp.eof()) {
size_t bufferSize = 1024;
char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize);
fp.read(buffer, bufferSize);
if (fp.bad())
- throw exception(/*"Could not read from config file"*/);
+ throw ConfigParserException("Could not read from config file");
fifo->Write(NULL, fp.gcount());
}
fifo->Read(NULL, fifo->GetSize());
if (jsonobj == NULL)
- throw exception(/*"Could not parse config file."*/);
+ throw ConfigParserException("Could not parse config file.");
for (cJSON *typeobj = jsonobj->child; typeobj != NULL; typeobj = typeobj->next) {
string type = typeobj->string;
namespace icinga
{
+DEFINE_EXCEPTION_CLASS(ConfigParserException);
+
class ConfigFileComponent : public Component
{
public: