diff --git a/src/client.cpp b/src/client.cpp index 21d4c55..8473b29 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -4,14 +4,14 @@ #include #include #include +#include #include #include namespace client { void sendMsg(int msqid, data::RequestMapMsg *msg) { if (msgsnd(msqid, msg, sizeof(data::RequestMapMsg) - sizeof(long), 0) < 0) { - BOOST_LOG_TRIVIAL(fatal) << "send error " << strerror(errno); - exit(1); + throw std::runtime_error(std::string("send error ") + strerror(errno)); } } @@ -19,8 +19,7 @@ std::string recvMsg(int msqid) { data::ResponseMapMsg msg; if (msgrcv(msqid, &msg, sizeof(data::ResponseMapMsg) - sizeof(long), data::clientbound_msg, 0) < 0) { - BOOST_LOG_TRIVIAL(fatal) << "receive error " << strerror(errno); - exit(1); + throw std::runtime_error(std::string("receive error ") + strerror(errno)); } return std::string(&msg.data[0], msg.datalen); @@ -33,8 +32,7 @@ void map(key_t key) { auto msqid = msgget(key, 0664); if (msqid < 0) { - BOOST_LOG_TRIVIAL(fatal) << "msgget fail"; - exit(0); + throw std::runtime_error("msgget fail. is the server running?"); } data::RequestMapMsg req; diff --git a/src/client.hpp b/src/client.hpp index 98caf3d..3020c4c 100644 --- a/src/client.hpp +++ b/src/client.hpp @@ -1,3 +1,4 @@ +#include #include namespace client { diff --git a/src/data.cpp b/src/data.cpp index fa366b3..847cafa 100644 --- a/src/data.cpp +++ b/src/data.cpp @@ -1,6 +1,7 @@ #include "data.hpp" #include #include +#include #include namespace data { @@ -8,8 +9,7 @@ key_t getIpcKeyFromExeName(char *argv0) { auto k = ftok(argv0, 'X'); if (k < 0) { - BOOST_LOG_TRIVIAL(fatal) << "failed to get IPC key"; - exit(1); + throw std::runtime_error("failed to get IPC key"); } return k; diff --git a/src/data.hpp b/src/data.hpp index 602f407..84fd47e 100644 --- a/src/data.hpp +++ b/src/data.hpp @@ -1,3 +1,4 @@ +#include #include namespace data { diff --git a/src/main.cpp b/src/main.cpp index 540782d..512dc71 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,11 +3,11 @@ #include "server.hpp" #include #include +#include -int main(int argc, char *argv[]) { +void main2(int argc, char *argv[]) { if (argc < 2) { - BOOST_LOG_TRIVIAL(fatal) << "not enough arguments!"; - return 1; + throw std::runtime_error("not enough arguments!"); } if (strcmp(argv[1], "serve") == 0) { @@ -17,9 +17,16 @@ int main(int argc, char *argv[]) { } else if (strcmp(argv[1], "resourceloc") == 0) { client::resolveResourceLoc(data::getIpcKeyFromExeName(argv[0])); } else { - BOOST_LOG_TRIVIAL(fatal) << "unknown command!"; + throw std::runtime_error("unknown command!"); + } +} + +int main(int argc, char *argv[]) { + try { + main2(argc, argv); + return 0; + } catch (const std::exception &e) { + BOOST_LOG_TRIVIAL(fatal) << "Exception: " << e.what(); return 1; } - - return 0; } diff --git a/src/server.cpp b/src/server.cpp index fbbd259..bcf7d74 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -28,8 +28,7 @@ void run(key_t key) { auto msqid = msgget(key, 0664 | IPC_CREAT); if (msqid < 0) { - BOOST_LOG_TRIVIAL(fatal) << "msgget fail"; - exit(1); + throw std::runtime_error("msgget fail"); } BOOST_LOG_TRIVIAL(info) << "msqid " << msqid; diff --git a/src/server.hpp b/src/server.hpp index 57f3e4d..9f976fb 100644 --- a/src/server.hpp +++ b/src/server.hpp @@ -1,3 +1,4 @@ +#include #include namespace server {