OSDN Git Service

Initial commit
authormomen <momen@users.sourceforge.jp>
Mon, 17 Jan 2011 10:44:01 +0000 (19:44 +0900)
committermomen <momen@users.sourceforge.jp>
Mon, 17 Jan 2011 10:44:01 +0000 (19:44 +0900)
Log.cpp [new file with mode: 0644]
Log.h [new file with mode: 0644]
Makefile [new file with mode: 0644]
MujiKoma.cpp [new file with mode: 0644]
MujiKoma.h [new file with mode: 0644]
main.cpp [new file with mode: 0644]
mujikoma-common.h [new file with mode: 0644]

diff --git a/Log.cpp b/Log.cpp
new file mode 100644 (file)
index 0000000..67fe3db
--- /dev/null
+++ b/Log.cpp
@@ -0,0 +1,44 @@
+#include "mujikoma-common.h"
+#include "Log.h"
+
+#include <cstdarg>
+
+namespace MUJIKOMA_NAMESPACE {
+
+//
+// Public
+//
+
+Log& Log::instance()
+{
+       static Log log;
+       return log;
+}
+
+Log::~Log()
+{
+}
+
+void Log::log(level lv, const char* format, ...)
+{
+       va_list va;
+       char buf[4096];
+
+       va_start(va, format);
+       vsnprintf(buf, sizeof(buf)-1, format, va);
+       va_end(va);
+
+       printf("%s", buf);
+       fflush(stdout);
+}
+
+//
+// Private
+//
+
+Log::Log()
+{
+}
+
+}
+
diff --git a/Log.h b/Log.h
new file mode 100644 (file)
index 0000000..c3c438c
--- /dev/null
+++ b/Log.h
@@ -0,0 +1,53 @@
+#ifndef __MUJIKOMA_LOG_H__
+#define __MUJIKOMA_LOG_H__
+
+#include "mujikoma-common.h"
+
+namespace MUJIKOMA_NAMESPACE {
+
+using namespace std;
+
+class Log {
+public:
+       static Log& instance();
+       ~Log();
+
+       typedef enum {
+               LOG_ERR         = 3,
+               LOG_WARNING     = 4,
+               LOG_NOTICE      = 5,
+               LOG_INFO        = 6,
+               LOG_DEBUG       = 7,
+       } level;
+       void log(level lv, const char* format, ...);
+
+private:
+       Log();
+};
+
+#define ELOG(fmt, ...) \
+       Log::instance().log(Log::LOG_ERR, fmt "\n", ##__VA_ARGS__)
+#define WLOG(fmt, ...) \
+       Log::instance().log(Log::LOG_WARNING, fmt "\n", ##__VA_ARGS__)
+#define NLOG(fmt, ...) \
+       Log::instance().log(Log::LOG_NOTICE, fmt "\n", ##__VA_ARGS__)
+#define ILOG(fmt, ...) \
+       Log::instance().log(Log::LOG_INFO, fmt "\n", ##__VA_ARGS__)
+#define DLOG(fmt, ...) \
+       Log::instance().log(Log::LOG_DEBUG, fmt "\n", ##__VA_ARGS__)
+
+#define ELOG_NB(fmt, ...) \
+       Log::instance().log(Log::LOG_ERR, fmt, ##__VA_ARGS__)
+#define WLOG_NB(fmt, ...) \
+       Log::instance().log(Log::LOG_WARNING, fmt, ##__VA_ARGS__)
+#define NLOG_NB(fmt, ...) \
+       Log::instance().log(Log::LOG_NOTICE, fmt, ##__VA_ARGS__)
+#define ILOG_NB(fmt, ...) \
+       Log::instance().log(Log::LOG_INFO, fmt, ##__VA_ARGS__)
+#define DLOG_NB(fmt, ...) \
+       Log::instance().log(Log::LOG_DEBUG, fmt, ##__VA_ARGS__)
+
+}
+
+#endif // __MUJIKOMA_LOG_H__
+
diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..5ebad6f
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+PROG = mujikoma
+CXX = g++
+CXXFLAGS = -Wall
+OBJS = main.o \
+       MujiKoma.o \
+       Log.o
+
+$(PROG) : $(OBJS)
+       $(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS)
+
+clean :
+       rm -f $(OBJS) $(PROG)
+
diff --git a/MujiKoma.cpp b/MujiKoma.cpp
new file mode 100644 (file)
index 0000000..cd20deb
--- /dev/null
@@ -0,0 +1,23 @@
+#include "MujiKoma.h"
+
+namespace MUJIKOMA_NAMESPACE {
+
+void MujiKoma::start()
+{
+       DLOG("MujiKoma::start() %d", alive);
+
+       alive = 1;
+       while (alive) {
+               DLOG_NB(".");
+               sleep(1);
+       }
+}
+
+void MujiKoma::stop()
+{
+       DLOG("MujiKoma::stop()\n");
+       alive = 0;
+}
+
+}
+
diff --git a/MujiKoma.h b/MujiKoma.h
new file mode 100644 (file)
index 0000000..7d8671c
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef __MUJIKOMA_H__
+#define __MUJIKOMA_H__
+
+#include "mujikoma-common.h"
+#include "Log.h"
+
+namespace MUJIKOMA_NAMESPACE {
+
+class Log;
+
+class MujiKoma {
+public:
+       void start();
+       void stop();
+
+private:
+       int alive;
+};
+
+}
+
+#endif // __MUJIKOMA_H__
+
diff --git a/main.cpp b/main.cpp
new file mode 100644 (file)
index 0000000..34386c2
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,62 @@
+#include "mujikoma-common.h"
+#include "MujiKoma.h"
+
+#include <signal.h>
+
+using namespace MUJIKOMA_NAMESPACE;
+
+static MujiKoma *koma = NULL;
+
+void sig_trap(int signo)
+{
+       printf("### signo %d\n", signo);
+       if (koma) {
+               koma->stop();
+       }
+}
+
+void init_sig()
+{
+       struct sigaction sa;
+
+       memset(&sa, 0x0, sizeof(struct sigaction));
+       sa.sa_handler = sig_trap;
+       sa.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP;
+
+       if (sigaction(SIGINT, &sa, NULL)) {
+               perror("sigaction");
+       }
+       if (sigaction(SIGTERM, &sa, NULL)) {
+               perror("sigaction");
+       }
+       if (sigaction(SIGHUP, &sa, NULL)) {
+               perror("sigaction");
+       }
+       return;
+}
+
+int main(int argc, char** argv)
+{
+       int ret;
+
+       printf("MujiKoma start\n");
+
+       init_sig();
+
+       if (argc == 1) {
+               printf("Daemonized...\n");
+               ret = daemon(1, 1);
+               if (ret) {
+                       perror("daemon");
+                       return ret;
+               }
+       }
+
+       printf("pid %d\n", getpid());
+
+       koma = new MujiKoma();
+       koma->start();
+
+       return 0;
+}
+
diff --git a/mujikoma-common.h b/mujikoma-common.h
new file mode 100644 (file)
index 0000000..6477a80
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef __MUJIKOMA_COMMON_H__
+#define __MUJIKOMA_COMMON_H__
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#define MUJIKOMA_NAMESPACE momen_mujikoma
+
+#endif // __MUJIKOMA_COMMON_H__
+