]> git.sesse.net Git - cubemap/commitdiff
Replace all perror() calls with our own log calls.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 15 Apr 2013 22:32:52 +0000 (00:32 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Mon, 15 Apr 2013 22:32:52 +0000 (00:32 +0200)
13 files changed:
acceptor.cpp
client.cpp
config.cpp
httpinput.cpp
log.cpp
log.h
main.cpp
server.cpp
stats.cpp
stream.cpp
thread.cpp
udpinput.cpp
util.cpp

index ecebf7e5b65d74359e6ce6cf857c55fa4920c19b..70cd30c23bdfdcd53c16efe025c2ce4e58b01bdd 100644 (file)
@@ -10,6 +10,7 @@
 #include <unistd.h>
 
 #include "acceptor.h"
+#include "log.h"
 #include "serverpool.h"
 #include "state.pb.h"
 
@@ -28,26 +29,26 @@ int create_server_socket(int port, SocketType socket_type)
                server_sock = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP);
        }
        if (server_sock == -1) {
-               perror("socket");
+               log_perror("socket");
                exit(1);
        }
 
        int one = 1;
        if (setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) == -1) {
-               perror("setsockopt(SO_REUSEADDR)");
+               log_perror("setsockopt(SO_REUSEADDR)");
                exit(1);
        }
 
        // We want dual-stack sockets. (Sorry, OpenBSD and Windows XP...)
        int zero = 0;
        if (setsockopt(server_sock, IPPROTO_IPV6, IPV6_V6ONLY, &zero, sizeof(zero)) == -1) {
-               perror("setsockopt(IPV6_V6ONLY)");
+               log_perror("setsockopt(IPV6_V6ONLY)");
                exit(1);
        }
 
        // Set as non-blocking, so the acceptor thread can notice that we want to shut it down.
        if (ioctl(server_sock, FIONBIO, &one) == -1) {
-               perror("ioctl(FIONBIO)");
+               log_perror("ioctl(FIONBIO)");
                exit(1);
        }
 
@@ -57,13 +58,13 @@ int create_server_socket(int port, SocketType socket_type)
        addr.sin6_port = htons(port);
 
        if (bind(server_sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)) == -1) {
-               perror("bind");
+               log_perror("bind");
                exit(1);
        }
 
        if (socket_type == TCP_SOCKET) {
                if (listen(server_sock, 128) == -1) {
-                       perror("listen");
+                       log_perror("listen");
                        exit(1);
                }
        }
@@ -99,7 +100,7 @@ void Acceptor::close_socket()
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-               perror("close");
+               log_perror("close");
        }
 }
 
@@ -117,7 +118,7 @@ void Acceptor::do_work()
                        continue;
                }
                if (nfds == -1) {
-                       perror("poll");
+                       log_perror("poll");
                        usleep(100000);
                        continue;
                }
@@ -131,7 +132,7 @@ void Acceptor::do_work()
                        continue;
                }
                if (sock == -1) {
-                       perror("accept");
+                       log_perror("accept");
                        usleep(100000);
                        continue;
                }
@@ -139,7 +140,7 @@ void Acceptor::do_work()
                // Set the socket as nonblocking.
                int one = 1;
                if (ioctl(sock, FIONBIO, &one) == -1) {
-                       perror("FIONBIO");
+                       log_perror("FIONBIO");
                        exit(1);
                }
 
index 3a2552172d6a924ad41d68db874edbf4f1db17b7..bb2d26cdcd4abec4516d23ae92142636a33ac341 100644 (file)
@@ -4,6 +4,7 @@
 #include <sys/socket.h>
 
 #include "client.h"
+#include "log.h"
 #include "markpool.h"
 #include "state.pb.h"
 #include "stream.h"
@@ -29,12 +30,12 @@ Client::Client(int sock)
        socklen_t addr_len = sizeof(addr);
 
        if (getpeername(sock, reinterpret_cast<sockaddr *>(&addr), &addr_len) == -1) {
-               perror("getpeername");
+               log_perror("getpeername");
                remote_addr = "";
        } else {
                char buf[INET6_ADDRSTRLEN];
                if (inet_ntop(addr.sin6_family, &addr.sin6_addr, buf, sizeof(buf)) == NULL) {
-                       perror("inet_ntop");
+                       log_perror("inet_ntop");
                        remote_addr = "";
                } else {
                        remote_addr = buf;
@@ -64,7 +65,7 @@ Client::Client(const ClientProto &serialized, Stream *stream)
        }
        if (setsockopt(sock, SOL_SOCKET, SO_MARK, &fwmark, sizeof(fwmark)) == -1) {
                if (fwmark != 0) {
-                       perror("setsockopt(SO_MARK)");
+                       log_perror("setsockopt(SO_MARK)");
                }
        }
 }
index ec9a0e14dc65eb8debf599ae09ab6fa115b52c05..660090d4a237b3c1fb22cf9793988838ed47ae72 100644 (file)
@@ -26,7 +26,7 @@ bool read_config(const string &filename, vector<ConfigLine> *lines)
 {
        FILE *fp = fopen(filename.c_str(), "r");
        if (fp == NULL) {
-               perror(filename.c_str());
+               log_perror(filename.c_str());
                return false;
        }
 
index 0c5c62087eb50ebf60fd7192fa069ead9752be69..962be2a47d2c91a5de834727d90915e182378078 100644 (file)
@@ -62,7 +62,7 @@ void HTTPInput::close_socket()
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-               perror("close()");
+               log_perror("close()");
        }
 }
 
@@ -115,7 +115,7 @@ int HTTPInput::lookup_and_connect(const string &host, const string &port)
                } while (err == -1 && errno == EINTR);
 
                if (err == -1) {
-                       perror("close");
+                       log_perror("close");
                        // Can still continue.
                }
 
@@ -223,7 +223,7 @@ void HTTPInput::do_work()
                                continue;
                        }
                        if (nfds == -1) {
-                               perror("poll");
+                               log_perror("poll");
                                state = CLOSING_SOCKET;
                        }
                }
@@ -249,7 +249,7 @@ void HTTPInput::do_work()
                                // Yay, successful connect. Try to set it as nonblocking.
                                int one = 1;
                                if (ioctl(sock, FIONBIO, &one) == -1) {
-                                       perror("ioctl(FIONBIO)");
+                                       log_perror("ioctl(FIONBIO)");
                                        state = CLOSING_SOCKET;
                                } else {
                                        state = SENDING_REQUEST;
@@ -267,7 +267,7 @@ void HTTPInput::do_work()
                        } while (ret == -1 && errno == EINTR);
 
                        if (ret == -1) {
-                               perror("write");
+                               log_perror("write");
                                state = CLOSING_SOCKET;
                                continue;
                        }
@@ -289,7 +289,7 @@ void HTTPInput::do_work()
                        } while (ret == -1 && errno == EINTR);
 
                        if (ret == -1) {
-                               perror("read");
+                               log_perror("read");
                                state = CLOSING_SOCKET;
                                continue;
                        }
@@ -343,7 +343,7 @@ void HTTPInput::do_work()
                        } while (ret == -1 && errno == EINTR);
 
                        if (ret == -1) {
-                               perror("read");
+                               log_perror("read");
                                state = CLOSING_SOCKET;
                                continue;
                        }
@@ -365,7 +365,7 @@ void HTTPInput::do_work()
                        } while (err == -1 && errno == EINTR);
 
                        if (err == -1) {
-                               perror("close");
+                               log_perror("close");
                        }
 
                        state = NOT_CONNECTED;
diff --git a/log.cpp b/log.cpp
index ba05a64d1fe5236c4dfe370a947254587c56cf37..a4f923644a5b9597febecb038fa8c54c8526a207 100644 (file)
--- a/log.cpp
+++ b/log.cpp
@@ -1,9 +1,11 @@
 #include "log.h"
 
 #include <stdio.h>
+#include <string.h>
 #include <stdarg.h>
 #include <syslog.h>
 #include <assert.h>
+#include <errno.h>
 #include <string>
 #include <vector>
 
@@ -110,3 +112,10 @@ void log(LogLevel log_level, const char *fmt, ...)
                }
        }
 }
+
+void log_perror(const char *msg)
+{
+       char errbuf[4096];
+       strerror_r(errno, errbuf, sizeof(errbuf));
+       log(ERROR, "%s: %s", msg, errbuf);
+}
diff --git a/log.h b/log.h
index 7e51fcbe134fc8cd684d93702fe1a4e256529c30..ce731ab9b2ab0be8d676697b991ff1582bd0fd62 100644 (file)
--- a/log.h
+++ b/log.h
@@ -20,5 +20,6 @@ void start_logging();
 void shut_down_logging();
 
 void log(LogLevel log_level, const char *fmt, ...);
+void log_perror(const char *msg);
 
 #endif  // !defined(_LOG_H)
index 8825631771ffe80fe4ca3a7baa5261e2748f1083..4a5904b0715f1905817210138e3c9b7091329fae 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -197,14 +197,14 @@ bool dry_run_config(const std::string &argv0, const std::string &config_filename
        pid_t pid = fork();
        switch (pid) {
        case -1:
-               perror("fork()");
+               log_perror("fork()");
                free(argv0_copy);
                free(config_filename_copy);
                return false;
        case 0:
                // Child.
                execlp(argv0_copy, argv0_copy, "--test-config", config_filename_copy, NULL);
-               perror(argv0_copy);
+               log_perror(argv0_copy);
                _exit(1);
        default:
                // Parent.
@@ -221,7 +221,7 @@ bool dry_run_config(const std::string &argv0, const std::string &config_filename
        } while (err == -1 && errno == EINTR);
 
        if (err == -1) {
-               perror("waitpid()");
+               log_perror("waitpid()");
                return false;
        }       
 
@@ -402,7 +402,7 @@ start:
        for ( ;; ) {
                execlp(argv[0], argv[0], config_filename.c_str(), "--state", buf, NULL);
                open_logs(config.log_destinations);
-               perror("execlp");
+               log_perror("execlp");
                log(ERROR, "re-exec of %s failed. Waiting 0.2 seconds and trying again...", argv[0]);
                shut_down_logging();
                usleep(200000);
index 02efdbbdb6c165612f2ac45219072bbfbd414982..46bda5b21935ff36d0aca3be838885c5e4b7b7ba 100644 (file)
@@ -31,7 +31,7 @@ Server::Server()
 
        epoll_fd = epoll_create(1024);  // Size argument is ignored.
        if (epoll_fd == -1) {
-               perror("epoll_fd");
+               log_perror("epoll_fd");
                exit(1);
        }
 }
@@ -44,7 +44,7 @@ Server::~Server()
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-               perror("close(epoll_fd)");
+               log_perror("close(epoll_fd)");
        }
 }
 
@@ -72,7 +72,7 @@ void Server::do_work()
                        continue;
                }
                if (nfds == -1) {
-                       perror("epoll_wait");
+                       log_perror("epoll_wait");
                        exit(1);
                }
 
@@ -141,7 +141,7 @@ void Server::add_client(int sock)
        ev.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
        ev.data.u64 = reinterpret_cast<uint64_t>(&clients[sock]);
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, sock, &ev) == -1) {
-               perror("epoll_ctl(EPOLL_CTL_ADD)");
+               log_perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
        }
 
@@ -173,7 +173,7 @@ void Server::add_client_from_serialized(const ClientProto &client)
        ev.data.u64 = 0;  // Keep Valgrind happy.
        ev.data.u64 = reinterpret_cast<uint64_t>(client_ptr);
        if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client.sock(), &ev) == -1) {
-               perror("epoll_ctl(EPOLL_CTL_ADD)");
+               log_perror("epoll_ctl(EPOLL_CTL_ADD)");
                exit(1);
        }
 
@@ -254,7 +254,7 @@ read_request_again:
                        return;
                }
                if (ret == -1) {
-                       perror("read");
+                       log_perror("read");
                        close_client(client);
                        return;
                }
@@ -316,7 +316,7 @@ sending_header_or_error_again:
 
                if (ret == -1) {
                        // Error! Postcondition #1.
-                       perror("write");
+                       log_perror("write");
                        close_client(client);
                        return;
                }
@@ -387,7 +387,7 @@ sending_data_again:
                }
                if (ret == -1) {
                        // Error, close; postcondition #1.
-                       perror("sendfile");
+                       log_perror("sendfile");
                        close_client(client);
                        return;
                }
@@ -435,7 +435,7 @@ int Server::parse_request(Client *client)
        }
        if (setsockopt(client->sock, SOL_SOCKET, SO_MARK, &client->fwmark, sizeof(client->fwmark)) == -1) {                          
                if (client->fwmark != 0) {
-                       perror("setsockopt(SO_MARK)");
+                       log_perror("setsockopt(SO_MARK)");
                }
        }
        client->request.clear();
@@ -455,7 +455,7 @@ void Server::construct_header(Client *client)
        ev.data.u64 = reinterpret_cast<uint64_t>(client);
 
        if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
-               perror("epoll_ctl(EPOLL_CTL_MOD)");
+               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
                exit(1);
        }
 }
@@ -475,7 +475,7 @@ void Server::construct_error(Client *client, int error_code)
        ev.data.u64 = reinterpret_cast<uint64_t>(client);
 
        if (epoll_ctl(epoll_fd, EPOLL_CTL_MOD, client->sock, &ev) == -1) {
-               perror("epoll_ctl(EPOLL_CTL_MOD)");
+               log_perror("epoll_ctl(EPOLL_CTL_MOD)");
                exit(1);
        }
 }
@@ -490,7 +490,7 @@ void delete_from(vector<T> *v, T elem)
 void Server::close_client(Client *client)
 {
        if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->sock, NULL) == -1) {
-               perror("epoll_ctl(EPOLL_CTL_DEL)");
+               log_perror("epoll_ctl(EPOLL_CTL_DEL)");
                exit(1);
        }
 
@@ -511,7 +511,7 @@ void Server::close_client(Client *client)
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-               perror("close");
+               log_perror("close");
        }
 
        clients.erase(client->sock);
index f5916edf2fb696530fe081cad9eee503457c8a70..71fb4852cef695693c1a91e70ef4a61c2f764cb1 100644 (file)
--- a/stats.cpp
+++ b/stats.cpp
@@ -10,6 +10,7 @@
 #include <vector>
 
 #include "client.h"
+#include "log.h"
 #include "serverpool.h"
 #include "stats.h"
 
@@ -35,14 +36,14 @@ void StatsThread::do_work()
                char *filename = strdup((stats_file + ".new.XXXXXX").c_str());
                fd = mkostemp(filename, O_WRONLY);
                if (fd == -1) {
-                       perror(filename);
+                       log_perror(filename);
                        free(filename);
                        goto sleep;
                }
 
                fp = fdopen(fd, "w");
                if (fp == NULL) {
-                       perror("fdopen");
+                       log_perror("fdopen");
                        close(fd);
                        unlink(filename);
                        free(filename);
@@ -61,14 +62,14 @@ void StatsThread::do_work()
                                (long long unsigned)(client_stats[i].num_loss_events));
                }
                if (fclose(fp) == EOF) {
-                       perror("fclose");
+                       log_perror("fclose");
                        unlink(filename);
                        free(filename);
                        goto sleep;
                }
                
                if (rename(filename, stats_file.c_str()) == -1) {
-                       perror("rename");
+                       log_perror("rename");
                        unlink(filename);
                }
 
@@ -89,7 +90,7 @@ sleep:
                        break;
                }
                if (nfds == -1) {
-                       perror("poll");
+                       log_perror("poll");
                        usleep(100000);
                        continue;
                }
index ab10e35390fd9d6a16abe261e2d28f1f7f9b4e9f..2b9a69593a478605800fd21c9585063eebec8d0c 100644 (file)
@@ -6,6 +6,7 @@
 #include <vector>
 
 #include "state.pb.h"
+#include "log.h"
 #include "stream.h"
 #include "util.h"
 
@@ -31,7 +32,7 @@ Stream::~Stream()
                        ret = close(data_fd);
                } while (ret == -1 && errno == EINTR);
                if (ret == -1) {
-                       perror("close");
+                       log_perror("close");
                }
        }
 }
@@ -116,7 +117,7 @@ void Stream::add_data(const char *data, ssize_t bytes)
                                continue;
                        }
                        if (ret == -1) {
-                               perror("pwrite");
+                               log_perror("pwrite");
                                // Dazed and confused, but trying to continue...
                                break;
                        }
@@ -134,7 +135,7 @@ void Stream::add_data(const char *data, ssize_t bytes)
                        continue;
                }
                if (ret == -1) {
-                       perror("pwrite");
+                       log_perror("pwrite");
                        // Dazed and confused, but trying to continue...
                        break;
                }
index b263be649147e5f44952e1a3161bb6a5fb989e68..f719eacb95c5379a2d664f23c49c04e0b50b767b 100644 (file)
@@ -5,6 +5,7 @@
 #include <signal.h>
 #include <errno.h>
 
+#include "log.h"
 #include "thread.h"
        
 Thread::~Thread() {}
@@ -14,7 +15,7 @@ void Thread::run()
        should_stop = false;
        int pipefd[2];
        if (pipe2(pipefd, O_CLOEXEC) == -1) {
-               perror("pipe");
+               log_perror("pipe");
                exit(1);
        }
        stop_fd_read = pipefd[0];
@@ -32,7 +33,7 @@ void Thread::stop()
        } while (err == -1 && errno == EINTR);
 
        if (err == -1) {
-               perror("write");
+               log_perror("write");
                exit(1);
        }
 
@@ -41,13 +42,13 @@ void Thread::stop()
        } while (err == -1 && errno == EINTR);
 
        if (err == -1) {
-               perror("close");
+               log_perror("close");
                // Can continue (we have close-on-exec).
        }
 
        pthread_kill(worker_thread, SIGHUP);
        if (pthread_join(worker_thread, NULL) == -1) {
-               perror("pthread_join");
+               log_perror("pthread_join");
                exit(1);
        }
        
@@ -56,7 +57,7 @@ void Thread::stop()
        } while (err == -1 && errno == EINTR);
 
        if (err == -1) {
-               perror("close");
+               log_perror("close");
                // Can continue (we have close-on-exec).
        }
 }
index 081f631c47e114ba42f7ff46699667d8d1d31c5e..3a8beffeabcc8fcfd4aff48e2694f5aaa79603dd 100644 (file)
@@ -61,7 +61,7 @@ void UDPInput::close_socket()
        } while (ret == -1 && errno == EINTR);
 
        if (ret == -1) {
-               perror("close()");
+               log_perror("close()");
        }
 
        sock = -1;
@@ -102,7 +102,7 @@ void UDPInput::do_work()
                        continue;
                }
                if (nfds == -1) {
-                       perror("poll");
+                       log_perror("poll");
                        close_socket();
                        continue;       
                }
@@ -114,7 +114,7 @@ void UDPInput::do_work()
                } while (ret == -1 && errno == EINTR);
 
                if (ret <= 0) {
-                       perror("recv");
+                       log_perror("recv");
                        close_socket();
                        continue;
                }
index 41c13c6f2a3ed743a5345b3d0d41c37aefb6e5bc..8159e3821ccb1071efa8801ae8a63b90b65d2db4 100644 (file)
--- a/util.cpp
+++ b/util.cpp
@@ -14,12 +14,12 @@ int make_tempfile(const std::string &contents)
        char filename[] = "/tmp/cubemap.XXXXXX";
        int fd = mkstemp(filename);
        if (fd == -1) {
-               perror("mkstemp");
+               log_perror("mkstemp");
                return -1;
        }
 
        if (unlink(filename) == -1) {
-               perror("unlink");
+               log_perror("unlink");
                // Can still continue;
        }
 
@@ -28,7 +28,7 @@ int make_tempfile(const std::string &contents)
        while (to_write > 0) {
                ssize_t ret = write(fd, ptr, to_write);
                if (ret == -1) {
-                       perror("write");
+                       log_perror("write");
                        close(fd);
                        return -1;
                }
@@ -47,7 +47,7 @@ bool read_tempfile(int fd, std::string *contents)
 
        off_t len = lseek(fd, 0, SEEK_END);
        if (len == -1) {
-               perror("lseek");
+               log_perror("lseek");
                ok = false;
                goto done;
        }
@@ -55,7 +55,7 @@ bool read_tempfile(int fd, std::string *contents)
        contents->resize(len);
 
        if (lseek(fd, 0, SEEK_SET) == -1) {
-               perror("lseek");
+               log_perror("lseek");
                ok = false;
                goto done;
        }
@@ -64,7 +64,7 @@ bool read_tempfile(int fd, std::string *contents)
        while (has_read < len) {
                ret = read(fd, &((*contents)[has_read]), len - has_read);
                if (ret == -1) {
-                       perror("read");
+                       log_perror("read");
                        ok = false;
                        goto done;
                }
@@ -82,7 +82,7 @@ done:
        } while (ret == -1 && errno == EINTR);
        
        if (ret == -1) {
-               perror("close");
+               log_perror("close");
                // Can still continue.
        }