powerguru-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Powerguru-commit] [SCM] powerguru branch, master, updated. ec0bbec83c1e


From: Rob Savoye
Subject: [Powerguru-commit] [SCM] powerguru branch, master, updated. ec0bbec83c1e6ffa99d347521490eb09d48e13e0
Date: Fri, 28 Dec 2018 23:30:10 -0500 (EST)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "powerguru".

The branch, master has been updated
       via  ec0bbec83c1e6ffa99d347521490eb09d48e13e0 (commit)
      from  68ff1919bf090c4ccc48a79211c858dcc91d83a9 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.savannah.gnu.org/cgit/powerguru.git/commit/?id=ec0bbec83c1e6ffa99d347521490eb09d48e13e0


commit ec0bbec83c1e6ffa99d347521490eb09d48e13e0
Author: Rob Savoye <address@hidden>
Date:   Fri Dec 28 21:30:03 2018 -0700

    Better usage of boost::log for warnings and errors

diff --git a/client/threads.cc b/client/threads.cc
index aa1b38d..5485e3c 100644
--- a/client/threads.cc
+++ b/client/threads.cc
@@ -100,17 +100,13 @@ daemon_handler(Tcpip &net)
     retcode_t ret;
     int retries = 10;
 
-    //net.createNetClient(DEFAULTPORT);
-
-    //net.writeNet("Hello World!\r\n");
-
     while (retries-- > 0) {
         bool loop = true;
         std::vector<unsigned char> data;
         while (loop) {
             data.clear();
             if (net.readNet(data).size() < 0) {
-                BOOST_LOG(lg) << "ERROR: Got error from socket ";
+                BOOST_LOG_SEV(lg, severity_level::error) << "ERROR: Got error 
from socket ";
                 loop = false;
             } else {
                 if (data.data() == 0) {
@@ -120,7 +116,7 @@ daemon_handler(Tcpip &net)
                 std::string buffer = (char *)data.data();
                 // Check to see if the socket was closed, so the read failed.
                 if (buffer[0] == 255) {
-                    std::cerr << "Done!!!!" << std::endl;
+                    //std::cerr << "Done!!!!" << std::endl;
                     loop = false;
                     retries = 0;
                     break;
diff --git a/daemon/main.cc b/daemon/main.cc
index 5fa55a7..1b1fc44 100644
--- a/daemon/main.cc
+++ b/daemon/main.cc
@@ -192,7 +192,8 @@ main(int argc, char *argv[])
     BOOST_LOG(lg) << "PowerGuru - 1 Wire Mode" << std::endl;
     Tcpip net;
     if (net.createNetServer(DEFAULTPORT) == ERROR) {
-        std::cerr << "ERROR: Couldn't create a network server!" << std::endl;
+        BOOST_LOG_SEV(lg, severity_level::error)
+            << "ERROR: Couldn't create a network server!";
         exit(-1);
     }
 
@@ -230,7 +231,8 @@ main(int argc, char *argv[])
     }
 
     // synchronize threads:
-    BOOST_LOG(lg) << "Killing all threads..." << std::endl;
+    BOOST_LOG(lg) << "Killing all threads...";
+
     onewire_thread.join();      // pauses until first finishes
     client_thread.join();       // pauses until first finishes
 #ifdef BUILD_OWNET
diff --git a/devices/onewire.cc b/devices/onewire.cc
index 609d8b2..b0f0638 100644
--- a/devices/onewire.cc
+++ b/devices/onewire.cc
@@ -48,7 +48,7 @@ Onewire::Onewire(void)
                 }
             }
         } else {
-            std::cerr << "ERROR: " << _rootdir << " doesn't exist" << 
std::endl;
+            BOOST_LOG_SEV(lg, severity_level::error) << _rootdir << " doesn't 
exist!";
             _mounted = false;
         }
     } catch (const boost::filesystem::filesystem_error& ex) {
diff --git a/devices/ownet.cc b/devices/ownet.cc
index 2838e10..b726d7d 100644
--- a/devices/ownet.cc
+++ b/devices/ownet.cc
@@ -52,7 +52,7 @@ Ownet::Ownet(std::string &host)
     // but always does on the second.
     while (retries-- > 0) {
         if (OW_init(argv.c_str()) < 0) {
-            BOOST_LOG(lg) << "WARNING: Couldn't connect to owserver with " << 
argv;
+            BOOST_LOG_SEV(lg, severity_level::warning) << "Couldn't connect to 
owserver with " << argv;
             //return;
         } else {
             BOOST_LOG(lg) << "Connected to owserver on host " << host;
diff --git a/lib/log.cc b/lib/log.cc
index c87ddd7..2461cf6 100644
--- a/lib/log.cc
+++ b/lib/log.cc
@@ -31,14 +31,21 @@ src::logger lg;
 void
 log_init(const std::string &name)
 {
+    // Change the default timestamp to be less long than the commom_attributes
     logging::formatter formatter = expr::stream
         << expr::format_date_time< boost::posix_time::ptime >
         ("TimeStamp", "[%Y-%m-%d %H:%M:%S] ") << expr::message;
-    
-    logging::add_file_log(name + ".log")->set_formatter(formatter);
+
+    logging::add_file_log (
+        keywords::file_name = name + ".log",
+        keywords::rotation_size = 10 * 1024 * 1024,
+        keywords::time_based_rotation = sinks::file::rotation_at_time_point(0, 
0, 0),
+        keywords::format = "[%TimeStamp%]: %Message%",
+        keywords::auto_flush = true
+        )->set_formatter(formatter);
+
     logging::add_console_log()->set_formatter(formatter);
 
-    // Register common attributes
     logging::add_common_attributes();
 }
 
diff --git a/lib/log.h b/lib/log.h
index 9a63680..19d501c 100644
--- a/lib/log.h
+++ b/lib/log.h
@@ -63,6 +63,7 @@ namespace src = boost::log::sources;
 namespace sinks = boost::log::sinks;
 namespace expr = boost::log::expressions;
 namespace keywords = boost::log::keywords;
+namespace severity_level = boost::log::trivial;
 
 extern src::logger lg;
 
diff --git a/lib/tcpip.cc b/lib/tcpip.cc
index 32e145f..17b11bc 100644
--- a/lib/tcpip.cc
+++ b/lib/tcpip.cc
@@ -108,21 +108,23 @@ Tcpip::createNetServer(short port, const std::string 
&protocol)
         // Get a file descriptor for this socket connection
         _sockIOfd = ::socket(addr->ai_family, addr->ai_socktype, 
addr->ai_protocol);
         if (_sockIOfd < 0) {
-            std::cerr << "ERROR: unable to create socket: " << strerror(errno) 
<< std::endl;
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "unable to create socket: " << strerror(errno);
             return ERROR;
         }
     } else {
-        std::cerr << "ERROR: unable to get address! " << gai_strerror(errno) 
<< std::endl;
+        BOOST_LOG_SEV(lg, severity_level::error)
+            << "unable to get address! " << gai_strerror(errno);
         return ERROR;
     }
     
     if (bind(_sockIOfd, addr->ai_addr, addr->ai_addrlen) == -1) {
-        BOOST_LOG(lg) << "WARNING: unable to bind to"
+        BOOST_LOG_SEV(lg, severity_level::warning) << "unable to bind to"
                    << inet_ntoa(sock_in.sin_addr)
                    << " port!" << strerror(errno);
         if (errno == EADDRINUSE) {
-            std::cerr << "ERROR: There is another process already bound to 
this port!"
-                      << std::endl;
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "ERROR: There is another process already bound to this 
port!";
             return ERROR;
         }
     }
@@ -132,8 +134,9 @@ Tcpip::createNetServer(short port, const std::string 
&protocol)
                    << " using fd #" << _sockIOfd;
     
     if (listen(_sockIOfd, 5) < 0) {
-        BOOST_LOG(lg) << "ERROR: unable to listen on port: "
-                   << port << ": " <<  strerror(errno); 
+        BOOST_LOG_SEV(lg, severity_level::error)
+            << "ERROR: unable to listen on port: "
+            << port << ": " <<  strerror(errno); 
         return ERROR;
     }
     
@@ -200,14 +203,15 @@ Tcpip::newNetConnection(bool block)
         }
     
         if (ret == -1) {
-            BOOST_LOG(lg) << "ERROR: The accept() socket for fd " << _sockIOfd
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "ERROR: The accept() socket for fd " << _sockIOfd
                        << " never was available for writing!";
             return ERROR;
         }
     
         if (ret == 0) {
-            BOOST_LOG(lg) <<
-                "ERROR: The accept() socket for fd #%d timed out waiting to 
write!"
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "The accept() socket for fd #%d timed out waiting to write!"
                        << _sockIOfd;
         }
         if (ret >= 1) {
@@ -221,7 +225,8 @@ Tcpip::newNetConnection(bool block)
     _sockfd = ::accept(_sockIOfd, &fsin, &alen);
   
     if (_sockfd < 0) {
-        BOOST_LOG(lg) << "ERROR: unable to accept : " << strerror(errno);
+        BOOST_LOG_SEV(lg, severity_level::error)
+            << "unable to accept : " << strerror(errno);
         return ERROR;
     } else {
         BOOST_LOG(lg) << "Accepting tcp/ip connection on fd #" << _sockfd;
@@ -259,14 +264,16 @@ Tcpip::createNetClient(const std::string &hostname, short 
port,
     if (addr) {
         _sockfd = ::socket(addr->ai_family, addr->ai_socktype, 
_proto->p_proto);
         if (_sockfd < 0) {
-            BOOST_LOG(lg) << "WARNING: unable to create socket: "
+            BOOST_LOG_SEV(lg, severity_level::warning)
+                << "WARNING: unable to create socket: "
                        << ::strerror(errno);
             return ERROR;
         }
 
         BOOST_LOG(lg) << "Trying to connect to: " << hostname << ":" << port;
         if (::connect(_sockfd, addr->ai_addr, addr->ai_addrlen) < 0) {
-            BOOST_LOG(lg) << "ERROR: Couldn't connect to: " << hostname << " "
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "ERROR: Couldn't connect to: " << hostname << " "
                        << strerror(errno);
             retries = 1;
             while (retries-- > 0) {
@@ -293,23 +300,23 @@ Tcpip::createNetClient(const std::string &hostname, short 
port,
                     return ERROR;
                 }
                 if (ret == 0) {
-                    BOOST_LOG(lg) <<
+                    BOOST_LOG_SEV(lg, severity_level::warning) <<
                         "WARNING: The connect() socket for fd #%d timed out 
waiting to write!"
                                << _sockfd;
                 }
             }
             ret = ::connect(_sockfd, addr->ai_addr, addr->ai_addrlen);
             if (ret <= 0) {
-                BOOST_LOG(lg) << "unable to connect to "
-                           << hostname
+                BOOST_LOG_SEV(lg, severity_level::warning)
+                    << "unable to connect to " << hostname
                            << ", port " << port
                            << ": " << strerror(errno);
                 close(_sockfd);
                 return ERROR;
             }
             if (ret <= 0) {
-                BOOST_LOG(lg) << "unable to connect to "
-                           << hostname
+                BOOST_LOG_SEV(lg, severity_level::warning)
+                    << "unable to connect to " << hostname
                            << ", port " << port
                            << ": " << strerror(errno);
                 close(_sockfd);
@@ -368,7 +375,8 @@ Tcpip::closeNet(void)
 #if 0
             if (shutdown(_sockfd, SHUT_RDWR) < 0) {
                 if (errno != ENOTCONN) {
-                    BOOST_LOG(lg) << "WARNING: Unable to shutdown socket for 
fd #"
+                    BOOST_LOG_SEV(lg, severity_level::warning)
+                        << "WARNING: Unable to shutdown socket for fd #"
                                << _sockfd << strerror(errno);
                 } else {
                     BOOST_LOG(lg) << "The socket using fd #" << _sockfd
@@ -378,8 +386,8 @@ Tcpip::closeNet(void)
             }
 #endif 
             if (close(_sockfd) < 0) {
-                BOOST_LOG(lg) <<
-                    "WARNING: Unable to close the socket for fd "
+                BOOST_LOG_SEV(lg, severity_level::warning) <<
+                    "Unable to close the socket for fd "
                            <<  _sockfd << strerror(errno);
                 sleep(1);
                 retries++;
@@ -545,7 +553,8 @@ Tcpip::anydata(int fd, std::vector<const unsigned char *> 
&msgs)
                     break;
                 } else { 
                     //BOOST_LOG(lg) << "WARNING: Throwing out partial packet " 
<< std::endl;
-                    BOOST_LOG(lg) << "WARNING: Throwing out partial packet " 
<< packet;
+                    BOOST_LOG_SEV(lg, severity_level::warning)
+                        << "Throwing out partial packet " << packet;
                     break;
                 }
             }
@@ -588,8 +597,8 @@ Tcpip::readNet(std::vector<unsigned char> &buf)
         FD_ZERO(&fdset);
         FD_SET(_sockfd, &fdset);
     } else {
-        BOOST_LOG(lg) << "WARNING: Can't do anything with socket fd #"
-                   << _sockfd;
+        BOOST_LOG_SEV(lg, severity_level::warning)
+            << "WARNING: Can't do anything with socket fd #" << _sockfd;
         buf.clear();
         buf.push_back(255);
         return buf;
@@ -614,7 +623,8 @@ Tcpip::readNet(std::vector<unsigned char> &buf)
     if (ret == -1 && errno == EINTR) {
         BOOST_LOG(lg) <<
             "The socket for fd #" << _sockfd << " we interupted by a system 
call!";
-        BOOST_LOG(lg) << "WARNING: error is " << strerror(errno);
+        BOOST_LOG_SEV(lg, severity_level::warning)
+            << "error is " << strerror(errno);
     }
   
     if (ret == -1) {
@@ -669,8 +679,8 @@ Tcpip::writeNet(const std::vector<unsigned char> &buffer)
             FD_ZERO(&fdset);
             FD_SET(_sockfd, &fdset);
         } else {
-            BOOST_LOG(lg) << "WARNING: Can't do anything with socket fd #!"
-                       << _sockfd;
+            BOOST_LOG_SEV(lg, severity_level::warning)
+                << "Can't do anything with socket fd #!" << _sockfd;
             return -1;
         }
     
@@ -702,12 +712,14 @@ Tcpip::writeNet(const std::vector<unsigned char> &buffer)
     
     
         if (ret == 0) {
-            BOOST_LOG(lg) << "Couldn't write any bytes to fd #" << _sockfd;
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "Couldn't write any bytes to fd #" << _sockfd;
             return ret;
         }
     
         if (ret < 0) {
-            BOOST_LOG(lg) << "Couldn't write " << nbytes << " bytes to fd #"
+            BOOST_LOG_SEV(lg, severity_level::error)
+                << "Couldn't write " << nbytes << " bytes to fd #"
                        << _sockfd;
             return ret;
         }
diff --git a/lib/tcputil.cc b/lib/tcputil.cc
index 5062094..2d85a2a 100644
--- a/lib/tcputil.cc
+++ b/lib/tcputil.cc
@@ -101,8 +101,8 @@ Tcputil::getAddrInfo(const std::string& hostname, 
std::uint16_t port)
         ret = ::getaddrinfo(hostname.c_str(), portstr.c_str(), &hints, &ans);
     }
     if (ret != 0) {
-        std::cerr << "ERROR: getaddrinfo(" << hostname << ") failed! "
-                  << gai_strerror(ret) << std::endl;
+        BOOST_LOG_SEV(lg, severity_level::error) << "getaddrinfo(" << hostname 
<< ") failed! "
+                  << gai_strerror(ret);
         return nullptr;
     }
 

-----------------------------------------------------------------------

Summary of changes:
 client/threads.cc  |  8 ++-----
 daemon/main.cc     |  6 +++--
 devices/onewire.cc |  2 +-
 devices/ownet.cc   |  2 +-
 lib/log.cc         | 13 +++++++---
 lib/log.h          |  1 +
 lib/tcpip.cc       | 70 ++++++++++++++++++++++++++++++++----------------------
 lib/tcputil.cc     |  4 ++--
 8 files changed, 62 insertions(+), 44 deletions(-)


hooks/post-receive
-- 
powerguru



reply via email to

[Prev in Thread] Current Thread [Next in Thread]